我正在关注这个关于 CameraX 的 Google Codelab
当我开始添加相机预览时,我会这样做:
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)
然后我收到以下错误:
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)
似乎我需要以某种方式初始化 CameraX,但 codelab 或 CameraX 文档都没有引用类似的东西。
以前有人遇到过这个问题吗?