How to implement a camera preview with CameraX

104 Views Asked by At

I'm trying to start a camera preview, by using this tutorial, in Android Studio (Java).

After implementing the code I get a "cannot resolve symbol 'previewView'" error on the following line:

preview.setSurfaceProvider(previewView.getSurfaceProvider());

So, I tried to replace it with:

PreviewView mPreviewView = findViewById(R.id.viewFinder);
preview.setSurfaceProvider(mPreviewView.getSurfaceProvider());

Now, the app compiles successfully but the camera preview is not showing.
I added the camera permission in the manifest, like so

<uses-feature
        android:name="android.hardware.camera"
        android:required="true" />
<uses-permission android:name="android.permission.CAMERA" />

and the dependencies in the build.gradle.kts module

implementation ("androidx.camera:camera-core:1.2.2")
implementation ("androidx.camera:camera-camera2:1.2.2")
implementation ("androidx.camera:camera-lifecycle:1.2.2")
implementation ("androidx.camera:camera-video:1.2.2")

implementation ("androidx.camera:camera-view:1.2.2")
implementation ("androidx.camera:camera-extensions:1.2.2")

I think it's the way I'm referencing the preview view from the layout. What should I do to correct my mistake? Thanks.

0

There are 0 best solutions below