通过将图片转换成二进制来比较图盘的相似度 ! 别的不多说,这个比较简单,直接看代码!
package com.tgb.image;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
/**
* @author
* @Description:
* @date
*/
public class App {
public static void main(String[] args) {
String imagePath1="/Users/caoxueliang/Desktop/3.jpeg";
String imagePath2="/Users/caoxueliang/Desktop/4.jpeg";
try {
BufferedImage image1 = ImageIO.read(new File(imagePath1));
BufferedImage image2 = ImageIO.read(new File(imagePath2));
int width1 = image1.getWidth();
int height1 = image1.getHeight();
int width2=image2.getWidth();
int height2=image2.getHeight();
if(width1!=width2 || height1!=height2){
System.out.println("图片大小不一致");
}else{
int totalCount=width1*height1;
int diffCount=0;
for (int i=0;i<width1;i++){
for (int j=0;j<height1;j++){
int bit1=image1.getRGB(i,j);
int bit2=image2.getRGB(i,j);
if(bit1!=bit2){
diffCount=diffCount+1;
}
}
}
if(diffCount<totalCount*0.15){
System.out.println("是同一个图片,误差为:"+(diffCount*1.0/totalCount*1.0));
}else{
System.out.println("误差过大,不是同一张图片");
}
}
}catch (Exception e){
e.printStackTrace();
}
}
}