开发者

Conversion of a byte[] BGRA array into something more useful/speedy (JAVA)

I have a byte[] array that contains BGRA raster data (e.g. first byte = blue component, second = green, fifth = next pixel, blue) and would like to play with it.

Specifically, is there a Java class that's already designed to wrap something like this? I'm wondering, because I'd like to make my code as neat/correct as possible, and if Java already has a compiled version that's faster, then I'd go with that.

Even more specifically, I want to transform the byte[] array into 2 arrays, where BGR1[] + BGR2[] = BGR, and A1 = A2 = A. Any suggestions?

开发者_开发技巧I could of course just write raw code for this, but perhaps there is a neater/faster way.


I don't know if this is speedy, but it is sure more useful. My source data array came from the Color Stream from Kinect, using J4KSDK.

My goal with this method was to read the binary bytes of an image. I'm sure you can modify it for your own uses.

/* Reference imports */
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/* method */
public byte[] getImage(byte[] bytes) throws IOException {
    int width = 640;
    int height = 480;

    int[] shifted = new int[width * height];

    // (byte) bgra to rgb (int)
    for (int i = 0, j = 0; i < bytes.length; i = i + 4, j++) {
        int b, g, r;

        b = bytes[i] & 0xFF;
        g = bytes[i + 1] & 0xFF;
        r = bytes[i + 2] & 0xFF;

        shifted[j] = (r << 16) | (g << 8) | b;
    }

    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    bufferedImage.getRaster().setDataElements(0, 0, width, height, shifted);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(bufferedImage, "JPG", baos);
    byte[] ret = baos.toByteArray();

    return ret;
}


You could see this other question which has responses for good Java image manipulation libraries.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