Search code examples
androidopencvcamerajava-native-interfaceandroid-camera

setPreviewSize doesn't work propertly only in some devices


I'm developing a application using OpenCV native, and opening the camera in Java. My app works propertly in a MotoG, but when I use other devices, it fails.

My preview callback send the image and I got this Error:

E/Camera-JNI(22013): Callback buffer was too small! Expected 3110400 bytes, but got 460800 bytes!

My camera is propertly opened using:

_camera.startPreview();
...
for (Size s : parameters.getSupportedPreviewSizes()) 
...
parameters.setPreviewSize(desiredSize.width, desiredSize.height); // 640x480 in runtime

But, the error means something went wrong with the buffer.

460800 = 640x480x1.5 // width x height x YUV format, as I configured in setpreviewsize
3110400 = 1920x1080x1.5 // other size x YUV format, I didn't do this!

I found in the device where doesn't work some disturbing logs:

E/mm-camera-sensor(319): port_sensor_caps_reserve:155 ide 30002 stream type 1 w*h 1920*1080
...
I/QCameraParameters(303): initDefaultParameters: supported preview sizes: 1920x1080,1440x1080,1280x720,1056x864,960x720,720x480,640x480,352x288,320x240,176x144
I/QCameraParameters(303): set Default prview sizes : 1920x1080
I/ShotCommon(303): Preview width(1920), height(1080)
I/ShotCommon(303): Preview color format [yuv420sp]

So, It seems that in some devices, preview is not opened with provided resolution. What is going on?


Solution

  • It seems to Nexus devices fails if you call:

     mCamera.setPreviewDisplay(mSurfaceHolder);
    

    before calling:

    Camera.Parameters parameters = mCamera.getParameters();
    parameters.setPreviewFormat(ImageFormat.NV21);
    parameters.setPreviewSize(desiredSize.width, desiredSize.height);
    mCamera.setParameters(parameters);
    

    So, if you want your camera works on Nexus, first set preview size and then set surface holder to preview display.