androidandroidxandroid-camera2camera-view

Cannot access '<init>': it is package-private in 'ViewfinderSurfaceRequest'


Im trying to implement CameraViewFinder with Camera2 API in Kotlin/Android. But I got an error like this ,

Cannot access '': it is package-private in 'ViewfinderSurfaceRequest'.

import androidx.camera.viewfinder.ViewfinderSurfaceRequest
import androidx.camera.viewfinder.CameraViewfinder

val viewfinderSurfaceRequest =
             ViewfinderSurfaceRequest(
                Size(640, 480),
                characteristics.get(CameraCharacteristics.LENS_FACING)!!,
                characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION)!!,
                CameraViewfinder.ImplementationMode.PERFORMANCE
            )
        val surfaceListenableFuture =
            cameraViewFinder.requestSurfaceAsync(viewfinderSurfaceRequest)

        Futures.addCallback(surfaceListenableFuture, object : FutureCallback<Surface> {
            override fun onSuccess(result: Surface?) {
                /* create a CaptureSession using this surface as usual */
            }

            override fun onFailure(t: Throwable) { /* something went wrong */
            }
        }, ContextCompat.getMainExecutor(cameraViewFinder.context))

In my build.gradle:

 implementation "androidx.camera:camera-camera2:1.3.0-rc01"
 implementation "androidx.camera:camera-lifecycle:1.3.0-rc01"
 implementation "androidx.camera:camera-view:1.3.0-rc01"
 implementation "androidx.camera:camera-viewfinder:1.3.0-beta02"

When I navigate to source code of ViewfinderSurfaceRequest , its declared as public, as it's supposed to be. What's wrong?


Solution

  • The constructor is not public - you're supposed to use the Builder to create instances of the request: https://developer.android.com/reference/androidx/camera/viewfinder/ViewfinderSurfaceRequest.Builder

    That way you can use default values or XML layout where appropriate.

    You can also use the extension method to skip a few lines of reading from CameraCharacteristics: https://developer.android.com/reference/androidx/camera/viewfinder/ViewfinderSurfaceRequest.Builder#(androidx.camera.viewfinder.ViewfinderSurfaceRequest.Builder).populateFromCharacteristics(android.hardware.camera2.CameraCharacteristics)