0

I'm looking to read in the RGB values of a bitmap (or the hex colour codes, either work).

I have tried both this code :

 File image = serverConfig.get(map.bmp);
BufferedImage buffer = ImageIO.read(image);
dimX = buffer.getWidth();
dimY = buffer.getHeight();
byte[] pixlesB = (byte[]) buffer.getRaster().getDataElements(0, 0, buffer.getWidth(), buffer.getHeight(), null);

and this code :

File image = serverConfig.get(map.bmp);
BufferedImage buffer = ImageIO.read(image);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(buffer, "bmp", baos );
baos.flush();
byte[] pixlesB = baos.toByteArray();
baos.close();

The both work fine for a small bitmap, but when I load a large bitmap, the data gets compressed and the array returns a bunch of semi random numbers.

for example: A green pixel will read 2,2,2 instead of 0,255,0 A red pixel will read 5,5,5 instead of 255,0,0 A yellow pixel will read 8,8,8 instead of 255,255,0

The bitmaps I'm using only include the colours red, yellow and green. My problem is I have no way of knowing what colour 2,2,2 relates to without checking it manually (which I cannot do since it changes with each bitmap)

I know that there is some metadata in the bitmap that specifies 2 is green, but I don't know how to access it or use it to turn 2 back into 0,255,0

And this is not a duplicate of Java - get pixel array from image since that doesn't mention compressed files. And while I did ask this question a while back, it was just redirected to the above site.

Thanks in advance!

EDIT: Just thought this might make the question a bit clearer. I believe the file is being read correctly, it is just compressed. How do I decompress it?

Alt01
  • 11
  • 3
  • Possible duplicate of [How to get the rgb values of a jpeg image in TYPE\_3BYTE\_BGR?](https://stackoverflow.com/questions/11959626/how-to-get-the-rgb-values-of-a-jpeg-image-in-type-3byte-bgr) – Martín Zaragoza Jul 23 '18 at 17:42

1 Answers1

0

If you want to read an image as a bitmap or as rgb values, you need to transform the image's format first.

Jpeg is a compressed image format, you need to use a tool or library in order to read as rgb.

check this answer: How to get the rgb values of a jpeg image in TYPE_3BYTE_BGR?

Hope this helps

Martín Zaragoza
  • 1,717
  • 8
  • 19