Android中的相机,如何获得最佳尺寸、预览尺寸、图片尺寸、视图尺寸、图像失真

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21668394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:54:10  来源:igfitidea点击:

Camera in Android, how to get best size, preview size, picture size, view size, image distorted

androidimagecameraandroid-camera

提问by Kevik

having image distorted when mixing a view from OpenGL and the Android camera to get an image of both when using the takepicture method. I checked and found that the camera picture was set to 640X480 and the openGL view and the camera preview was both set to 1280x720.

在使用拍照方法时混合来自 OpenGL 和 Android 相机的视图以获取两者的图像时图像失真。我查了一下,发现相机图片设置为640X480,openGL视图和相机预览都设置为1280x720。

so I set the camera picture size to 1280x720 and the result was perfect. however I can't set the size in code because each Android device will be different and every device the settings for both preview size and picture size have to be selected from a list of supported sizes.

所以我将相机图片大小设置为 1280x720,结果非常完美。但是我无法在代码中设置大小,因为每个 Android 设备都会不同,并且每个设备都必须从支持的大小列表中选择预览大小和图片大小的设置。

what is the best way to do this considering that there are three variables here, the screen size of the activity layout, the camera preview size, and the picture size.

考虑到这里有三个变量,活动布局的屏幕大小、相机预览大小和图片大小,最好的方法是什么。

is it best to use a match_parent or fitXY for the FrameLayout size and only work with two variables, the preview size and picture size?

最好对 FrameLayout 大小使用 match_parent 或 fitXY 并且只使用两个变量,预览大小和图片大小?

notice that several of the width and height combinations in preview sizes and picture sizes are the same. for example 1280 x 720 exists in both preview and picture, is it always the case that there will be matches in both sizes?

请注意,预览尺寸和图片尺寸中的几个宽度和高度组合是相同的。例如,预览和图片中都存在 1280 x 720,是否总是两种尺寸都会匹配?

 List<Size> previewSizes = mCamera.getParameters().getSupportedPreviewSizes();

 List<Size> imageSizes = mCamera.getParameters().getSupportedPictureSizes();

 // layout in the activity that the cameraView will placed in
 int layoutWidth = frameLayout.getWidth();
 int layoutHeight = frameLayout.getHeight();

for example in one Android tablet after measurement these are the results for the 3 variables used

例如,在测量后的一台 Android 平板电脑中,这些是所使用的 3 个变量的结果

size of layout viewGroup in activity as result of measurement

作为测量结果的活动中布局视图组的大小

 1280 x 736

supported picture sizes

支持的图片尺寸

 320 x 240
 640 x 480
 1024 x 768
 1280 x 720 << best size in my example to use
 1280 x 768
 1280 x 920
 1600 x 1200
 2048 x 1536
 2560 x 1440
 2560 x 1536
 2560 x 1920  << native resolution of hardware camera

supported preview sizes

支持的预览尺寸

 176 x 144
 320 x 240
 352 x 288
 480 x 320
 480 x 368
 640 x 480
 800 x 480
 800 x 600
 864 x 480
 864 x 576
 960 x 540
 1280 x 720 << best size in my example to use
 1280 x 768
 1280 x 960

采纳答案by Alex Cohn

Most often, but not always, there is correspondence between picture aspect ratios and preview aspect ratios. You can be assured that at least some of them are classic 4:3 ratios (e.g. 640x480). Support for 16:9 is also widely available.

大多数情况下,但并非总是如此,图片纵横比和预览纵横比之间存在对应关系。您可以放心,其中至少有一些是经典的 4:3 比例(例如 640x480)。对 16:9 的支持也广泛可用。

The screen may have different aspect ratio. To fill it with a camera image correctly, it is widely accepted to add black margins (same approach is used in YouTube). Alternatively, you may crop the camera image to fill the whole screen.

屏幕可能有不同的纵横比。为了正确地用相机图像填充它,人们普遍接受添加黑色边距(YouTube 中使用了相同的方法)。或者,您可以裁剪相机图像以填充整个屏幕。

Note that the screen size (as reported in tech specs for the variety of devices) is not always actually available for your image display. For example, the system menus, and title bar, etc. may take their share of the screen real estate. The immersive modeis available on some devices, and its behavior depends on the system version. You may expect that future developments of Android, e.g. second screen support, will make the game even more interesting.

