Java实现图片比对(2)-逐像素对比

图片对比的另一种方式是通过将图片每一个像素的RGB值提取出来,然后比较两个图片每一个像素的RGB值;该方法的问题是速度比较慢,需要消耗较大的空间;

private static InputStream getImageStreamFromWeb(String urlAddress) {
URL url = null;
HttpURLConnection conn = null;
try {
url = new URL(urlAddress);
conn = (HttpURLConnection) url.openConnection();
if (conn.getResponseCode() == 200) {
InputStream in = conn.getInputStream();
return in;
} else {
throw new PictureNotFoundError("can not found #ADD# picture: "
+ urlAddress);
}
} catch (Exception e) {
}
return null;
}

public static String[][] getPX(InputStream in) {
int[] rgb = new int[3];

BufferedImage bi = null;
try {
bi = ImageIO.read(in);
} catch (Exception e) {
e.printStackTrace();
}

int width = bi.getWidth();
int height = bi.getHeight();
int minx = bi.getMinX();
int miny = bi.getMinY();
String[][] list = new String[width][height];
for (int i = minx; i < width; i++) {
for (int j = miny; j < height; j++) {
int pixel = bi.getRGB(i, j);
rgb[0] = (pixel & 0xff0000) >> 16;
rgb[1] = (pixel & 0xff00) >> 8;
rgb[2] = (pixel & 0xff);
list[i][j] = rgb[0] + "," + rgb[1] + "," + rgb[2];
}
}
rgb = null;
return list;

}

public static boolean compareImage(InputStream img1, InputStream img2) {
// 分析图片相似度 begin
String[][] list1 = getPX(img1);
String[][] list2 = getPX(img2);
int xiangsi = 0;
int busi = 0;
int i = 0, j = 0;
for (String[] strings : list1) {
if ((i + 1) == list1.length) {
continue;
}
for (int m = 0; m < strings.length; m++) {
try {
String[] value1 = list1[i][j].toString().split(",");
String[] value2 = list2[i][j].toString().split(",");
int k = 0;
for (int n = 0; n < value2.length; n++) {
if (Math.abs(Integer.parseInt(value1[k])
- Integer.parseInt(value2[k])) < 5) {
xiangsi++;
} else {
busi++;
}
}
} catch (RuntimeException e) {
continue;
}
j++;
}
i++;
}

list1 = list2;
list2 = list1;
i = 0;
j = 0;
for (String[] strings : list1) {
if ((i + 1) == list1.length) {
continue;
}
for (int m = 0; m < strings.length; m++) {
try {
String[] value1 = list1[i][j].toString().split(",");
String[] value2 = list2[i][j].toString().split(",");
int k = 0;
for (int n = 0; n < value2.length; n++) {
if (Math.abs(Integer.parseInt(value1[k])
- Integer.parseInt(value2[k])) < 5) {
xiangsi++;
} else {
busi++;
}
}
} catch (RuntimeException e) {
continue;
}
j++;
}
i++;
}
String baifen = "";
try {
baifen = ((Double.parseDouble(xiangsi + "") / Double
.parseDouble((busi + xiangsi) + "")) + "");
baifen = baifen.substring(baifen.indexOf(".") + 1, baifen
.indexOf(".") + 3);
} catch (Exception e) {
baifen = "0";
}
if (baifen.length() <= 0) {
baifen = "0";
}
if (busi == 0) {
baifen = "100";
}

list1 = null;
list2 = null;

if (!baifen.equals("100")) {
return false;
} else {
return true;
}

}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在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。 希望这可以帮助到您!如果您有任何其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值