how can I convert a byte array from BGR24 to JPG in JAVA?

Viewed 229

At this time I tried the following solution. In this way the image is mirrored and the operations are computationally too onerous. I need to perform operations in about 40 ms.

int[] shifted = new int[CAPTURE_WIDTH * CAPTURE_HEIGHT];

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

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

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

BufferedImage bufferedImage = new BufferedImage(CAPTURE_WIDTH, CAPTURE_HEIGHT, BufferedImage.TYPE_INT_RGB);
bufferedImage.getRaster().setDataElements(0, 0, CAPTURE_WIDTH, CAPTURE_HEIGHT, pbFrame);

String path = "C:\\Users\\Administrator\\Desktop\\images" +
String.format("%03d", n) + ".jpg";
File outputfile = new File(path); //create new outputfile object
ImageIO.write(bufferedImage, "JPG", outputfile);
n++

In C# the following code works:

Bitmap bitmap = new Bitmap(cx, cy, cx*3, PixelFormat.Format24bppRgb, pbFrame);
ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");
bitmap.Save("./test.jpeg", myImageCodecInfo, null);

ImageCodecInfo GetEncoderInfo(String mimeType)
{
    int j
    ImageCodecInfo[] encoders
    encoders = ImageCodecInfo.GetImageEncoders();
    for (j = 0; j < encoders.Length; ++j)
    {
      if (encoders[j].MimeType == mimeType)
        return encoders[j];
    }
    return null;
}
0 Answers
Related