Comparison of Java pictures

Source: Internet
Author: User

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 {//different pixels marked red public static final int RGB    _red = 16711680;    allowable pixel difference of red,green,blue single dimension public static final int diff_allow_range = 5;    Statistical value of different pixel points public static int diffpointcount = 0;    Extract red public static int getred (int rgbvalue) from RGB values {return Rgbvalue & 0xff0000 >> 16;    }//Extract green public static int getgreen (int rgbvalue) from RGB values {return Rgbvalue & 0xff00 >> 8;    }//Extract the blue public static int getBlue (int rgbvalue) from the RGB values {return rgbvalue & 0xFF; }/** * Compare two pictures, mark different pixels with red, then save the difference picture to local, print match rate * @param srcimgpath * @param targetimgpath * * Public St atic void Compareimages (String srcimgpath,string targetimgpath) {try {bufferedimage srcimg = imageio.re            AD (new File (Srcimgpath)); BUfferedimage targetimg = Imageio.read (new File (Targetimgpath));            Diffpointcount = 0;            BufferedImage diffimg = srcimg;            int srcheight = Srcimg.getheight ();            int srcwidth = Srcimg.getwidth ();            Modify the dimensions of the image to be compared to fit the size of the source image 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.GETRG                    B (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++;       }                }     }//Save diff picture Imageio.write (diffimg, "JPG", New File ("diffimg.jpg")); System.out.println ("Save diff picture Success!")            ");            Calculates the similarity (retains four digits after the decimal point) int totalpixel = Srcheight*srcwidth;            DecimalFormat DecimalFormat = new DecimalFormat ("#.####");            Double matchrate = (totalpixel-diffpointcount)/(totalpixel*1.0);        System.out.println ("Picture similarity:" +decimalformat.format (matchrate) + "%");        }catch (Exception ex) {ex.printstacktrace ();  }}/** * Modify the image size in BufferedImage to compare with the source image * @param image * @param newheight * @param newwidth * @return */public static bufferedimage changeimagesize (bufferedimage image,int newheight,int newwidth) {Ima        GE img = image.getscaledinstance (newwidth,newheight,image.scale_smooth);        int width = img.getwidth (null);        int height = img.getheight (null); Gets the BufferedImage instance of the new picture 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"); }}

  

Comparison of Java pictures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.