Im following this Google Codelab about CameraX
And when I get to the part of adding a Camera preview, I do it like this:
val previewConfig = PreviewConfig.Builder().apply {
setTargetAspectRatio(Rational(1, 1))
setTargetResolution(Size(640, 640))
setLensFacing(CameraX.LensFacing.BACK)
}.build()
val preview = Preview(previewConfig)
preview.setOnPreviewOutputUpdateListener { previewOutput ->
val parent = previewView.parent as ViewGroup
parent.removeView(previewView)
parent.addView(previewView, 0)
previewView.surfaceTexture = previewOutput.surfaceTexture
updateTransform()
}
CameraX.bindToLifecycle(this, preview)
And then I get the following error:
java.lang.ExceptionInInitializerError
at androidx.camera.core.Preview.<clinit>(Preview.java:55)
at com.grow.vision.ImagePreviewActivity.startCamera(ImagePreviewActivity.kt:120)
at com.grow.vision.ImagePreviewActivity.access$startCamera(ImagePreviewActivity.kt:28)
at com.grow.vision.ImagePreviewActivity$onCreate$1.run(ImagePreviewActivity.kt:140)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7032)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
Caused by: java.lang.IllegalStateException: CameraX not initialized yet.
at androidx.camera.core.CameraX.getCameraDeviceSurfaceManager(CameraX.java:485)
at androidx.camera.core.CameraX.getSurfaceManager(CameraX.java:282)
at androidx.camera.core.Preview$Defaults.<clinit>(Preview.java:416)
at androidx.camera.core.Preview.<clinit>(Preview.java:55)
Seems like I need to initialise CameraX somehow but neither the codelab or the CameraX documentation references something like that.
Has someone had this issue before?
I'm pretty sure I found the problem. Did you add Camera2 to your dependencies? I left it out thinking it wasn't necessary, but it's in the guide. I added it and that error stopped.
def camerax_version = "1.0.0-alpha01"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With