Muzikant
Muzikant

Reputation: 8090

Convert android preview frame to OpenCV Mat

I'm trying to capture an image from the camera preview and convert it to a Mat object using OpenCV. I registered the callbak method public void onPreviewFrame(byte[] data, Camera camera) so I receive all preview frames from the camera, but I can't get it to convert to OpenCV Mat object.

I'm currently using the following code to convert the preview data to a Mat object:

Mat previewFrameMat = new Mat(1, data.length, CvType.CV_8U);
previewFrameMat.put(0, 0, data);

Is this the correct way to do it ? Am I using the correct image type (CvType.CV_8U) ?

Upvotes: 7

Views: 8469

Answers (1)

Andrey Kamaev
Andrey Kamaev

Reputation: 30122

Have you seen how OpenCV samples do this?

mYuv = new Mat(getFrameHeight() + getFrameHeight() / 2, 
    getFrameWidth(), CvType.CV_8UC1);

....

mYuv.put(0, 0, data);

Full source can be found in the Android package from SourceForge or here: http://code.opencv.org/projects/opencv/repository/entry/trunk/opencv/samples/android/tutorial-1-addopencv/src/org/opencv/samples/tutorial1/Sample1View.java

Upvotes: 8

Related Questions