Camera2全屏预览和图像捕获 [英] Camera2 full screen preview and image capture

查看:70
本文介绍了Camera2全屏预览和图像捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Camera2使用示例代码,我想知道如何制作全屏预览和捕获的图像?这个这样的问题似乎可以在视频模式下解决,但是我不能查找图像捕获的任何解决方案.样本片段的底部有一个蓝色区域,并且状态为bas.我想同时隐藏这两者,并使用整个屏幕显示预览,并以全屏尺寸捕获图像.

I am using the sample code for Camera2, and I was wondering how I can make the preview and captured image in full screen? This SO question seems to solve it in video mode, but I can't find any solution for image captures. The sample fragment has a blue area at the bottom and also has the status bas. I want to hide both of these and use the entire screen to show the preview, and also capture the image in full screen size.

推荐答案

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

Eddy is correct about the aspect ratio.The camera sensor is 4:3. The screen is usually 16:9. The sample code choose to show the entire camera preview, so part of the screen is not filled. You need to stretch to fill the whole screen, however in that case the images captured include areas not shown in the preview.

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

To view it in full screen, in the AutoFitTextureView, change the onMeasure method to this:

    @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);
            }
        }
    }

更新:正如Eddy所指出的那样,这将在全屏幕上显示摄像机预览的左上部分,而使底部和右侧不在视图范围内,结果是图像偏离中心.在我的特殊情况下,对象需要水平居中,因此我修改了变换矩阵以使对象水平居中.这是代码:

Update: As pointed out by Eddy, this will show the upper left part of the camera preview on the full screen, leaving the bottom and right side out of the view, the result is that the image is off center. In my particular case the subject needs to be centered horizontally, so I modified the transform matrix to center the subject horizontally. Here is the code:

// 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);

这篇关于Camera2全屏预览和图像捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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