| | |
| | | package com.whyc.util; |
| | | |
| | | import javax.imageio.ImageIO; |
| | | import javax.xml.bind.DatatypeConverter; |
| | | import java.awt.*; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.IOException; |
| | | import java.security.MessageDigest; |
| | | import java.security.NoSuchAlgorithmException; |
| | | |
| | | public class ImageDiff { |
| | | |
| | |
| | | } |
| | | |
| | | } |
| | | public static boolean compareImagesCheck(String srcImgPath, String targetImgPath) { |
| | | String hash1 = calculateMD5(srcImgPath); |
| | | String hash2 = calculateMD5(targetImgPath); |
| | | // 比较两个哈希值是否相同 |
| | | return hash1 != null && hash2 != null && hash1.equals(hash2); |
| | | /*boolean sameImage = true; |
| | | try { |
| | | |
| | | |
| | | |
| | | BufferedImage srcImg = ImageIO.read(new File(srcImgPath)); |
| | | BufferedImage targetImg = ImageIO.read(new File(targetImgPath)); |
| | | |
| | | int srcHeight = srcImg.getHeight(); |
| | | int srcWidth = srcImg.getWidth(); |
| | | |
| | | //修改待比较图片的尺寸以适应源图片的尺寸 |
| | | targetImg = changeImageSize(targetImg, srcHeight, srcWidth); |
| | | |
| | | |
| | | int srcRgb; |
| | | int targetRgb; |
| | | for (int h = 0; h < srcHeight; h++) { |
| | | for (int w = 0; w < srcWidth; w++) { |
| | | srcRgb = srcImg.getRGB(w, h); |
| | | targetRgb = targetImg.getRGB(w, h); |
| | | *//*if (Math.abs(getRed(srcRgb) - getRed(targetRgb)) > DIFF_ALLOW_RANGE || |
| | | Math.abs(getGreen(srcRgb) - getGreen(targetRgb)) > DIFF_ALLOW_RANGE || |
| | | Math.abs(getBlue(srcRgb) - getBlue(targetRgb)) > DIFF_ALLOW_RANGE) { |
| | | diffImg.setRGB(w, h, RGB_RED); |
| | | diffPointCount++; |
| | | } |
| | | }*//* |
| | | if (Math.abs(targetRgb - srcRgb) > DIFF_ALLOW_RANGE) { //像素点不同,判断为不同图片 |
| | | return false; |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | }catch (Exception ex){ |
| | | ex.printStackTrace(); |
| | | } |
| | | return sameImage;*/ |
| | | } |
| | | public static String calculateMD5(String filePath) { |
| | | try (FileInputStream fis = new FileInputStream(filePath)) { |
| | | // 获取MD5摘要算法的 MessageDigest 实例 |
| | | MessageDigest md = MessageDigest.getInstance("MD5"); |
| | | |
| | | // 读取文件内容并更新摘要 |
| | | byte[] buffer = new byte[1024]; |
| | | int bytesRead; |
| | | while ((bytesRead = fis.read(buffer)) != -1) { |
| | | md.update(buffer, 0, bytesRead); |
| | | } |
| | | |
| | | // 计算哈希值并转换为十六进制字符串 |
| | | byte[] digest = md.digest(); |
| | | return DatatypeConverter.printHexBinary(digest).toUpperCase(); |
| | | } catch (NoSuchAlgorithmException | IOException e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | public static void main(String[] args) throws IOException { |
| | | |
| | | compareImages("C:\\\\Users\\\\29550\\\\Desktop\\\\当前项目\\\\202207图纸管理\\\\图纸升级\\\\fbo-60010nt-t-j-002_a11-dwg.png", |