Ask Your Question
0

onCameraFrame Mat to Jpeg

asked 2013-05-02 14:20:15 -0600

Eyal gravatar image

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.total() * matYUV.channels();
    byte buffer[] = new byte[iRawFrameDataSize];
    matYUV.get(0, 0, buffer);
    android.graphics.Rect previewRect = new android.graphics.Rect(0, 0, iImageWidth, iImageHeight);
    
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final YuvImage image = new YuvImage(buffer, android.graphics.ImageFormat.YUY2, iImageWidth, iImageHeight, null);
    image.compressToJpeg(previewRect, 100, baos);
    
    byte[] jdata = baos.toByteArray();
    
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2013-05-03 02:30:47 -0600

rics gravatar image

You can simply call Highgui.imwrite on the Mat as Alexander has already answered here. Quoting from the docs of function:

The image format is chosen based on the filename extension.

edit flag offensive delete link more

Comments

Hi, I'm not sure how this helps me. I have a Mat object acquired by the camera. First, I'm not sure what is its format? RGBA, RGB, YUV? How do I determine this based on the camera or the Mat object? Second, I need to convert the Mat, whatever format it is in, to a YuvImage object so I can later on stream it out of the android device. How do I convert the Mat object to the buffer data that the YuvImage object expects?

Thanks Eyal

Eyal gravatar imageEyal ( 2013-05-03 03:50:36 -0600 )edit
-1

answered 2013-05-03 01:13:30 -0600

NightLife gravatar image

updated 2013-05-03 01:25:31 -0600

Do you wat to save them in JPEG?

if yes , use it :

String fileName = Environment.getExternalStorageDirectory().getPath() +"/IMAGE_NAME.jpg";

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-05-02 14:20:15 -0600

Seen: 4,682 times

Last updated: May 03 '13