2

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;
}
Wozywors
  • 59
  • 6
  • 1
    Assuming `pbFrame` is a byte array containing in BGR triplets, try using `BufferedImage.TYPE_3BYTE_BGR`, forget about the shifting, and you should be good. However, in your loop you use `i = i + 4`, which seems that your data is not BGR24, but rather "BGRx32"? In that case, you might need to re-pack your samples to BGR triplets, or create a custom `SampleModel`/`ColorModel` for "TYPE_4BYTE_BGRX" (which does not exist in `BufferedImage`, so you'll end up with a `TYPE_CUSTOM` image). – Harald K Apr 01 '20 at 11:04
  • 1
    For the fastest way to *create* a `BufferedImage` from a byte array and avoid any copying, see [this answer](https://stackoverflow.com/a/30257072/1428606). For BGR24, simply change `samplesPerPixel` to `3` and `bandOffsets` to `{2, 1, 0}`. – Harald K Apr 01 '20 at 11:14

0 Answers0