Android Camera getSupportedPreviewSizes() error

2019-09-19 02:52发布

问题:

I am testing my Android app on a Samsung Galaxy Tab. When I open up the camera application on the tablet, I see that the possible resolutions you can set for the camera are:

  • 2560 x 1920
  • 2560 x 1440
  • 2048 x 1536
  • 2048 x 1152
  • 1600 x 1200
  • 1536 x 864
  • 640 x 480

However, when I open up a camera instance in my Android app and fetch the supported preview sizes, I get the following resolutions:

  • 1024 x 768
  • 1280 x 720
  • 1024 x 576
  • 768 x 512
  • 640 480
  • 352 x 288
  • 320 x 240

Why is there such a discrepancy in the supported resolutions? I want my custom camera activity to have a full screen camera, and I can't do it with resolutions that are smaller than the screen size (which my app says is 800 x 1344).

    // fetch screen size
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int width = metrics.widthPixels;
    int height = metrics.heightPixels+navBarHeight;
    Log.d(TAG, "screen width and height: " + width + " " + height);

    // get supported preview sizes
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for(int camIdx = 0; camIdx < cameraCount; camIdx++){
        Camera.getCameraInfo( camIdx, cameraInfo );
        if(cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK){
            try {
                c = Camera.open(); // attempt to get a Camera instance
                for(int i =0; i< c.getParameters().getSupportedPreviewSizes().size(); i++){
                    Size size = c.getParameters().getSupportedPreviewSizes().get(i);
                    Log.d(TAG, "supported preview size: " + size.width + " " + size.height);
                }
            } catch (Exception e) {
                // Camera is not available (in use or does not exist)
                e.printStackTrace();
            }
        } else if(cameraCount == 1 && cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)          {
            // device only has a front facing camera
            try {
                c = Camera.open(camIdx); // attempt to get a Camera instance
            } catch (Exception e) {
                // Camera is not available (in use or does not exist)
                e.printStackTrace();
            }
        }
    }

回答1:

Why is there such a discrepancy in the supported resolutions?

Picture resolutions and preview resolutions are not the same thing. The former is for pictures. The latter is for the preview frames.

I want my custom camera activity to have a full screen camera, and I can't do it with resolutions that are smaller than the screen size (which my app says is 800 x 1344).

Yes, you can. Android will happily stretch or shrink your preview frames to match the size of the TextureView or SurfaceView that you are using for the preview. In fact, such stretching or shrinking is pretty much unavoidable, since it is rather unlikely that the camera will support some arbitrary preview resolution, one that happens to match whatever you are choosing for your preview View size.