Java图片比对

 在自动化测试中,除了普通的值验证,经常还有一些图片验证,比如图片的匹配率,输出图片的差异图片等。本文主要用到了BufferedImage类来操作图片比对和输出差异图片,大体的思路如下:

1. 通过ImageIO读入图片,生成相应的BufferedImage实例(Image操作流)

2. 修改目标图片的尺寸大小,以适应期望图片的大小(为像素比对做准备)

3. 获取每一个(width,height)的ARGB,并获取相应的Red, Green,Blue的值

4. 按照每个像素点的R,G,B进行比较(需要定义允许的R,G,B的误差)

5. 统计不同的像素点,生成diff图片

 

代码如下:

 

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<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++;
                    }
                }
            }

            //保存差异图片
            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");
    }
}

  

转载于:https://www.cnblogs.com/AlwinXu/p/7103493.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java中进行OCR(光学字符识别)并比较两个图像,您可以使用Tesseract OCR库。以下是一个示例代码,演示了如何使用Tesseract进行OCR和图像比对: 首先,确保您已将Tesseract OCR库添加到您的Java项目中。然后,使用以下代码示例进行图像OCR和比对: ```java import net.sourceforge.tess4j.ITesseract; import net.sourceforge.tess4j.Tesseract; import net.sourceforge.tess4j.TesseractException; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class ImageComparator { public static void main(String[] args) { String image1Path = "path/to/image1.png"; String image2Path = "path/to/image2.png"; try { BufferedImage image1 = ImageIO.read(new File(image1Path)); BufferedImage image2 = ImageIO.read(new File(image2Path)); String text1 = performOCR(image1); String text2 = performOCR(image2); if (text1.equals(text2)) { System.out.println("图像内容相同"); } else { System.out.println("图像内容不同"); } } catch (IOException e) { e.printStackTrace(); } } private static String performOCR(BufferedImage image) { ITesseract tesseract = new Tesseract(); tesseract.setDatapath("path/to/tessdata"); // 设置tessdata文件夹的路径 try { return tesseract.doOCR(image); } catch (TesseractException e) { e.printStackTrace(); return ""; } } } ``` 请确保将`image1Path`和`image2Path`变量替换为您要比对的两个图像的实际路径。该代码将使用Tesseract进行OCR,并将提取的文本存储在两个字符串变量中,然后比较这两个字符串的内容。 请注意,上述代码需要Tesseract OCR库以及其训练数据文件(位于`tessdata`文件夹中)。您需要下载和配置这些文件,以便Tesseract能够正确进行OCR。 希望这可以帮助到您!如果您有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值