Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Camera Preview Size issue

My app opens the camera using surface holder. Camera opens with a default preview size. On certain devices the default preview size captures more image than is actually visible on the screen. Inorder to avoid this, I started setting the preview size myself. Criterion is to select the preview size from the available list of sizes, which is closest to the width of the screen.

This works well most of the time. But often I stumble upon few devices which doesn't like the preview size I selected.

1) Nexus 4 didn't capture the image for one of the preview sizes I selected. 2) Samsung S 4 gives band of lines(noise) along the captured images.

So I wanted to understand if I am doing anything against the standard. Here are my questions :-

1) I am using surface holder to open the camera. Is there any other way I can use the camera hardware in my app.

2) I am selecting the preview size from the list of available sizes. Are there any specific which I should look for before selecting any size ?. Logic for selecting the size is mentioned below :-

private Camera.Size getBestPreviewSize(int width, int height)
    {
        List<Size> sizes = camera.getParameters().getSupportedPreviewSizes();
        if (sizes == null) return null;

        Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;

        int targetHeight = height;
        int targetWidth = width;

        int minWidthDiff = 0;
        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Size size : sizes) {
                if (Math.abs(size.width - targetWidth) < minDiff) {
                    if(size.width > width) {
                        if (minWidthDiff == 0) {
                            minWidthDiff = size.width - width;
                            optimalSize = size;
                        }
                        else if (Math.abs(size.width - targetWidth) < minWidthDiff) {
                            minWidthDiff = size.width - width;
                            optimalSize = size;

                        }   
                        minDiff = Math.abs(size.width - targetWidth);
                     }
                }
            }
        }

        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }

Any directions on this will really help.

Thanks !! Gagan

like image 580
user2751441 Avatar asked Oct 08 '13 22:10

user2751441


People also ask

How do I add a cameraprovider to a previewview?

Once you have created and confirmed the CameraProvider, do the following: Create a Preview. Specify the desired camera LensFacing option. Bind the selected camera and any use cases to the lifecycle. Connect the Preview to the PreviewView.

Why is my camera preview wider than it should be?

When a phone is in portrait orientation, the camera preview is assumed to be taller than it is wide. When the phone (and camera) are rotated to landscape, the camera preview is expected to be wider than it is tall.

How to measure the size of a cameraview?

CameraView has a smart measuring behavior that will let you do what you want with a few flags. Measuring is controlled simply by layout_width and layout_height attributes, with this meaning: CameraView will choose this dimension, in order to show the whole preview without cropping. The aspect ratio will be respected.

What are the limitations of using previewview?

Using PreviewView has some limitations. When using PreviewView, you can't do any of the following things: Create a SurfaceTexture to set on TextureView and Preview.SurfaceProvider. Retrieve the SurfaceTexture from TextureView and set it on Preview.SurfaceProvider. Get the Surface from SurfaceView and set it on Preview.SurfaceProvider.


1 Answers

When working with the camera you have a few different sizes to consider:

Sensor size The camera has a native resolution and aspect ratio that is determined by the hardware.

Picture size This is the size of the image produced when you tell the camera to take a photo. If it is the same aspect ratio as the native resolution then it will be directly scaled from that. If the aspect ratio is different then it will be cropped from the native size. In my experience, the largest size returned by getSupportedPictureSizes is the native resolution of the camera.

Preview size This is the size of the image preview that is shown on-screen. It may be a different aspect ratio than either the native size or the picture size, causing further cropping.

To get the closest match between what you see on screen and the image that is produced when you take a photo try to select a preview size with an aspect ratio as close as possible to the aspect ratio of the picture size that you've selected. I generally try to get both as close as possible to the native size.

like image 168
goto10 Avatar answered Oct 16 '22 10:10

goto10