Android摄像头,如何获得最佳尺寸,预览大小,图片大小,视图大小,图像失真。

时间:2021-10-01 21:24:41

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相机的视图混合,使图像失真,从而在使用takepicture方法时获得两者的图像。我检查了一下,发现相机图片设置为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?

框架布局大小最好使用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?

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

 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

例如,在一个安卓平板电脑中,在测量后,这些是三个变量的结果

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

3 个解决方案

#1


11  

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 mode is 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.

最好是使用match_parent或fitXY来获得FrameLayout大小,并且只使用两个变量,预览大小和图片大小?——没有。

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, 1280x720 preview and 2560x1440 picture 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 frame if 1280x720 is what you really need.

即使你的相机支持不同的“宽”尺寸的图片,1280x720的预览和2560x1440的图片可能是最好的搭配,都正好在16:9。通常情况下,小尺寸图片的质量并不比相同尺寸的预览效果好,所以你可以选择保存预览框,如果1280x720是你真正需要的。

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

如果你可以放弃takePicture(),你的应用程序将会更加响应。

#2


1  

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

我认为获得预览尺寸,图片尺寸的最好方法是设置摄像头来支持所有Android设备。这意味着您决定设置的预览尺寸、图片大小与所有设备支持的流行格式相适应,例如1280x720等等。

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

在下面的代码中,是我所使用的。

// 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 size depend on Camera MP. So I think should use default if using Camera.

最佳尺寸取决于相机MP。所以我认为使用相机时应该使用默认设置。

#3


1  

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

如果没有关闭或分辨率过低,选择最大的一个,如果没有关闭或分辨率过低的话,那么就把你的表面(你画的帧)与最大支持的尺寸相同,并把它放在父视图中。

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)

希望帮助)

#1


11  

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 mode is 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.

最好是使用match_parent或fitXY来获得FrameLayout大小,并且只使用两个变量,预览大小和图片大小?——没有。

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, 1280x720 preview and 2560x1440 picture 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 frame if 1280x720 is what you really need.

即使你的相机支持不同的“宽”尺寸的图片,1280x720的预览和2560x1440的图片可能是最好的搭配,都正好在16:9。通常情况下,小尺寸图片的质量并不比相同尺寸的预览效果好,所以你可以选择保存预览框,如果1280x720是你真正需要的。

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

如果你可以放弃takePicture(),你的应用程序将会更加响应。

#2


1  

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

我认为获得预览尺寸,图片尺寸的最好方法是设置摄像头来支持所有Android设备。这意味着您决定设置的预览尺寸、图片大小与所有设备支持的流行格式相适应,例如1280x720等等。

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

在下面的代码中,是我所使用的。

// 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 size depend on Camera MP. So I think should use default if using Camera.

最佳尺寸取决于相机MP。所以我认为使用相机时应该使用默认设置。

#3


1  

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

如果没有关闭或分辨率过低,选择最大的一个,如果没有关闭或分辨率过低的话,那么就把你的表面(你画的帧)与最大支持的尺寸相同,并把它放在父视图中。

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)

希望帮助)