How to make bmp image from pixel byte array in java

若如初见. 提交于 2019-12-05 14:13:22

You need to pack three bytes into each integer you make. Depending on the format of the buffered image, this will be 0xRRGGBB.

byteToInt will have to consume three bytes like this:

private int[] byteToInt(byte[] data) {
    int[] ints = new int[data.length / 3];

    int byteIdx = 0;
    for (int pixel = 0; pixel < ints.length) {
        int rByte = (int) pixels[byteIdx++] & 0xFF;
        int gByte = (int) pixels[byteIdx++] & 0xFF;
        int bByte = (int) pixels[byteIdx++] & 0xFF;
        int rgb = (rByte << 16) | (gByte << 8) | bByte
        ints[pixel] = rgb;
    }
}

You can also use ByteBuffer.wrap(arr, offset, length).toInt()

Having just a byte array is not enough. You also need to construct a header (if you are reading from a raw format, such as inside a DICOM file).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!