0

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!

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I still seem to be getting color noise when displaying the bitmap in an ImageView.
Did you copy the whole code or did you updated only the matrix dimensions?
Can you paste the code that selects the preview size?
Do you mean this? Camera.Size size = params.getPictureSize(); int frameHeight = size.height; int frameWidth = size.width;
Long shot, but can you check if getPictureSize() return is different from getPreviewSize()? I use the latter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.