图片比对java代码_Java图片比对

import javax.imageio.ImageIO;

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.text.DecimalFormat;

public class ImageDiff {

//不同的像素标记为红色

public static final int RGB_RED = 16711680;

//允许的Red,Green,Blue单个维度的像素差值

public static final int DIFF_ALLOW_RANGE = 5;

//不同像素点统计值

public static int diffPointCount = 0;

//从rgb值中抽取red

public static int getRed(int rgbValue){

return rgbValue & 0xff0000 >> 16;

}

//从rgb值中抽取green

public static int getGreen(int rgbValue){

return rgbValue & 0xff00 >> 8;

}

//从rgb值中抽取blue

public static int getBlue(int rgbValue){

return rgbValue & 0xff;

}

/**

* 比较两图片,并用红色标出不同的像素点,然后保存差异图片到本地,打印匹配率

* @param srcImgPath

* @param targetImgPath

*/

public static void compareImages(String srcImgPath,String targetImgPath){

try {

BufferedImage srcImg = ImageIO.read(new File(srcImgPath));

BufferedImage targetImg = ImageIO.read(new File(targetImgPath));

diffPointCount = 0;

BufferedImage diffImg = srcImg;

int srcHeight = srcImg.getHeight();

int srcWidth = srcImg.getWidth();

//修改待比较图片的尺寸以适应源图片的尺寸

targetImg = changeImageSize(targetImg,srcHeight,srcWidth);

int srcRgb;

int targetRgb;

for(int h = 0;h

for(int w=0;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++;

}

}

}

//保存差异图片

ImageIO.write(diffImg,"jpg",new File("diffImg.jpg"));

System.out.println("保存差异图片成功!");

//计算相似度(保留小数点后四位)

int totalPixel = srcHeight*srcWidth;

DecimalFormat decimalFormat = new DecimalFormat("#.####");

double matchRate = (totalPixel-diffPointCount)/(totalPixel*1.0);

System.out.println("图片相似度为: "+decimalFormat.format(matchRate)+"%");

}catch (Exception ex){

ex.printStackTrace();

}

}

/**

* 修改BufferedImage中的图片尺寸,以便和源图片进行比较

* @param image

* @param newHeight

* @param newWidth

* @return

*/

public static BufferedImage changeImageSize(BufferedImage image,int newHeight,int newWidth){

Image img = image.getScaledInstance(newWidth,newHeight,Image.SCALE_SMOOTH);

int width = img.getWidth(null);

int height = img.getHeight(null);

//获取新图片的BufferedImage实例

BufferedImage newBufferedImage = new BufferedImage(width, height,

BufferedImage.TYPE_INT_ARGB);

Graphics g = newBufferedImage.getGraphics();

g.drawImage(img, 0, 0, null);

g.dispose();

return newBufferedImage;

}

public static void main(String[] args){

compareImages("1.jpg","2.jpg");

}

}

原文:http://www.cnblogs.com/AlwinXu/p/7103493.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值