/**
* 图片相似度比对
* 以下部分为两个文件进行像素比对的算法实现,获取文件的像素个数大小,然后使用循环的方式将两张图片的
* 所有项目进行一一对比,如有任何一个像素不相同,则退出循环,计算相似度
* 表示两张图片并不是完全匹配
* @param expectedPath 预期图片
* @param actualPath 实际图片
*/
public static void pictureComparison( File fileInput, File fileOutPut) {
BufferedImage bufileInput = null;
try {
bufileInput = ImageIO.read(fileInput);
} catch (IOException e) {
e.printStackTrace();
}
DataBuffer dafileInput = bufileInput.getData().getDataBuffer();
int sizefileInput = dafileInput.getSize();
BufferedImage bufileOutPut = null;
try {
bufileOutPut = ImageIO.read(fileOutPut);
} catch (IOException e) {
e.printStackTrace();
}
DataBuffer dafileOutPut = bufileOutPut.getData().getDataBuffer();
int sizefileOutPut = dafileOutPut.getSize();
int flagF=0;
if (sizefileInput == sizefileOutPut) {//像素
for (int j = 0; j < sizefileInput; j++) {
if (dafileInput.getElem(j) != dafileOutPut.getElem(j)) {
flagF++;
}
}
}else {
flagF=(sizefileInput+sizefileOutPut)/2;//相识度为0
}
double similarity=1-(double)(flagF*2)/(sizefileInput+sizefileOutPut);//相似度
Assert.assertTrue(similarity==1, "图片对比相似度:"+similarity);
}