Christina Tsangouri
Christina Tsangouri

Reputation: 187

onpreviewcallback byte array to mat opencv

Hi I'm trying to use Android's PreviewCallback to perform some processing with opencv on the frames captured OnPreviewFrame. The problem I'm having is while testing that everything works before the processing, when setting the byte[] to a Mat and then the Mat to a bitmap, the bitmap looks like color noise.

mCamera.setPreviewCallback(new Camera.PreviewCallback() {
    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
    Mat color = new Mat(480,640, CvType.CV_8UC1);
    color.put(0, 0, data);
    Mat imageMat = new Mat(480,640,CvType.CV_8UC1);
    Bitmap bmpOut = Bitmap.createBitmap(color.width(),color.height(),
                                        Bitmap.Config.ARGB_8888);

    Imgproc.cvtColor(color,imageMat, Imgproc.COLOR_YUV420sp2RGBA);

    Utils.matToBitmap(imageMat,bmpOut,true);
    image.setImageBitmap(bmpOut);
}

Any help would be greatly appreciated!

Upvotes: 0

Views: 2616

Answers (1)

Edson Menegatti
Edson Menegatti

Reputation: 4036

Actually, the size of the Mat is not supposed to be the size of the selected preview.

The width of your matrix needs to be 1.5 times bigger than the preview size as follows:

// Copy preview data to a new Mat element 
Mat mYuv = new Mat(frameHeight + frameHeight / 2, frameWidth, CvType.CV_8UC1);
mYuv.put(0, 0, data);

Afterwards, to create a Bitmap I usually do:

// Convert preview frame to rgba color space
final Mat mRgba = new Mat();
Imgproc.cvtColor(mYuv, mRgba, Imgproc.COLOR_YUV2BGR_NV12, 4);

// Converts the Mat to a bitmap.
Bitmap bitmap = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(img, bitmap);

The matToBitmap method is a utility method that saves up a lot of effort in the conversion to Bitmap, so I strongly advise to use it as oppose to creating by yourself.

Upvotes: 3

Related Questions