简体   繁体   English

如何在 cameraX 预览中设置预览大小?

[英]How to set preview size on cameraX preview?

I am trying to create a preview in portrait mode and I want the preview to be as wide as the screen and wrap content vertically.我正在尝试以纵向模式创建预览,并且我希望预览与屏幕一样宽并垂直包装内容。 However, when I do that I always get the preview size 1200x 1200 and the preview is stretched horizontally to fill the screen.但是,当我这样做时,我总是得到1200x 1200的预览尺寸,并且预览被水平拉伸以填满屏幕。

The image captured is the correct size though ( 1024x768 .捕获的图像尺寸正确( 1024x768 .

In other words I want the preview size to be same as image captured.换句话说,我希望预览大小与捕获的图像相同。

Any help will be appreciated.任何帮助将不胜感激。

preview = new Preview.Builder()
            .setTargetAspectRatioCustom(new Rational(3, 4))
            .build();
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.molescope.CameraXActivity">

    <androidx.camera.view.PreviewView
        android:id="@+id/preview_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <ImageButton
        android:id="@+id/capture_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginBottom="@dimen/margin_vertical"/>


</androidx.constraintlayout.widget.ConstraintLayout>
    def camerax_version = "1.0.0-beta03"
    implementation "androidx.camera:camera-core:${camerax_version}"
    implementation "androidx.camera:camera-camera2:${camerax_version}"
    implementation "androidx.camera:camera-lifecycle:${camerax_version}"
    implementation "androidx.camera:camera-view:1.0.0-alpha10"
    implementation "androidx.camera:camera-extensions:1.0.0-alpha10"

If you want the preview to fit the width of the screen, you might want to test setting the scale type of PreviewView to FIT_CENTER (or FIT_START or FIT_END , depending on how you want to layout the preview on the screen).如果您希望预览适合屏幕的宽度,您可能需要测试将FIT_CENTER PreviewViewFIT_STARTFIT_END ,具体取决于您希望如何在屏幕上布局预览)。

You can either set the scale type programmatically:您可以通过编程方式设置比例类型:

previewView.setScaleType(PreviewView.ScaleType.FIT_CENTER);

Or in the layout file:或者在布局文件中:

<androidx.camera.view.PreviewView
   ...
   android:scaleType="fitCenter" />

FIT_* fits either the width or height of the preview to match the width or height of PreviewView 's container (or both, if the container and the preview resolution have the same aspect ratio), which dimension is matched depends on a couple of things, including the preview resolution, the natural orientation of the device and the display's current orientation. FIT_*适合预览的宽度或高度以匹配PreviewView容器的宽度或高度(或两者,如果容器和预览分辨率具有相同的纵横比),匹配的尺寸取决于几件事,包括预览分辨率、设备的自然方向和显示器的当前方向。 As a result, this approach might not work in all situations, at times, the preview will fill the width of its container, at others, it will fill the height of the container.因此,这种方法可能不适用于所有情况,有时,预览将填充其容器的宽度,有时,它将填充容器的高度。

If your concern is the aspect ratio of the displayed preview, another way to solve your problem is to set PreviewView 's aspect ratio (in your case, I assume it would be 4x3), and set PreviewView to use a FIT_CENTER scale type.如果您关心的是显示预览的纵横比,解决问题的另一种方法是设置PreviewView的纵横比(在您的情况下,我假设它是 4x3),并将PreviewView设置为使用FIT_CENTER比例类型。

About your question of In other words I want the preview size to be same as image captured.关于您的问题In other words I want the preview size to be same as image captured. This post https://groups.google.com/a/android.com/g/camerax-developers/c/6GNMfHIDeAM seems to be quite helpful.这篇文章https://groups.google.com/a/android.com/g/camerax-developers/c/6GNMfHIDeAM似乎很有帮助。 It says:它说:

You have a try on the ViewPort feature to produce the same crop rect in a WYSIWYG.您可以尝试使用 ViewPort 功能以所见即所得的方式生成相同的裁剪矩形。 While using the CameraXBasic sample code, you can replace the bindToLifeCycle with在使用 CameraXBasic 示例代码时,您可以将 bindToLifeCycle 替换为

val viewPort: ViewPort = ViewPort.Builder(
Rational(
viewFinder.width,
viewFinder.height
),
viewFinder.display.rotation
).setScaleType(ViewPort.FILL_CENTER).build()

val useCaseGroupBuilder: UseCaseGroup.Builder = UseCaseGroup.Builder().setViewPort(
viewPort
)

useCaseGroupBuilder.addUseCase(preview!!)
useCaseGroupBuilder.addUseCase(imageCapture!!)
useCaseGroupBuilder.addUseCase(imageAnalyzer!!)

camera = cameraProvider.bindToLifecycle(
this, cameraSelector,
useCaseGroupBuilder.build()
)

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

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