4

I have an image with TYPE_3BYTE_BGR and I want to convert it to a TYPE_INT_RGB.

Though I have searched, I have not found a method to do this. I want to convert the image pixel by pixel. However, it seems that BufferedImage.getRGB(i, j) doesn't work.

How can I get the RGB values in an image of type TYPE_3BYTE_BGR?

Adam Wagner
  • 15,469
  • 7
  • 52
  • 66
user572485
  • 103
  • 2
  • 5
  • you can do it manually as you are proposing, but it would be much simpler just to create a target BufferedImage with TYPE_INT_RGB, and just draw the source image into the destination BufferedImage. – MeBigFatGuy Oct 19 '11 at 05:23
  • Tried your suggestion. using "result.getGraphics().drawImage(source, 0, 0, null);". Pixels arent reverted in result image. – dernasherbrezon May 15 '13 at 18:17

1 Answers1

2

I'm not sure what you mean by "getRGB(i,j) doesn't work". getRGB returns a packed int; you need to decode it.

int color = image.getRGB(i,j);
int r = (argb)&0xFF;
int g = (argb>>8)&0xFF;
int b = (argb>>16)&0xFF;
int a = (argb>>24)&0xFF;

See How to convert get.rgb(x,y) integer pixel to Color(r,g,b,a) in Java?

Community
  • 1
  • 1
I82Much
  • 26,901
  • 13
  • 88
  • 119
  • I think you can safely ignore the alpha channel since TYPE_3BYTE_BGR has no alpha channel. – I82Much Oct 19 '11 at 05:20
  • yes, I decode it like this, and then set 'setRGB' to draw the TYPE_INT_RGB image, but at last I get two different images. – user572485 Oct 19 '11 at 05:30