乐丶伤心 2019-03-08 10:27 采纳率: 0%
浏览 377

如何将数据库中的BLOB值进行比较筛选重复,BLOB是图片

我想要筛选faceImage列中的BLOB图片哪些是重复的,显示出他的playeerId,,有没有大神帮帮忙 要JAVA的完整代码

图片说明

  • 写回答

1条回答 默认 最新

  • lshen01 2023-03-15 16:16
    关注

    参考GPT和自己的思路:

    首先,比较两个BLOB是否相等,我们可以使用二进制比较。因为图片是二进制数据,所以如果两个BLOB的二进制数据完全相等,那么这两个BLOB就是重复的。

    以下是JAVA的代码实现:

    public class ImageCompare {
        
        public static void main(String[] args) throws Exception {
            Connection conn = getConnection();  //获取数据库连接
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery("SELECT DISTINCT t.playerId, t.faceImage FROM player t");  //查询faceImage列的去重数据
            Map<String, byte[]> map = new HashMap<String, byte[]>();  //用于存储BLOB数据及其对应的playerId
            while (rs.next()) {
                String playerId = rs.getString(1);
                Blob blob = rs.getBlob(2);
                byte[] data = blob.getBytes(1, (int) blob.length());  //获取BLOB数据的字节数组
                String key = Arrays.toString(data);  //将字节数组转换为字符串作为key
                if (map.containsKey(key)) {  //如果已有相同数据,输出playerId
                    System.out.println(playerId + " is duplicate!");
                } else {
                    map.put(key, playerId.getBytes());  //将BLOB数据及其对应的playerId存储到map中
                }
            }
            rs.close();
            st.close();
            conn.close();
        }
        
        private static Connection getConnection() throws SQLException {
            String url = "jdbc:mysql://localhost:3306/mydb";
            String user = "root";
            String password = "root";
            return DriverManager.getConnection(url, user, password);  //连接数据库
        }
        
    }
    

    上述代码通过查询faceImage列去重数据,并使用HashMap存储BLOB数据(key)及其对应的playerId(value),来比较和筛选重复图片。如果map中已有相同的BLOB数据,就输出该图片对应的playerId,否则将该BLOB数据及对应的playerId存入map中。

    希望能对你有所帮助!

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料