简体   繁体   中英

Byte array of 3/3/2 RGB samples to BufferedImage in Java

I have a byte array, where each byte describes one pixel (256 colors). Here is bitmask that I use: 0xRRRGGGBB So there are 3 bits for R and G components and 2 bits for B component. How can I construct BufferedImage from that array assuming that I know width and height of the image?

First, we must create a data buffer with your data
DataBufferByte buffer = new DataBufferByte(data, data.length);

Next, we need to declare "bandMasks" so the raster can understand your format
int[] bandMasks = {0b11100000, 0b00011100, 0b00000011};

Now, we can create the raster
WritableRaster raster = Raster.createPackedRaster(buffer, width, height, width, bandMasks, null); (FYI, width is specified twice because it is the scansize)

Now we can create the image with the buffer, raster and Color Model
BufferedImage image = new BufferedImage(new DirectColorModel(8, 0b11100000, 0b00011100, 0b00000011), raster, false, null);
Also, you can trim off the proceeding 0's in binary literals because those bits will be 0 by default (0b00000011 is the same as 0b11 or (in decimal) 00029 is the same as 29) you don't need to specify all 32 bits in an integer

I verified the previous code works using this entire segment:

    byte[] data = new byte[]{
        (byte) 0b00000011/*Blue*/, (byte) 0b11100011/*Purple*/,
        (byte) 0b11100011/*Purple*/, (byte) 0b11111111/*White*/};//This is the "image"

    int width = 2, height = 2;

    DataBufferByte buffer = new DataBufferByte(data, data.length);
    int[] bandMasks = {0b11100000, 0b00011100, 0b00000011};

    WritableRaster raster = Raster.createPackedRaster(buffer, width, height, width, bandMasks, null);

    BufferedImage image = new BufferedImage(new DirectColorModel(8, 0b11100000, 0b00011100, 0b00000011), raster, false, null);

    JFrame frame = new JFrame("Test");
    Canvas c = new Canvas();
    frame.add(c);
    frame.setSize(1440, 810);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    while (true) {
        Graphics g = c.getGraphics();
        g.drawImage(image, 0, 0, image.getWidth() * 40, image.getHeight() * 40, null);
    }

I hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM