Go to Java to get the pixel color of a picture

Source: Internet
Author: User

public int getpixel (int x,int y) throws awtexception{//function returns the RGB value of the color.
Robot RB = null; The class in the Java.awt.image package can be used to crawl the screen, that is, screenshots.
RB = new Robot ();
Toolkit tk = Toolkit.getdefaulttoolkit (); Get the default toolkit
Dimension di = Tk.getscreensize (); Screen Size Specifications
Rectangle rec = new Rectangle (0,0,di.width,di.height);
BufferedImage bi = rb.createscreencapture (rec);
int pixelcolor = Bi.getrgb (x, y);
return 16777216+pixelcolor; The value of Pixelcolor is negative, after practice: The maximum color value is the actual color value.
}

Another kind

Import Java.awt.image.BufferedImage;
Import Javax.imageio.ImageIO;
Import java.io.*;

public class test{
public static void Main (String args[]) {
int[] RGB = new INT[3];

File File = new file ("A.bmp");
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 ();
System.out.println ("Width=", "+width+", height= "+height+");
System.out.println ("minx=", "+minx+", miniy= "+miny+");

for (int i=minx;i<width;i++) {
for (int j=miny;jSystem.out.print (Bi.getrgb (JW, IH));
int Pixel=bi.getrgb (i, j);
Rgb[0] = (Pixel & 0xff0000) >> 16;
RGB[1] = (Pixel & 0xff00) >> 8;
RGB[2] = (pixel & 0xff);
System.out.println ("i=" +i+ ", j=" +j+ ":(" +rgb[0]+ "," +rgb[1]+ "," +rgb[2]+ ")");

}
}

}


}

Related Article

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.