Home » Android » How to set camera resolution in Android with OpenCV?

How to set camera resolution in Android with OpenCV?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m trying to develop an app for Android, and I would need to get uncompressed pictures with a resolution as high as possible from the camera. I tried takePicture’s rawCallback and postviewCallback, but they are not working.

Right now I’m trying with OpenCV (version 2.4) using VideoCapture, but I’m stuck in the default 960×720, which is poor for what I need; and my phone, a Samsung Galaxy S3, is able to provide, theoretically, up to 8Mpx (3,264×2,448 for pictures, and 1,920×1,080 for video, according to Wikipedia). VideoCapture.set(Highgui.CV_CAP_PROP_FRAME_WIDTH/HEIGHT, some number) makes the camera return a black image as far as I’ve found.

Is there any way to obtain a higher resolution, either through OpenCV or with the Android API, without compressing?

I’m really sorry if this has been asked before; I have been looking for days and I have found nothing.

Thank you for your time!

EDIT: Although it is not exactly what I was asking, I found that there is a way to do something very similar: if you set an OnPreviewCallback for the Camera, using setPreviewCallback, you do get the raw picture data from the camera (at least in the S3 I’m working with). I leave it here in case somebody finds it useful in the future.

EDIT: A partial solution is explained in an answer below. To sum up,

vc.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, desiredFrameWidth);
vc.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, desiredFrameHeight);

works under some conditions; please see below for further detail.

How to&Answers:

You have to get supported camera preview resoultions by calling getSupportedPreviewSizes.

After this you can set any resolution with method setPreviewSize. And don’t forget to setParameters in the end. Actally many OpenCV Android examples contain this information (look at sample3).

Answer:

In case anybody ever finds this useful, I found a (partial) solution: If your VideoCapture variable is called vc, this should work:

vc.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, desiredFrameWidth);
vc.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, desiredFrameHeight);

Mind that the combination of width and height must be one of the supported picture formats for your camera, otherwise it will just get a black image. You can get those through Camera.Parameters.getSupportedPictureSizes().

However, setting a high resolution appears to exceed the YUV conversion buffer’s capacity, so I’m still struggling with that. I’m going to make a new separate question for that, to keep everything clearer: new thread

Answer:

setPreviewSize does not set picture resolution. setPictureSize does.