Android - How get Mat from camera ?

asked 2014-07-25 10:12:18 -0500

Dany B. gravatar image

I'm trying to retrieve images (Mat) from camera on android device, and I have some problems.

I first tried to use VideoCapture class with method but i get error 11 (SIGSEGV) when i open the camera. I think this bug is caused by hardware camera management because it works on emulator/webcam.

So, to avoid this bug, i choose to use CameraBridgeViewBase class.

CameraBridgeViewBase cameraBridge = (CameraBridgeViewBase) ((Activity) context).
// Camera c =;
// List<Size> sizes = c.getParameters().getSupportedPictureSizes();
// c.release();
// c = null;
// for (int i = 0; i < sizes.size(); i++)
// {
// Log.d(tag, sizes.get(i).width + "x" + sizes.get(i).height);
// }


// cameraBridge.setMaxFrameSize(_widthImage, _heightImage);
// cameraBridge.getHolder().setFixedSize(_widthImage, _heightImage);
// cameraBridge.getHolder().setFixedSize(1, 1);


And the listener method.

public Mat onCameraFrame(CvCameraViewFrame inputFrame)
    this._image = inputFrame.rgba().clone();
    Log.d(tag, "onCameraFrame : " + this._image.size());
    return inputFrame.rgba();
    // return null;

It works, I get a Mat for each frame displayed but I would like to hide the preview.

I also have a ratio problem with Mat when I change the maxFrameSize (commented code). I have to minimize image size to improve image processing. The default size is 720x480 (ratio 3:2). My size is the minimum supported size : 320x240 (ratio 4:3).

Do you have any idea to solve my problems ?

edit retag flag offensive close merge delete