Android camera supported picture sizes

2019-01-22 21:14发布

问题:

I am trying to retrieve the available camera image sizes, so I able to adjust the camera to my preferred image resolution.

To retrieve the Android camera size I've used the following code:

camera=Camera.open();
Parameters params = camera.getParameters();
List sizes = params.getSupportedPictureSizes();
for (int i=0;i<sizes.size();i++){
    Log.i("PictureSize", "Supported Size: " +sizes.get(i));         
}

This gives me the following output, which I am not sure how to translate into a size.

"Supported size: android.hardware.Camera$Size@65d4c50"
"Supported size: android.hardware.Camera$Size@65d4a70"
"Supported size: android.hardware.Camera$Size@3fe4e00"
"Supported size: android.hardware.Camera$Size@3fe4cd0"
"Supported size: android.hardware.Camera$Size@18f5600"
"Supported size: android.hardware.Camera$Size@13f7860"

If anyone could help me understand the output it would help me a lot, thanks!

Edit: I ended up solving my problem by doing the following:

camera=Camera.open();
Parameters params = camera.getParameters();
List sizes = params.getSupportedPictureSizes();
Camera.Size result = null;
    for (int i=0;i<sizes.size();i++){
        result = (Size) sizes.get(i);
        Log.i("PictureSize", "Supported Size. Width: " + result.width + "height : " + result.height); 
    }

回答1:

getSupportedPictureSizes() returns a List of Camera.Size objects. Camera.Size has height and width data members that tell you the height and width of the supported picture size.

Here is a sample project that uses the related getSupportedPreviewSizes() to find the preview size with the largest area that is smaller than the SurfaceView's size.



回答2:

Camera camera = Camera.open();
Parameters params = camera.getParameters();
List sizes = params.getSupportedPictureSizes();
for (int i=0;i<sizes.size();i++) {
    Log.i("PictureSize", "Supported Size: " +sizes.get(i).width + "height : " + sizes.get(i).height);     
}   

You have to take height and width from one size object



回答3:

Here is a example of how to search for the size that is aprroaching your max size (1024 * 768) for example.

                List<Camera.Size> sizes = parameters.getSupportedPictureSizes();
                //searches for good picture quality
                Camera.Size bestDimens = null;
                for(Camera.Size dimens : sizes){
                    if(dimens.width  <= 1024 && dimens.height <= 768){
                        if (bestDimens == null || (dimens.width > bestDimens.width && dimens.height > bestDimens.height)) {
                            bestDimens = dimens;
                        }
                    }
                }
                parameters.setPictureSize(bestDimens.width, bestDimens.height);
                camera.setParameters(parameters);


回答4:

This is what I ended up doing. Since the method params.getSupportedPictureSizes() returns a list of type Camera.Size, it was easy to do this:

        List<Camera.Size> sizes = p.getSupportedPictureSizes();

        //searches for good picture quality
        for(Camera.Size dimens : sizes){
            if(dimens.width  <= 1024 && dimens.height <= 768){
                Debug.WriteLine("Width: " + dimens.width + " Height: " + dimens.height);
                return dimens;
            }
        }

        Debug.WriteLine("Width: " + sizes.get(sizes.size()-1).width + " Height: " + sizes.get(sizes.size()-1).height);
        //returns the smallest camera size if worst comes to worst
        return p.getSupportedPictureSizes().get(sizes.size()-1);

You can get a List<Camera.Size> and traverse it like any other list. the Camera.Size object has a width and height property.

Declaring your Listthis way might be better than casting each element of the list to type Camera.Size inside of a loop.

On my devices, if I look at sizes.get(0), it will have the highest resolution while sizes.get( sizes.size()-1 ) has the lowest. I'm not sure if this is true across all devices, however. Anyone have insight on that?