Nexus 4 Camera preview aspect Ratio always require

2020-07-03 04:50发布

问题:

Device Nexus 4 Android ver: 4.2.2

Hoping someone else has found this and can explain how to resolve it....

Nexus 4 supports the following preview sizes: -

W:1280 H:720 Ratio: 1.777777
W:800 H:480 Ratio: 1.6666666
W:768 H:432 Ratio: 1.7777778
W:720 H:480 Ratio: 1.5
W:640 H:480 Ratio: 1.3333334
W:576 H:432 Ratio: 1.3333334
W:480 H:320 Ratio: 1.5
W:384 H:288 Ratio: 1.3333334
W:352 H:288 Ratio: 1.2222222
W:320 H:240 Ratio: 1.3333334
W:240 H:160 Ratio: 1.5
W:176 H:144 Ratio: 1.2222222

myCamera.setPreviewSize() sets them, and when I call myCamera.getPreviewSize() I get the correct one that I set, BUT.... If I set my surface view to the same size as my camera preview then I get a stretched image. e.g.

setPreviewSize(640,480)
getPreviewSize -> I get 640,480
Surface view (640,480) -> Stretched image

Only if I set my Surface view to 16x9 (1.77777) do I get a perfect image.

Surface view (1280,720) -> Perfect image

This is the only device that I have this issue with. Please can someone advise if there is a camera setting I am missing for full screen mode or something that is stopping this from working.

In my long search I found 1 other post which relates also to this issue, but not an answer, just a bit more evidence of my problem PictureCallback not called unless using supportedsizes[0]

Thanks

回答1:

On the Nexus 4, there's an unfortunate issue where the preview aspect ratio and the still picture aspect ratio are tied together, even if you never take a picture. If they don't match, one of the two gets distorted (typically preview, since it is the lower resolution).

If you want to use a 4:3 preview, configure the still picture size to be 4:3 as well, before starting preview. For example, 640x480 preview with a full 8MP picture size (3264 x 2448) on N4 should cause no stretching for preview.

Use Camera.Parameters.setPictureSize to select the picture size; the list of available sizes can be read from Camera.Parameters.getSupportedPictureSizes.



回答2:

This method calculate the best (for me) screen size for each device. But, I have the same problem like you when I tried this code in the Nexus 4. So, my solution is to have a special case in the end of this method which gets the width of the nexus 4 and calculates the best height for this device.

The last case could be used in all devices. You could delete the first part of the method.

private void setAspectResolutionCamera(Parameters camParams, int screen_width, int screen_height) {
    boolean chosen_one_resolution = false;

    //Init screen sizes
    width_video = ConstantsCamera.VIDEO_ASPECT_WIDTH;
    height_video = ConstantsCamera.VIDEO_ASPECT_HEIGHT;

    float aspect_ratio = 1f;
    int aspect_width = 6000, aspect_height = 6000;
    List<Size> supported_sizes_list = camParams.getSupportedPreviewSizes();
    for (int i = 0; i < supported_sizes_list.size(); i++) {
        Size size = supported_sizes_list.get(i);

        float aspect = (float) size.height / size.width;
        if (ConstantsCamera.VIDEO_ASPECT_RATIO - aspect <= aspect_ratio && (aspect - ConstantsCamera.VIDEO_ASPECT_RATIO >= 0)) {

            if (screen_width - size.height <= aspect_width && size.height - screen_width >= 0) {

                if (screen_height - size.width < aspect_height) {
                    height_video = size.width;
                    width_video = size.height;
                    aspect_ratio = ConstantsCamera.VIDEO_ASPECT_RATIO - (float) size.height / size.width;
                    aspect_width = screen_width - size.height;
                    aspect_height = screen_height - size.width;

                    chosen_one_resolution = true;


                }
            }
        }
    }

    //Special case
    if (width_video != screen_width && !chosen_one_resolution) {
        height_video = screen_width * height_video / width_video;
        width_video = screen_width;

    }
}


回答3:

Try setting the size of your surfaceview based on the ratio of the camera parameters that you are using.