Android CameraX 是一个用于简化 Android 相机功能开发的库
在 app 的 build.gradle 文件中,添加 CameraX 和存储库的依赖项:
dependencies {
implementation 'androidx.camera:camera-core:1.0.0'
implementation 'androidx.camera:camera-camera2:1.0.0'
implementation 'androidx.camera:camera-lifecycle:1.0.0'
implementation 'androidx.camera:camera-view:1.0.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
}
同时,确保添加了以下权限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.front" />
在 Activity 或 Fragment 中,配置 CameraX 和生命周期组件:
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.camera.core.ImageAnalysis
import androidx.camera.core.ImageProxy
import androidx.camera.core.CameraSelector
import androidx.camera.core.ImageAnalyzer
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch
class MainActivity : AppCompatActivity() {
private val viewModel: MainViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
cameraProviderFuture.addListener({
val cameraProvider = cameraProviderFuture.get()
bindPreview(cameraProvider)
}, ContextCompat.getMainExecutor(this))
}
private fun bindPreview(cameraProvider: ProcessCameraProvider) {
val preview = androidx.camera.core.Preview.Builder().build()
preview.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraSelector = CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build()
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
cameraProviderFuture.addListener({
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraSelector = CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {
it.setSurfaceProvider(findViewById<View>(R.id.previewView).surfaceProvider)
}
val cameraProvider = cameraProviderFuture.get()
val imageAnalyzer = ImageAnalyzerBuilder().build()
val cameraProvider = cameraProviderFuture.get()
val preview = androidx.camera.core.Preview.Builder().build().also {