The example of this article tells the way to compare two pictures of Java. Share it for everyone for your reference. The specifics are as follows:
summary:
Importjava.awt.image.bufferedImage;
Importjava.io.file;
Importjavax.imageio.imageio;
/*** Compare the similarity of the two pictures*@Authorguihu **/PublicClassBMPLOADER {// Change to binary code
publicstring [] [] [] getpx (stringargs) {int [] rgb = newint [3];
Filefile = newfile (args);
BufferedImagebi = null;
try
The main code is as follows:
Import java.awt.image.buffRedimage; Import Java.iO.File; Import Javax.imageio.imageio;/** * The similarity of the two pictures * @Author Guihua * */Public Class s bmploader {// change to binary Code Public Static String [] [] Getpx (String ARGS) {int [] rgb = New Int [3]; File File = New File (ARGS); buffredimage bi = null; TRY {bi = imageio.rea d (file); } Catch (Exception E) {e.printstacktrace ();} int Width = bi.getWidth (); int height = bi.getheight (); int minX = bi.getminx (); getminy (); String [] [] list = new string [width] [height]; for (int i = minx; i <width; i ++) {for (int j = miny; j ++) {int pixel = bi.getRgbb (i, j); rgb [0] = (pixel & 0xff0000) >> 16; RGB [1] = (pixel & 0xff00) >> 8; rgb [2] = (pixel & 0xff); list [i] [i] [i] [i] [i] [i] [i] [i] [i] [i] [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);} // analyze image similarity begin s s tring [] [] List1 = getpx (images [0]); string [] [] list2 = getpx (images [1]); int xiangsi = 0; int busi = 0; int i = 0, j = 0; for (string [string [string [string [string [string [string ] Strings: list1) {if ((i+1) == list1.length) {Continue;} for (int m = 0; m <strings.length; m ++) {try {string [] value1 = list1 [i] [j] .tring (). split (",", "); string [] value2 = list2 [i] [j] .tring (). split (", "); int k = 0; for (int n = 0 0 0 0 0 ; n <value2.length; n ++) {if (math.abs (integer.parseint (value1 [k]) -edeger.parseint (value2 [k]) <5) {xiangsi ++;} else {busi ++; }} Catch (Runtimeexception E) {Continue;} j ++;} I ++;} list1 = getpx (images [1]); list2 = getpx (images [0]); gs: list1) {if ((i+1) == list1.length) {Continue;} for (int m = 0; m <strings.length; m ++) {try {string [] value1 = list1 [i] [j] .tring (). split (",", "); string [] value2 = list2 [i] [j] .tring (). split (", "); int k = 0; for (int n = 0; n <<<<<<<<<<<<<< Value2.Length; n ++) {if (math.abs (integer.parseint (value1 [k]) -Cleger.parseint (Value2 [K]) <5) {xiangsi ++;} else {busi ++;}}}} catch (runtimeexception e) {Continue;} j ++;} I ++;} String Baifen = "" ""; Try {baifen = (Double.parsed allowance (xiangsi+"") / Double.parsed allowance ((Busi+xiangsi)+"" "" "" )) + " "); baifen = baifen.substring (baifen.indexof (". ") + 1, baifen.indexof (". ");} Catch (Exception E) {baifen =" 0 ";} if (baifen. length () <= 0) {baifen = "0";} if (busi == 0) {baifen = "100";} System.out.println ("Similar pixel quantity: + xiangsi +" : " + BUSI +" Similarity: " + Integer.parseint (Baifen) +"%");} Public Static void main (string [] args) {bmploader.compareimage (" e: //12.bmp "," E: //1.bmp ");}}It is hoped that this article is helpful to everyone's Java program design.