【问题标题】:Get full screen preview with Android camera2使用 Android camera2 获取全屏预览
【发布时间】:2016-08-19 17:11:34
【问题描述】:

我正在使用新的 camera2 API 构建自定义相机。我的代码基于 Google here 提供的代码示例。

我找不到全屏预览相机的方法。在代码示例中,他们使用比例优化来适应所有屏幕,但只占用屏幕高度的 3/4 左右。

这是我的AutoFitTextureView 代码:

public class AutoFitTextureView extends TextureView {

private int mRatioWidth = 0;
private int mRatioHeight = 0;

public AutoFitTextureView(Context context) {
    this(context, null);
}

public AutoFitTextureView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public AutoFitTextureView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

/**
 * Sets the aspect ratio for this view. The size of the view will be measured based on the ratio
 * calculated from the parameters. Note that the actual sizes of parameters don't matter, that
 * is, calling setAspectRatio(2, 3) and setAspectRatio(4, 6) make the same result.
 *
 * @param width  Relative horizontal size
 * @param height Relative vertical size
 */
public void setAspectRatio(int width, int height) {
    if (width < 0 || height < 0) {
        throw new IllegalArgumentException("Size cannot be negative.");
    }
    mRatioWidth = width;
    mRatioHeight = height;
    requestLayout();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);

    if (0 == mRatioWidth || 0 == mRatioHeight) {
        setMeasuredDimension(width, height);
    } else {
        if (width < height * mRatioWidth / mRatioHeight) {
            setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
        } else {
            setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
        }
    }
}

}

非常感谢您的帮助。

【问题讨论】:

    标签: android camera camera2 android-camera2


    【解决方案1】:

    您应该更改测量的宽度和高度以覆盖全屏,而不是像下面那样适合屏幕。

    发件人:

    if (width < height * mRatioWidth / mRatioHeight) 
    

    if (width > height * mRatioWidth / mRatioHeight)
    

    对我来说效果很好。

    【讨论】:

    • 我认为这行不通。这会扭曲图像(在纵向模式下挤压两侧)
    • 嗨,我按照你的回答很好,预览即将全屏显示,但录制的视频旋转了 180 dg,你能提供解决方案吗,谢谢
    • 优秀的解决方案,看来 - 到目前为止我的测试工作
    • 如果你使用这个解决方案,宽度会被拉长一点
    • 当活动处于横向时这不起作用。
    【解决方案2】:

    这是您问题的解决方案。在这个line 中,纵横比设置为 3/4。我更改了 chooseVideSize 方法来为 MediaRecorder 选择具有高清分辨率的视频大小。

        private static Size chooseVideoSize(Size[] choices) {
            for (Size size : choices) {
                // Note that it will pick only HD video size, you should create more robust solution depending on screen size and available video sizes
                if (1920 == size.getWidth() && 1080 == size.getHeight()) {
                    return size;
                }
            }
            Log.e(TAG, "Couldn't find any suitable video size");
            return choices[choices.length - 1];
        }
    

    然后我更正this method 以根据视频大小纵横比选择预览大小,下面是结果。

    private static Size chooseOptimalSize(Size[] choices, int width, int height, Size aspectRatio) {
        // Collect the supported resolutions that are at least as big as the preview Surface
        List<Size> bigEnough = new ArrayList<Size>();
        int w = aspectRatio.getWidth();
        int h = aspectRatio.getHeight();
        double ratio = (double) h / w;
        for (Size option : choices) {
            double optionRatio = (double) option.getHeight() / option.getWidth();
            if (ratio == optionRatio) {
                bigEnough.add(option);
            }
        }
    
        // Pick the smallest of those, assuming we found any
        if (bigEnough.size() > 0) {
            return Collections.min(bigEnough, new CompareSizesByArea());
        } else {
            Log.e(TAG, "Couldn't find any suitable preview size");
            return choices[1];
        }
    }
    

    希望对你有帮助!

    【讨论】:

    • 成功了!非常感谢@MrOnyszko!当您点击屏幕时,您是否也实现了自动对焦?我的预览图像现在有点模糊。
    • @J.M.使用您的代码,我的屏幕右侧出现了额外的空白,有什么办法解决这个问题吗?如果我选择任何其他纵横比,它们会太模糊或在底部被截断。
    • 能否提供设备信息,例如屏幕尺寸、首选预览尺寸、视频首选预览尺寸?
    • 它在预览中显示水平线和扭曲的预览。可能是什么问题?
    • 如果相机预览质量变差,所以把 max 而不是 min Collections.max(bigEnough, new CompareSizesByArea());
    猜你喜欢
    • 2017-01-19
    • 1970-01-01
    • 2017-06-18
    • 2018-05-31
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 2020-04-06
    相关资源
    最近更新 更多