我不知道这是否很快,但它肯定更有用。我的源数据数组来自 Kinect 的 Color Stream,使用J4KSDK。
我使用这种方法的目标是读取图像的二进制字节。我相信您可以根据自己的用途对其进行修改。
/* 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;
}