Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

onCameraFrame Mat to Jpeg

Hi, So I'm a bit new to OpenCV and android :) I'm using the camera and want to capture the the frame data and stream it out of the device. I get the frame data in the onCameraFrame method and have the following questions:

  • In what type does the data in Mat is? I saw somewhere that it might be YUV but from the samples as well as some tests I've done its more likely to be RGBA?
  • I need to convert the data to Jpeg but when I use the following code I get a corrupted JPEG image. Any ideas what's wrong?

        Mat matRGB = new Mat();
        Mat matYUV = new Mat();
        Imgproc.cvtColor(frame.mat_, matRGB, Imgproc.COLOR_RGBA2RGB, 3);
        Imgproc.cvtColor(matRGB, matYUV, Imgproc.COLOR_RGB2YUV, 3);
        int iImageWidth = matYUV.cols(); //frame.sFrameResolution_.width;
        int iImageHeight = matYUV.rows(); //frame.sFrameResolution_.height;
        int iRawFrameDataSize = (int) * matYUV.channels();
        byte buffer[] = new byte[iRawFrameDataSize];
        matYUV.get(0, 0, buffer); previewRect = new, 0, iImageWidth, iImageHeight);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final YuvImage image = new YuvImage(buffer,, iImageWidth, iImageHeight, null);
        image.compressToJpeg(previewRect, 100, baos);
        byte[] jdata = baos.toByteArray();