如何使用 CameraX 设置正确的纵横比?
How to set a correct aspect ratio using CameraX?
我正在使用 CameraX codelab,但即使使用 setTargetAspectRatio
和 setTargetResolution
方法,我在预览时也得到了错误的宽高比。
private fun startCamera() {
// Create configuration object for the viewfinder use case
val previewConfig = PreviewConfig.Builder().apply {
setTargetAspectRatio(Rational(1, 1))
setTargetResolution(Size(640, 640))
}.build()
...
并且布局使用代码实验室中提供的硬编码尺寸。
<TextureView
android:id="@+id/view_finder"
android:layout_width="640px"
android:layout_height="640px"
...
如果图书馆有CameraTextureView
和一个属性android:scaleType
(类似于现有的ImageView
)来调整预览就好了尺寸。
您可以像这样设置 TextureView
和 Matrix
的纵横比:
Matrix txform = new Matrix();
textureView.getTransform(txform);
txform.setScale((float) = videoWidth / viewWidth, (float) videoHeight / viewHeight);// aspect ratio
textureView.setTransform(txform); // apply matrix
你试过了吗?
val metrics = DisplayMetrics().also { viewFinder.display.getRealMetrics(it) }
val screenSize = Size(metrics.widthPixels, metrics.heightPixels)
val screenAspectRatio = Rational(metrics.widthPixels, metrics.heightPixels)
val viewFinderConfig = PreviewConfig.Builder().apply {
//...
setTargetResolution(screenSize)
setTargetAspectRatio(screenAspectRatio)
setTargetRotation(viewFinder.display.rotation)
}.build()
val preview = AutoFitPreviewBuilder.build(viewFinderConfig, viewFinder)
您可以在这里找到 AutoFitPreviewBuilder:
https://gist.github.com/yevhenRoman/90681822adef43350844464be95d23f1
我建议您使用 dp 或 constaraints 为您的 TextureView 设置宽度和高度。
让我知道它是否适合您,谢谢!
类似问题有解决办法。确定分辨率后,不要使用 setTargetAspectRatio
功能;相反,您应该只使用 setTargetResolution
。那么它应该是一样的。
根据官方Androiddocumentation:
“不允许在同一用例上同时设置目标纵横比和目标分辨率。”
尽管如果您尝试这样做编译器不会抛出错误,但可能会出现意外结果。
我正在使用 CameraX codelab,但即使使用 setTargetAspectRatio
和 setTargetResolution
方法,我在预览时也得到了错误的宽高比。
private fun startCamera() {
// Create configuration object for the viewfinder use case
val previewConfig = PreviewConfig.Builder().apply {
setTargetAspectRatio(Rational(1, 1))
setTargetResolution(Size(640, 640))
}.build()
...
并且布局使用代码实验室中提供的硬编码尺寸。
<TextureView
android:id="@+id/view_finder"
android:layout_width="640px"
android:layout_height="640px"
...
如果图书馆有CameraTextureView
和一个属性android:scaleType
(类似于现有的ImageView
)来调整预览就好了尺寸。
您可以像这样设置 TextureView
和 Matrix
的纵横比:
Matrix txform = new Matrix();
textureView.getTransform(txform);
txform.setScale((float) = videoWidth / viewWidth, (float) videoHeight / viewHeight);// aspect ratio
textureView.setTransform(txform); // apply matrix
你试过了吗?
val metrics = DisplayMetrics().also { viewFinder.display.getRealMetrics(it) }
val screenSize = Size(metrics.widthPixels, metrics.heightPixels)
val screenAspectRatio = Rational(metrics.widthPixels, metrics.heightPixels)
val viewFinderConfig = PreviewConfig.Builder().apply {
//...
setTargetResolution(screenSize)
setTargetAspectRatio(screenAspectRatio)
setTargetRotation(viewFinder.display.rotation)
}.build()
val preview = AutoFitPreviewBuilder.build(viewFinderConfig, viewFinder)
您可以在这里找到 AutoFitPreviewBuilder: https://gist.github.com/yevhenRoman/90681822adef43350844464be95d23f1
我建议您使用 dp 或 constaraints 为您的 TextureView 设置宽度和高度。 让我知道它是否适合您,谢谢!
setTargetAspectRatio
功能;相反,您应该只使用 setTargetResolution
。那么它应该是一样的。
根据官方Androiddocumentation: “不允许在同一用例上同时设置目标纵横比和目标分辨率。”
尽管如果您尝试这样做编译器不会抛出错误,但可能会出现意外结果。