Reputation: 642
I am trying to read the pixel values of the image using the below code
int[] pixel;
BufferedImage imageA = ImageIO.read(new File("xyz.bmp"));
for (int y = 0; y < imageA.getHeight(); ++y) {
for (int x = 0; x < imageA.getWidth(); ++x) {
pixel = imageA.getRaster().getPixel(x, y, new int[3]);
}}
values of RGB are stored in pixel[0],pixel[1] and pixel[2] respectively and when i saw the output i saw the values ranging between 0 to 255. I saw some use the below code to get pixel values
int pixel;
BufferedImage imageA = ImageIO.read(new File("xyz.bmp"));
for (int y = 0; y < imageA.getHeight(); ++y) {
for (int x = 0; x < imageA.getWidth(); ++x) {
pixel = imageA.getRGB(x, y);
}}
When i saw the output for a particular pixel it was -14935264 . What does this value represent and whats the difference between the above two methods.
Upvotes: 1
Views: 501
Reputation: 206996
In the second case, you get an int
that contains the RGB-values in the lower 24 bits. The red component is bits 23-16, the green component is bits 15-8 and the blue component is bits 7-0.
If you want to get the components out of the int:
int red = (pixel >> 16) & 0xFF;
int green = (pixel >> 8) & 0xFF;
int blue = pixel & 0xFF;
The other way around:
int pixel = (red << 16) | (green << 8) | blue;
Upvotes: 0