原文:http://www.open-open.com/code/view/1448334323079
import java.awt.image.BufferedImage;import java.io.File;import javax.imageio.ImageIO;/** * 比较两张图片的相似度 * @author Guihua * */public class BMPLoader { // 改变成二进制码 public static String[][] getPX(String args) { int[] rgb = new int[3]; File file = new File(args); BufferedImage bi = null; try { bi = ImageIO.read(file); } 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]; } } return list; } public static void compareImage(String imgPath1, String imgPath2){ String[] images = {imgPath1, imgPath2}; if (images.length == 0) { System.out.println("Usage >java BMPLoader ImageFile.bmp"); System.exit(0); } // 分析图片相似度 begin String[][] list1 = getPX(images[0]); String[][] list2 = getPX(images[1]); 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