Read the pixel value of an image as gray level.
Hi all,
I have an image which is converted to gray scale, now i want to read the pixel of the image( which is of the range of 0 to 255) but I am not getting it correct since I am using getrgb() to read the pixel which returns me rgb value as a default.
How can I get the pixel in the range of gray scale as from 0 to 255.
Here is the code that I have written.
{
Image image = ImageIO.read(new File("C:/test1.jpg"));
BufferedImage image1= convert(image);
int w = image1.getWidth();
int h = image1.getHeight();
System.out.println("Height " +h+ ", Width " +w);
int[] rgbs = new int[w*h];
int x = 0;
image1.getRGB(0, 0, w, h, rgbs, 0, w);
for (int i=0; i<2; i++)
for (int j=0;j<2;j++)
{
System.out.println("Pixel " + i +"," + j + "has " + "RGB values of "+ rgbs[x]);
x++;
}
}
//
public static BufferedImage convert(Image im)
{
BufferedImage bi = new BufferedImage(im.getWidth(null),im.getHeight(null),BufferedImage.TYPE_BYTE_GRAY);
Graphics bg = bi.getGraphics();
bg.drawImage(im, 0, 0, null);
bg.dispose();
return bi;
}
Thank you.

