简体   繁体   English

Camera2全屏预览和图像捕获

[英]Camera2 full screen preview and image capture

I am using the sample code for Camera2, and I was wondering how I can make the preview and captured image in full screen? 我正在使用Camera2的示例代码 ,并且想知道如何才能全屏显示预览和捕获的图像? 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. 样本片段的底部有一个蓝色区域,并且状态为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 is correct about the aspect ratio.The camera sensor is 4:3. 涡流是正确的长宽比。摄像头传感器为4:3。 The screen is usually 16:9. 屏幕通常为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. 您需要拉伸以填满整个屏幕,但是在这种情况下,捕获的图像包括预览中未显示的区域。

To view it in full screen, in the AutoFitTextureView, change the onMeasure method to this: 若要全屏查看,请在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);
            }
        }
    }

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. 更新:正如Eddy所指出的,这将在全屏幕上显示相机预览的左上部分,而使底部和右侧不在视图范围内,结果是图像偏离中心。 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);

Note that the aspect ratio of the camera sensor and the aspect ratio of your device screen most often do not match - sensors are generally 4:3 ratio and screens are generally around 16:9. 请注意,摄像头传感器的纵横比通常与设备屏幕的纵横比不匹配-传感器通常为4:3,屏幕通常在16:9左右。 Though there is wide variation in both. 虽然两者都有很大的差异。

Since the aspect ratios don't match, you'll need to decide if you're going to have black bars, or zoom the camera preview so that it fills the screen (but some part of the image is not visible). 由于宽高比不匹配,因此您需要确定是否要使用黑条,或缩放相机预览以使其充满屏幕(但是图像的某些部分不可见)。

In any case, to have the preview be full-screen, you just need to have the TextureView (or SurfaceView) be the entire layout. 无论如何,要使预览全屏显示,您只需要使TextureView(或SurfaceView)成为整个布局即可。 So basically, edit this layout file: fragment_camera2_basic.xml , remove all the other Views, and remove references to them in the source code. 因此,基本上,编辑以下布局文件: fragment_camera2_basic.xml ,删除所有其他视图,并在源代码中删除对其的引用。 Or change them to be on top of the TextureView, not next to - this is all standard Android UI layout stuff. 或将它们更改为TextureView的顶部,而不是旁边的-这是所有标准的Android UI布局。 And you'll need to make your Activity be full-screen . 而且,您需要将“活动”设为全屏

By default, AutoFillTextureView will try to maintain aspect ratio, so it'll produce black bars. 默认情况下,AutoFillTextureView将尝试保持宽高比,因此会产生黑条。 If you want no black bars, then you'll have to change AutoFillTextureView, which is not straightforward, and I won't get into it here. 如果您不希望出现黑条,则必须更改AutoFillTextureView,这不是很简单,我在这里不再赘述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM