本文介绍了正确的纵横比在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发的是画在相机上 SurfaceView 的应用程序。在开始的时候,我发现有意见的麻烦。他们没有正确地显示。所以我用下面的方法来纠正宽高比问题:

I'm developing an application that is painting the camera on a SurfaceView. At the beginning I found trouble with the views. They were not correctly shown. So I used the following method to correct the aspect ratio problem:

注:此方法在几个地方发现的一个修正为Android相机宽高比

NOTE: This method is found in several places as the one to correct aspect ratio for Android camera.

private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
    final double ASPECT_TOLERANCE = 0.1;
    double targetRatio;

    // Check wheter is portrait or landscape
    if (orientation == 1)
        targetRatio = (double) h / w;
    else
        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) {
        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);
            }
        }
    }
    return optimalSize;
}

这工作完美,我的相机被正确地显示。但我发现在较新的设备,如LG G3的问题。它有一项决议,该方法认为是最appropiate之一,但它是显示图像是邮筒在纵向模式,像下面的图片显示:

This worked perfect, and my camera was shown correctly. But I found problems in newer devices, such as the LG G3. It had a resolution that this method considered that was the most appropiate one, but it was showing the image with pillar boxes on portrait mode, like the image show below:

这是怎么回事?如何解决在纵向模式下,该邮筒?

Why is this happening? How can I solve this pillar box on portrait mode?

推荐答案

除非有在code的一个错误,你所得到的最好的结果可能。

Unless there is a bug in that code, you are getting the 'best' result possible.

手机的摄像头的纵横比不保证匹配的手机的屏幕。

The aspect ratio of the phone's camera is not guaranteed to match that of the phone's screen.

这仅仅是一个决定什么标准是最好的结果的问题。这code是谷歌的建议,以决定什么是用于显示手机屏幕上最好的相机分辨率。

It is just a matter of deciding what the criteria are for the best result. That code is Google's suggestion for deciding what is the best camera resolution for displaying on the phone's screen.

您可能无法与谷歌达成一致。在这种情况下,你需要编写自己的算法来决定什么是最好的相机的分辨率。你可能优化宽高比,或为沿宽度或高度的分辨率。您可以尝试以适应屏幕,或者你可以裁剪图像的位来填满整个屏幕。这是给你的。

You might not agree with Google. In that case you will need to write your own algorithm for deciding what is the best camera resolution. You might optimize for aspect-ratio, or for the resolution along the width or height. You may try to fit the screen, or you could clip bits of the image to fill the entire screen. It's up to you.

注:code您发布试图找到一个摄像头分辨率是屏幕的纵横比的 10%以内和的有相匹配的屏幕是最接近的高度。
如果有10%的范围内没有摄像机分辨率从屏幕宽高比,然后它选择具有相匹配的画面是最接近的高度照相机分辨率。在这种情况下,你就必须围绕支柱箱显著黑条。

Note: the code you posted tries to find a camera resolution that is within 10% of the screen's aspect-ratio and has a height that matches the screen's the closest.If there is no camera resolution within 10% from the screen aspect-ratio, then it picks the camera resolution that has a height that matches the screen's the closest. In this case you would have significant black bars around the pillar box.

这篇关于正确的纵横比在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:10