相机显示屏/ preVIEW在全屏不保持高宽比 - 图像歪斜,拉伸以适应屏幕上的 [英] Camera display / preview in full screen does not maintain aspect ratio - image is skewed, stretched in order to fit on the screen

查看:375
本文介绍了相机显示屏/ preVIEW在全屏不保持高宽比 - 图像歪斜,拉伸以适应屏幕上的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个小的应用程序以全屏幕显示摄像机preVIEW。我使用的摄像头API这一点。

I have developed a small application for displaying camera preview in full screen. I'm using Camera API for this.

这是该活动的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <!-- This is the container for the camera preview screen -->
    <FrameLayout android:id="@+id/camera_preview"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"/>
</LinearLayout>

当设备处于纵向,显示器垂直缩放,以匹配设备屏幕的高度 - 这样的高宽比是不一样的,从本地相机1。这是2图像这也解释了好我在说什么:

When device is in portrait, the display is vertically scaled in order to match the height of device screen - so the aspect ratio is not the same as the one from native camera. These are 2 images which explains better what I'm saying:

第一图像由与天然相机设备。 第二个图像是用我的应用程序,带摄像头的全屏幕 - 图像被扭曲,拉伸,以适应屏幕上的

The first image is made with native camera device. The second image is made with my app, with camera in full screen - the image is skewed, stretched in order to fit on the screen.

我需要相机preVIEW是全屏幕,不管()由 getSupported previewSizes给出的preVIEW大小 mehod不变形。 有没有办法做到这一点?有没有一种方法,以保持合适的宽高比时相机preVIEW处于全屏?我预期这将自动通过操作系统完成 - 裁剪图像,以匹配所要求的分辨率,同时保持高宽比,但这种情况不会发生

I need the camera preview to be full screen, regardless of the preview size given by getSupportedPreviewSizes() mehod and without distortion. Is there a way to accomplish this? Is there a way to maintain proper aspect ratio when camera preview is in full screen? I expected this to be done automatically by OS - cropping the image to match the requested resolution while maintaining aspect ratio, but this does not happen.

我试图让 SurfaceView 比显示(以下这个问题更大:<一href="http://stackoverflow.com/questions/11321251/fitting-a-camera-$p$pview-to-a-surfaceview-larger-than-the-display">Fitting相机preVIEW到SurfaceView比显示大),但并不确定。在我的情况,因为我拍摄的快照(6帧/秒),这些都不是在屏幕上看到哪些用户(框架包含所有的摄像头preVIEW即使不是全部是在屏幕上可见)。

I've tried to make the SurfaceView larger than the display (following this question: Fitting a camera preview to a SurfaceView larger than the display), but is not ok in my case since I'm capture snapshots (6 frames/second) and those are not what user sees on the screen (the frame contains all camera preview even if not all of it is visible on the screen).

我张贴在这里:<一href="https://www.dropbox.com/s/3d52xt8kazynsae/CameraFullScreen.7z?v=0mcn">https://www.dropbox.com/s/3d52xt8kazynsae/CameraFullScreen.7z?v=0mcn我做了整个项目。

I posted here: https://www.dropbox.com/s/3d52xt8kazynsae/CameraFullScreen.7z?v=0mcn the entire project I've made.

任何想法/解决方案不仅仅是对我很重要。非常感谢。

Any idea/solution is more than important to me. Thanks a lot.

=============================================== =========================

========================================================================

更新由于ss1271回答:

我会分析一点点,你写了上述决议的三星Galaxy王牌II

I'll analyze a little the resolutions you wrote above for Samsung Galaxy Ace II.

我。屏幕分辨率:480×800 - 纵横比为3:5 = 0.6

I. Screen resolution: 480x800 - aspect ratio 3:5 = 0,6

二。 getSupported previewSizes - 我几乎可以肯定,这些值是从后摄像头。下面是各方面比这些决议:

II. getSupportedPreviewSizes - I'm almost sure that these values are from back camera. Below are the aspects ratio for these resolutions:

   2560x1920   - 0,75

   2560x1536   - 0,60

   2048x1536   - 0,75

   2048x1232   - 0,60

   960x720     - 0,75

   640x480     - 0,75

所以,你的方法将返回一个尺寸对应的 2560x1536 2048x1232 - 这些具有相同的方面比屏幕分辨率,并使用这些值不会扭曲的画面。 我的问题是,我不能用这么大的决议,因为我捕捉6张/秒,这些需要被保存在一个较低的分辨率。

So, your method will return a Size corresponding to 2560x1536 or to 2048x1232 - these having the same aspect ratio as screen resolution and using these values will not distort the picture. The problem for me is that I cannot use so big resolutions since I capture 6 frames/second and these needs to be saved at a lower resolution.

我会在下面$一些结果p $ psent从三星S2 设备:

I'll present below some results from a Samsung S2 device:

我。屏幕分辨率:480 * 800 - 纵横比为3:5 = 0.6

I. Screen resolution: 480 x 800 - aspect ratio 3:5 = 0,6

二。返回相机
一个)。 getSupported previewSizes

II. Back camera
a). getSupportedPreviewSizes:

800 / 480   - 480/800 = 0,60
800 / 450   - 450/800 = 0,56
720 / 480   - 0,66 
640 / 480   - 0,75
352 / 288   - 0,81
320 / 240   - 0,75
176 / 144   - 0,81

B)。本机摄像头分辨率:

b). Native camera resolutions:

3264 / 2448  - 0,75  - not full screen
3264 / 1968  - 0,60  - FULL SCREEN (since has the same aspect ratio as device screen)
2048 / 1536  - 0,75  - not full screen
2048 / 1232  - 0,60  - FULL SCREEN (same aspect ratio as device screen)
800 / 480    - 0,60  - FULL SCREEN (same aspect ratio as device screen)
640 / 480    - 0, 75 - not full screen

