简体   繁体   English

使用SCALER_CROP_REGION裁剪时,Camera2预览会拉伸

[英]Camera2 preview stretched when cropping with SCALER_CROP_REGION

I want to display a preview in fullscreen using camera2 api. 我想使用camera2 API 全屏显示预览。 The camera size I selected is 4160x3120 (16:9) and my screen is 1080x1920 (9:16). 我选择的相机尺寸为4160x3120 (16:9),屏幕为1080x1920 (9:16)。 Therefore if I want the preview to be right scaled I have to crop the camera output : 因此,如果我想对预览进行正确缩放,则必须裁剪相机输出:

Rect zoomCrop = new Rect(0, 0, 1755, 3120);
captureRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoomCrop);

The cropped preview should be of size 1755x3120 which has a ratio of 9:16 as well. 裁剪的预览的尺寸应为1755x3120 ,并且比例为9:16。 Still the preview is streched. 预览仍然很努力。

Question : Am I using SCALER_CROP_REGION correctly ? 问题:我是否正确使用SCALER_CROP_REGION If not, how am I supposed to use it ? 如果没有,我应该如何使用它?

Thanks in advance 提前致谢

I've been struggling with the same sort of problem, as I want to implement a camera preview stream into a square TextureView. 我一直在努力解决同样的问题,因为我想将摄像机预览流实现为方形 TextureView。

Working with the SCALER_CROP_REGION seemed like the obvious place to resolve issues with incompatible aspect ratios, but I don't think that operation will ever actually change the aspect ratio of the result, hence it doesn't prevent stretching/warping behaviour. 使用SCALER_CROP_REGION似乎是解决纵横比不兼容的问题的明显场所,但是我认为该操作实际上不会改变结果的纵横比,因此它不能防止拉伸/翘曲行为。 ( the docs talk about this ) 文档对此进行了讨论

I've made progress by implementing a similar transform to the one in the Camera2Basic example project - see the function configureTransform in Camera2BasicFragment ). 见函数-我已经实现了类似的变换到一个在Camera2Basic示例项目取得了进展configureTransformCamera2BasicFragment )。

Using this sort of view-level scaling matrix, you can manage the scaling/cropping of the camera preview without altering the size of your view. 使用这种视图级别的缩放矩阵,您可以在不更改视图大小的情况下管理摄像机预览的缩放/裁剪。

It sounds like you are getting a landscape image and displaying it in a portrait view - this means that you will have 'pillar box' lines above and below the image if you want to avoid distortion. 听起来好像您正在获取风景图像并将其以纵向视图显示-这意味着,如果要避免变形,将在图像的上方和下方具有“支柱框”线。

If you did want to fill the screen, and not have distortion or the pillar box lines then you would have to accept that part of your image will not be visible, as its outside the shape of the viewing area. 如果您确实想填满屏幕,并且没有扭曲或柱状框线,那么您将不得不接受那部分图像将不可见,因为它超出了查看区域的形状。

SCALER_CROP_REGION is tricky to understand without diagrams - I found the best explanation to be in the Andoir source documentation here: https://source.android.com/devices/camera/camera3_crop_reprocess 没有图表,SCALER_CROP_REGION很难理解-我在以下的Andoir源文档中找到了最好的解释: https ://source.android.com/devices/camera/camera3_crop_reprocess

One diagram reproduced from that helps as an example here - the reason for highlight this is to show how the co-ordinates are being used. 从中复制出的一个图表在此处作为示例-突出显示该图表的原因是为了显示如何使用坐标。 If you look at your crop region it will be biased towards the top left as you start at (0,0), which I am not sure is what you actually want. 如果您查看自己的作物区域,则从(0,0)开始时,它会偏向左上方,我不确定这是您真正想要的。

在此处输入图片说明

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

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