【问题标题】:Android Camera getSupportedPreviewSizes() errorAndroid 相机 getSupportedPreviewSizes() 错误
【发布时间】:2014-10-02 23:56:00
【问题描述】:

我正在三星 Galaxy Tab 上测试我的 Android 应用程序。当我在平板电脑上打开相机应用程序时,我看到您可以为相机设置的可能分辨率是:

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

但是,当我在我的 Android 应用中打开相机实例并获取支持的预览尺寸时,我得到以下分辨率:

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

为什么支持的分辨率会出现这种差异?我希望我的自定义相机活动有一个全屏相机,但我无法使用小于屏幕尺寸(我的应用程序说是 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();
            }
        }
    }

【问题讨论】:

    标签: android camera screen-resolution


    【解决方案1】:

    为什么支持的分辨率会有这样的差异?

    图片分辨率和预览分辨率不是一回事。前者用于图片。后者用于预览帧。

    我希望我的自定义相机活动有一个全屏相机,但我无法使用小于屏幕尺寸的分辨率(我的应用程序说是 800 x 1344)。

    是的,你可以。 Android 会很高兴地拉伸或缩小您的预览帧以匹配您用于预览的TextureViewSurfaceView 的大小。事实上,这种拉伸或收缩几乎是不可避免的,因为相机不太可能支持任意预览分辨率,而这种分辨率恰好与您为预览选择的View 尺寸相匹配。

    【讨论】:

    • 感谢您的建议。我在 FrameLayout 中有我的 cameraPreview 并将 cameraPreview 设置为受支持的预览尺寸之一。然后如何让它拉伸以适应 FrameLayout?
    • @scientiffic:就像你得到任何东西来填充FrameLayout:将TextureViewSurfaceView的宽度和高度设置为match_parent。总体而言,这是否是正确的答案取决于您如何确定FrameLayout 的大小。请记住,“全屏”的相机预览确实比屏幕更大,因为屏幕的纵横比很少等于预览帧的纵横比。我必须在my camera library 中做一些花哨的步法才能完成所有这些工作。
    猜你喜欢
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2023-02-17
    • 2017-10-02
    相关资源
    最近更新 更多