三。前置摄像头
一个)。 getSupported previewSizes

III. Front camera
a). getSupportedPreviewSizes:

640 / 480   - 0,75
352 / 288   - 0,81
320 / 240   - 0,75
176 / 144   - 0,81

B)。原生相机是不是全屏,我不能选择的分辨率 - 禁用该选项

b). Native camera is not in full screen and I cannot choose a resolution - the option is disabled.

有关S2,背部摄像头,我很奇怪为什么 getSupported previewSizes()方法,机相机或不显示的那些不返回相同的决议机摄像头在图象的大小?我不知道为什么我没有像1968分之3264选项,一千二百三十二分之二千零四十八给出 getSupported previewSizes()的方法? :

For S2, back camera, I'm wonder why getSupportedPreviewSizes() method do not return the same resolutions as Native camera does or the ones displayed by native camera are the picture sizes ? I'm wonder why I do not have options like 3264 / 1968, 2048 / 1232 given by getSupportedPreviewSizes() method ? :

推荐答案

在简单地说,你可以做一个摄像头preVIEW全屏但是你需要找出支持$ P $之间的适当大小的PVIEW大小自己,只有当一个定制的摄像头preVIEW是你想要的。

In a nutshell, you can do a camera preview in full screen however you'll need to find out the appropriate sizes among the supported preview sizes by yourself, only if a customized camera preview is what you want.

对于你的问题,按照 Android开发者 - 摄像机

如果你想设置一个特定的大小为您的相机preVIEW,设置此   在 surfaceChanged()方法如上评论指出。什么时候   设置preVIEW大小,您必须从使用价值   getSupported previewSizes <$ C C $>()。 不要在设置任意值   设置previewSize() 方式

If you want to set a specific size for your camera preview, set this in the surfaceChanged() method as noted in the comments above. When setting preview size, you must use values from getSupportedPreviewSizes(). Do not set arbitrary values in the setPreviewSize() method.

这似乎不能手动传递你想以外的尺寸提供了 getSupported previewSizes大小()。通过仔细检查您的手机摄像头所支持的尺寸,你会发现大小的比例支持可能不完全一样,屏幕的比例。

It seemed you can't manually pass the size you want other than the sizes provides by getSupportedPreviewSizes(). With a closer examine the sizes supported by your phones camera, you'll find the ratio of sizes supported might not exactly the same as your screen's ratio.

例如,三星Galaxy王牌II具有480×800分辨率的屏幕,通过读 getSupported previewSizes大小返回(),它的摄像头支持:

For example, Samsung Galaxy Ace II has 480x800 resolution screen, by reading the Size returned from getSupportedPreviewSizes(), its camera supports:

2560x1920

2560x1920

2560x1536

2560x1536

2048×1536

2048x1536

2048x1232

2048x1232

960x720

640×480

如果你想正确地(无拉伸)显示相机preVIEW全屏,你需要计算,比较和应用适当的比例在这些支持preVIEW大小。

and if you want to display your camera preview in full screen correctly (without stretch), you'll need to calculate, compare and apply the suitable ratio in these supported preview sizes.

找到一个合适的preVIEW大小的实现并不复杂的东西。这样做的一个常见的​​方法是这样的:

Implementation of finding a proper preview size is not that complicated stuff. A common method for doing this would be something like this:

    /**
     * Calculate the optimal size of camera preview
     * @param sizes
     * @param w
     * @param h
     * @return
     */
    private Size getOptimalSize(List<Size> sizes, int w, int h) {

        final double ASPECT_TOLERANCE = 0.2;        
        double targetRatio = (double) w / h;         
        if (sizes == null)             
            return null;          
        Size optimalSize = null;         
        double minDiff = Double.MAX_VALUE;          
        int targetHeight = h;          
        // Try to find an size match aspect ratio and size         
        for (Size size : sizes) 
        {             
//          Log.d("CameraActivity", "Checking size " + size.width + "w " + size.height + "h");            
            double ratio = (double) size.width / size.height;            
            if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)                
                continue;             
            if (Math.abs(size.height - targetHeight) < minDiff) 
            {                 
                optimalSize = size;                 
                minDiff = Math.abs(size.height - targetHeight);             
            }         
        }          
        // Cannot find the one match the aspect ratio, ignore the requirement     

        if (optimalSize == null)
        {
            minDiff = Double.MAX_VALUE;             
            for (Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff)
                {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight); 
                }
            }
        }

        SharedPreferences previewSizePref;
        if (cameraId == Camera.CameraInfo.CAMERA_FACING_BACK) {
            previewSizePref = getSharedPreferences("PREVIEW_PREF",MODE_PRIVATE);
        } else {
            previewSizePref = getSharedPreferences("FRONT_PREVIEW_PREF",MODE_PRIVATE);
        }

        SharedPreferences.Editor prefEditor = previewSizePref.edit();
        prefEditor.putInt("width", optimalSize.width);
        prefEditor.putInt("height", optimalSize.height);

        prefEditor.commit();

//      Log.d("CameraActivity", "Using size: " + optimalSize.width + "w " + optimalSize.height + "h");            
        return optimalSize;     
    }

和你也可以做类似的找出适合的相机尺寸(输出图像尺寸)。

And you can also do the similar to find out the suitable camera sizes (the output picture size).

注:我发现了code以上来自互联网的原始版本和我做了一些修改/优化我自己的目的

Note: I found the original version of the code above from Internet and I did some modification/optimisation for my own purpose.

请让我知道,如果这对你的作品。

Please let me know if this works for you.

这篇关于相机显示屏/ preVIEW在全屏不保持高宽比 - 图像歪斜,拉伸以适应屏幕上的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