Camera2 全屏预览和图像捕捉

Kæm*_*ker 4 camera android

我正在使用Camera2的示例代码,我想知道如何以全屏方式进行预览和捕获的图像?这个SO question 似乎在视频模式下解决了它,但我找不到任何图像捕获的解决方案。样本片段底部有一个蓝色区域,也有状态 bas。我想隐藏这两个并使用整个屏幕来显示预览,并以全屏尺寸捕获图像。

Ray*_*Ray 6

Eddy 关于纵横比是正确的。相机传感器是 4:3。屏幕通常为 16:9。示例代码选择显示整个相机预览,因此部分屏幕未填充。您需要拉伸以填满整个屏幕,但是在这种情况下,捕获的图像包含预览中未显示的区域。

要全屏查看,在 AutoFitTextureView 中,将 onMeasure 方法更改为:

    @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);
                setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
            } else {
                //setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
                setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

更新:正如 Eddy 所指出的,这将在全屏上显示相机预览的左上部分,将底部和右侧留在视图之外,结果是图像偏离中心。在我的特殊情况下,主体需要水平居中,因此我修改了变换矩阵以将主体水平居中。这是代码:

// adjust the x shift because we are not showing the whole camera sensor on the screen, need to center the capture area
    int screenWidth = Resources.getSystem().getDisplayMetrics().widthPixels;
    Log.d(TAG, "screen width " + screenWidth);
    int xShift = (viewWidth - screenWidth)/2;
    matrix.setTranslate(-xShift, 0);

        mTextureView.setTransform(matrix);
Run Code Online (Sandbox Code Playgroud)

  • 请注意,这很可能不会使预览在屏幕上居中 - 它会在右侧和底部切断(尽管在这种情况下我必须准确检查包含父级的扩展方式)。这对您来说可能重要也可能无关紧要。 (2认同)