简体   繁体   English

在Android上拉伸相机预览

[英]Stretched camera preview on Android

I have a problem of proper implementation of camera preview. 我有正确实施相机预览的问题。 I looked over many answers provided here on StackOverflow and many tutorials. 我查看了StackOverflow此处提供的许多答案以及许多教程。 I created my own project to have it properly done. 我创建了自己的项目以正确完成它。

https://bitbucket.org/rdkit/cameratutorial https://bitbucket.org/rdkit/cameratutorial

I thought it works ok, but it turned out after checking this code on Sony Xperia SP that unfortunately it doesn't. 我认为它可以正常运行,但不幸的是,在Sony Xperia SP上检查了此代码后,结果却没有。

Can anyone look at this code and help me? 谁能看一下这段代码对我有帮助?

Screenshot from Moto G Moto G的屏幕截图

摩托G

and Sony Xperia SP 和Sony Xperia SP 索尼Xperia SP

Camera PreviewSize is very tricky to set. 设置Camera PreviewSize非常棘手。

On some phones, the top status bar's height will not be taken into consideration, but other implementation do will take that height into consideration. 在某些电话上,不会考虑顶部状态栏的高度,但其他实现确实会考虑该高度。 And also the bottom virtual button area. 以及底部的虚拟按钮区域。

For eg, if the total screen's width = 1080 and height = 1920, then when the status bar's height and virtual button area's height are not be taken into consideration, if you set PreviewSize a ratio with 16:9, it will be look good. 例如,如果总屏幕的宽度= 1080,高度= 1920,则当不考虑状态栏的高度和虚拟按钮区域的高度时,如果将PreviewSize的比率设置为16:9,则效果会很好。 But! 但! When the status bar's height and the virtaul button ares's height are taken into consideration, the available screen height will never get 1920, then the ratio in your screen will be different to the PreviewSize, and it will be stretched. 当考虑状态栏的高度和虚拟按钮的高度时,可用的屏幕高度将永远不会达到1920,因此屏幕中的比例将与PreviewSize不同,并且将被拉伸。

To solve the problem, you should implement your own getOptimalPreviewSize() method and choose the proper preview ratio according to your available screen height. 要解决此问题,您应该实现自己的getOptimalPreviewSize()方法,并根据可用的屏幕高度选择合适的预览比例。

Finally I found a solution by myself. 最终,我自己找到了解决方案。 I did not stop the preview before making changes. 进行更改之前,我没有停止预览。

As comments say in method 正如评论中所说的

surfaceChanged()

If your preview can change or rotate, take care of those events here. 如果您的预览可以更改或旋转,请在此处处理这些事件。 Make sure to stop the preview before resizing or reformatting it. 在调整大小或重新设置格式之前,请确保停止预览。

So I needed to add 所以我需要添加

try {
    mCamera.stopPreview();
} catch (Exception e) {
    // ignore: tried to stop a non-existent preview
}

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

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