一、学习内容
1.Android的ImageView应用
2.使用Android CameraX
二、遇到问题
1.使用cameraX出现报错:Must call CameraX.initialize() first
解决方法: 1.首先,项目必须依赖以下4个 camearx 库:
dependencies {
val cameraXVersion = "1.0.0-alpha10"
implementation("androidx.camera:camera-core:$cameraXVersion")
implementation("androidx.camera:camera-camera2:$cameraXVersion")
implementation("androidx.camera:camera-lifecycle:$cameraXVersion")
implementation("androidx.camera:camera-view:1.0.0-alpha07")
}
2,然后,自定义的 Application 必须实现?CameraXConfig.Provider ?接口:
class HappyFaceApplication : Application(), CameraXConfig.Provider {
override fun onCreate() {
super.onCreate()
}
override fun getCameraXConfig(): CameraXConfig {
return Camera2Config.defaultConfig()
}
}
最后,ProcessCameraProvider.getInstance(this) ?必须要在构造 Preview 、ImageCapture、ImageAnalysis 之前
// 该方法一定要在构造 Preview 、ImageCapture、ImageAnalysis 之前
cameraProviderFuture = ProcessCameraProvider.getInstance(this)
// 预览
val preview = Preview.Builder().build()
preview.setSurfaceProvider(viewBinding.previewView.previewSurfaceProvider)
// 拍照
val capture = ImageCapture.Builder().build()
// 分析
val analysis = ImageAnalysis.Builder().build()
// 可以放在需要的地方进行初始化
cameraProviderFuture.addListener(Runnable {
val cameraProvider = cameraProviderFuture.get()
cameraProvider.bindToLifecycle(
this@MainActivity as LifecycleOwner,
CameraSelector.DEFAULT_FRONT_CAMERA,
preview,
capture,
analysis
)
}, ContextCompat.getMainExecutor(this))
三、下周学习内容
1、继续学习cameraX,研究现有的Android项目代码
|