请注意,屏幕尺寸(如各种设备的技术规格中所报告的)实际上并不总是适用于您的图像显示。例如,系统菜单和标题栏等可能会占用屏幕空间。该沉浸式模式可在某些设备上,它的行为取决于系统版本。您可能期望 Android 的未来发展,例如第二屏幕支持,将使游戏更加有趣

So, the answers to your specific questions:

所以,你的具体问题的答案:

is it best to use a match_parent or fitXY for the FrameLayout size and only work with two variables, the preview size and picture size?- no.

最好对 FrameLayout 大小使用 match_parent 或 fitXY 并且只使用两个变量,预览大小和图片大小?-没有

is it always the case that there will be matches in both sizes?- yes, but maybe this size is not optimal.

两种尺寸的比赛总是如此吗?-是的,但也许这个尺寸不是最佳的。

Even if your camera supports different "wide" picture sizes, 1280x720preview and 2560x1440picture may be the best match, both precisely at 16:9. Usually, the quality of picture at small size is not significantly better than preview at that same size, so you may choose to save the preview frameif 1280x720is what you really need.

即使您的相机支持不同的“宽”图片尺寸,1280x720预览和2560x1440图片也可能是最佳匹配,两者都精确到16:9. 通常情况下,小尺寸图片的质量并不比同等尺寸的预览好1280x720多少,因此如果您确实需要,您可以选择保存预览帧

If you can forgo takePicture(), your app will be much more responsive.

如果您可以放弃takePicture(),您的应用程序将响应更快。

回答by Huy Tower

I think the best way to get preview size, picture sizeis the Camera was set up to support all Android devices. It means the preview size, picture sizeyou decide to set up is appropriate with the popular format that All Devices supported, ex. is 1280x720 etc.

我认为最好的获取方式preview size, picture size是将相机设置为支持所有 Android 设备。这意味着preview size, picture size您决定设置适用于所有设备支持的流行格式,例如。是 1280x720 等。

As in following codes, is the ex. what I used.

在以下代码中,是 ex。我用过的。

// start preview with new settings
    try {
        // set preview size and make any resize, rotate or
        // reformatting changes here
        Camera.Parameters parameters = mCamera.getParameters();

        for (Camera.Size size : parameters.getSupportedPictureSizes()) {
            // 640 480
            // 960 720
            // 1024 768
            // 1280 720
            // 1600 1200
            // 2560 1920
            // 3264 2448
            // 2048 1536
            // 3264 1836
            // 2048 1152
            // 3264 2176
            if (1600 <= size.width & size.width <= 1920) {
                parameters.setPreviewSize(size.width, size.height);
                parameters.setPictureSize(size.width, size.height);
                break;
            }
        }
        // Set parameters for camera
        CustomCamera.mCamera.setParameters(parameters);

        Camera.Size size = CustomCamera.mCamera.getParameters().getPictureSize();

        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();
    } catch (Exception e) {
        e.printStackTrace();
    }

Best sizedepend on Camera MP. So I think should use default if using Camera.

Best size取决于相机 MP。所以我认为如果使用相机应该使用默认值。

回答by Sam

Possible algorithm:

可能的算法:

1) get output area aspect ratio (=width/height)

1) 获取输出区域纵横比(=width/height)

2) iterate through supported sizes:

2)遍历支持的大小:

Choose biggest one with close aspect ratio(+-5%, for example) If nothing close or resolution too low - just make yor surface(that you draw frames to) be same aspect ratio as biggest supported size and center it in parent view

选择具有接近纵横比(例如+-5%)的最大一个如果没有接近或分辨率太低 - 只需使您的表面(您绘制框架)与最大支持的尺寸相同,并在父视图中居中

Howewer, there are preview sizes & picture sizes. If you wish them to have same aspect ratio - then better choice is to use selected picture size aspect ratio in 1) This way you'll probably have to center your surface in parent view

然而,有预览尺寸和图片尺寸。如果您希望它们具有相同的纵横比 - 那么更好的选择是在 1) 中使用选定的图片大小纵横比,这样您可能必须将您的表面居中在父视图中

Hope that helps)

希望有帮助)