Pixel value of the output image in java
I created a File, file File = new File ("e: // 1 // 4.bmp"); Call BufferedImage bi = ImageIO. read (file); read this image and read a grayscale image. The pixel value of this image is only 255 and 0, and the System is called. out. print (bi. getRGB (jw, ih); output the pixel value of the image, but 255 output (-16777216) through the above program ). What's going on? How can we get 255 and 0?
Best Answer
Import java. awt. image. bufferedImage; <br/> import javax. imageio. imageIO; <br/> import java. io. *; </p> <p> public class Test {<br/> public static void main (String args []) {<br/> int [] rgb = new int [3]; </p> <p> File file = new File ("a.bmp "); <br/> BufferedImage bi = null; <br/> try {<br/> bi = ImageIO. read (file); <br/>}catch (Exception e) {<br/> e. printStackTrace (); <br/>}</p> <p> int width = bi. getWidth (); <br/> int height = bi. getHeight (); <br/> int minx = bi. getMinX (); <br/> int miny = bi. getMinY (); <br/> System. out. println ("width =" + width + ", height =" + height + ". "); <br/> System. out. println ("minx =" + minx + ", miniy =" + miny + ". "); </p> <p> for (int I = minx; I <width; I ++) {<br/> for (int j = miny; j <peight; j ++) {<br/> // System. out. print (bi. getRGB (jw, ih); <br/> int pixel = bi. getRGB (I, j); <br/> rgb [0] = (pixel & 0xff0000)> 16; <br/> rgb [1] = (pixel & 0xff00)> 8; <br/> rgb [2] = (pixel & 0xff); <br/> System. out. println ("I =" + I + ", j =" + j + ":(" + rgb [0] + "," + rgb [1] + ", "+ rgb [2] + ")"); </p> <p >}< br/>}</p> <p >}< br/>