简体   繁体   English

如何在 Java 中使用 CameraX 设置取景器?

[英]How to set viewfinder using CameraX in Java?

I have been working on an app which needed to use CameraX for it's preview stream but it also needs a viewfinder.我一直在开发一个需要使用 CameraX 预览 stream 的应用程序,但它也需要一个取景器。 I have successfully implemented the preview but for the viewfinder part most of the codes that I can find online is in Kotlin and I being a newbie can't seem to effectively convert it to my java based code.我已经成功实现了预览,但是对于取景器部分,我可以在网上找到的大多数代码都在 Kotlin 中,我作为新手似乎无法有效地将其转换为基于 java 的代码。 Any help would be really appreciated.任何帮助将非常感激。

My XML preview code:我的 XML 预览代码:

<androidx.camera.view.PreviewView
    android:id="@+id/previewView"
    android:layout_width="match_parent"
    android:layout_height="675dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/toolbar">

My CameraX preview code:我的 CameraX 预览代码:

 PreviewView mCameraView;
 Camera camera;
 void startCamera() {
    mCameraView = findViewById(R.id.previewView);

    cameraProviderFuture = ProcessCameraProvider.getInstance(this);

    cameraProviderFuture.addListener(() -> {
        try {
            ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
            bindPreview(cameraProvider);
        } catch (ExecutionException | InterruptedException e) {
            // No errors need to be handled for this Future.
            // This should never be reached.
        }
    }, ContextCompat.getMainExecutor(this));
}

 void bindPreview(@NonNull ProcessCameraProvider cameraProvider) {
    Preview preview = new Preview.Builder().
            setTargetResolution(BestSize())
            .build();

    CameraSelector cameraSelector = new CameraSelector.Builder()
            .requireLensFacing(CameraSelector.LENS_FACING_BACK)
            .build();

    preview.setSurfaceProvider(mCameraView.createSurfaceProvider());
    camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview);

    }

    private int degreesToFirebaseRotation(int degrees) {
    switch (degrees) {
        case 0:
            return FirebaseVisionImageMetadata.ROTATION_0;
        case 90:
            return FirebaseVisionImageMetadata.ROTATION_90;
        case 180:
            return FirebaseVisionImageMetadata.ROTATION_180;
        case 270:
            return FirebaseVisionImageMetadata.ROTATION_270;
        default:
            throw new IllegalArgumentException(
                    "Rotation must be 0, 90, 180, or 270.");
    }
}

Sorry for the weird indentation and if the question is stupid.对不起,奇怪的缩进,如果问题很愚蠢。 Thanks in advance.提前致谢。

You also need to bind the preview use case to a lifecycle owner.您还需要将预览用例绑定到生命周期所有者。

preview.setSurfaceProvider(mCameraView.createSurfaceProvider());
cameraProvider.unbindAll();
Camera camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview);
// Do stuff with camera

Although this issue may be long gone, I hope it gives a clue to those who follow up.虽然这个问题可能早已不复存在,但我希望它能给跟进的人提供线索。

First I believe you should have seen this sample code from Google: https://developer.android.com/codelabs/camerax-getting-started where the so-called viewFinder is actually the id of a component in the xml layout file, ie your R.id.previewView . First I believe you should have seen this sample code from Google: https://developer.android.com/codelabs/camerax-getting-started where the so-called viewFinder is actually the id of a component in the xml layout file, ie你的R.id.previewView

Since the Google example uses the kotlin-android-extensions plugin, you can use the component's id directly in kotlin code.由于 Google 示例使用了kotlin-android-extensions插件,因此您可以在 kotlin 代码中直接使用组件的 id。

It's not unusual that you won't find it in the native kotlin code.在本机 kotlin 代码中找不到它并不罕见。 Here is the xml file for the Google example.这是 Google 示例的 xml 文件。

<?xml version="1.0" encoding="utf-8"? >
<androidx.constraintlayout.widget.ConstraintLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">

   <Button
       android:id="@+id/camera_capture_button"
       android:layout_width="100dp"
       android:layout_height="100dp"
       android:layout_marginBottom="50dp"
       android:scaleType="fitCenter"
       android:text="Take Photo"
       app:layout_constraintLeft_toLeftOf="parent"
       app:layout_constraintRight_toRightOf="parent"
       app:layout_constraintBottom_toBottomOf="parent"
       android:elevation="2dp" />

   <androidx.camera.view.PreviewView
       android:id="@+id/viewFinder"
       android:layout_width="match_parent"
       android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

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

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