1

I want to read in a bitmap so that I have an array of the 256-RGB values for each pixel.

Currently my code is:

    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);

This produces a byte array of the bitmap e.g.

[pixel1Red,pixel1Green,pixel1Blue,pixel2Red,pixel2Green,pixel2Blue,...]

My problem is that when I load a large bitmap (currently the one I'm trying is about 706,000 pixles^2) the bitmap lossless compresses the file and I just get a string of semi-meaningless numbers. Is there any way to force java to read out the RGB values for all bitmaps, like it does for small ones?

EDIT:

To clarify, I am getting back a [pixel1Red,pixel1Green,pixel1Blue,pixel2Red,pixel2Green,pixel2Blue,...] style list, but the values in there aren't the 0-255 bytes I'm expecting, they're just random, compressed numbers. I need to actual 0-255 values for RGB (or better yet just a byte array of all the hex values) in order for the rest of my code to reliably work.

Alt01
  • 11
  • 3
  • Tryed such solution? https://stackoverflow.com/questions/6524196/java-get-pixel-array-from-image – Marcos Vasconcelos Jul 04 '18 at 21:17
  • Yes, but this still isn't working. To clarify, I am getting back a [pixel1Red,pixel1Green,pixel1Blue,pixel2Red,pixel2Green,pixel2Blue,...] style list, but the values in there __aren't__ the 0-255 bytes I'm expecting, they're just random, compressed numbers. I need to actual 0-255 values for RGB (or better yet just a byte array of all the hex values) – Alt01 Jul 22 '18 at 00:11

1 Answers1

0

Try this method:

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();
NiVeR
  • 9,644
  • 4
  • 30
  • 35