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 VideoCapture.read(Mat) 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).
getWindow().getDecorView().findViewById(R.id.image);
// Camera c = Camera.open(_idCamera);
// 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.setCameraIndex(_idCamera);

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

cameraBridge.setVisibility(SurfaceView.VISIBLE);
cameraBridge.setCvCameraViewListener(HeadPilotCamera.this);
cameraBridge.enableView();

And the listener method.

@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame)
{
    this._image = inputFrame.rgba().clone();
    Log.d(tag, "onCameraFrame : " + this._image.size());
    this._waitForImage.release();
    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