Original: http://www.open-open.com/code/view/1448334323079
ImportJava.awt.image.BufferedImage;ImportJava.io.File;ImportJavax.imageio.ImageIO;/*** Compare the similarity of two pictures *@authorGuihua **/ Public classBmploader {//change into binary code Public Staticstring[][] getpx (String args) {int[] RGB =New int[3]; File File=NewFile (args); BufferedImage Bi=NULL; Try{bi=imageio.read (file); } Catch(Exception e) {e.printstacktrace (); } intwidth =bi.getwidth (); intHeight =bi.getheight (); intMinx =Bi.getminx (); intMiny =Bi.getminy (); String[][] List=NewString[width][height]; for(inti = Minx; I < width; i++) { for(intj = miny; J < height; J + +) { intPixel =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]; } } returnlist; } Public Static voidcompareimage (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 beginstring[][] List1 = GETPX (images[0]); String[][] List2= GETPX (images[1]); intXiangsi = 0; intBusi = 0; inti = 0, j = 0; for(string[] strings:list1) {if((i + 1) = =list1.length) {Continue; } for(intm=0; m<strings.length; m++) { Try{string[] value1= List1[i][j].tostring (). Split (","); String[] value2= List2[i][j].tostring (). Split (","); intK = 0; for(intn=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= GETPX (images[1]); List2= GETPX (images[0]); I= 0; J= 0; for(string[] strings:list1) {if((i + 1) = =list1.length) {Continue; } for(intm=0; m<strings.length; m++) { Try{string[] value1= List1[i][j].tostring (). Split (","); String[] value2= List2[i][j].tostring (). Split (","); intK = 0; for(intn=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"; } System.out.println ("Number of similar pixels:" + Xiangsi + "number of pixels not similar:" + busi + "similarity rate:" + integer.parseint (baifen) + "%"); } Public Static voidMain (string[] args) {bmploader.compareimage ("E:\\12.bmp", "E:\\1.bmp"); }}
JAVA comparison code for similarity of two pictures