CameraX 是一个 Jetpack 支持库,利用了 camera2 的功能,可感知生命周期,解决了设备兼容性问题,向后兼容至 Android 5.0(API 级别 21),并提供一致且易用的 API 接口。借助 CameraX,开发者只需两行代码就能实现与预安装的相机应用相同的相机体验和功能!
它是一种架构,官方介绍它是非常强大好用,那么,尝试用起来吧。
- 1、创建 CameraX 的练习项目
minSdkVersion 选 21,Android 5.0。
- 2、添加依赖
// CameraX core library using camera2 implementation
implementation "androidx.camera:camera-camera2:1.0.1"
// CameraX Lifecycle Library
implementation "androidx.camera:camera-lifecycle:1.0.1"
// CameraX View class
implementation "androidx.camera:camera-view:1.0.0-alpha27"
- 3、添加插件 kotlin-android-extensions
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
- 4、编写布局文件 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/camera_capture_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:elevation="2dp"
android:scaleType="fitCenter"
android:text="Take Photo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<androidx.camera.view.PreviewView
android:id="@+id/viewFinder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
- 5、打开 AndroidManifest.xml 添加权限
<!-- 确保设备有 camera .any 意味着可能是前置摄像头或者是后置摄像头 -->
<uses-feature android:name="android.hardware.camera.any" />
<uses-permission android:name="android.permission.CAMERA" />
6、正式编写代码啦,包括了打开相机,截取图像,保存图片,图片分析等等功能编写。
步骤:
- 将 CameraX 依赖项加入到的项目中。
- 显示相机取景器(使用预览用例)。
- 实现照片捕获,将图像保存到存储(使用 ImageCapture 用例)。
- 实时分析来自相机的帧(使用 ImageAnalysis 用例)。
package com.pyn.cameraxpractice
import android.Manifest
import android.content.pm.PackageManager
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.camera.core.*
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.pyn.cameraxpractice.databinding.ActivityMainBinding
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File
import java.nio.ByteBuffer
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
class MainActivity : AppCompatActivity() {
private lateinit var mBinding: ActivityMainBinding
private var imageCapture: ImageCapture? = null
private lateinit var outputDirectory: File
private lateinit var cameraExecutor: ExecutorService
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(mBinding.root)
if (allPermissionsGranted()) {
// 权限请求完毕,且授权了
startCamera()
} else {
ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS)
}
mBinding.btnCameraCapture.setOnClickListener { takePhoto() }
outputDirectory = getOutputDirectory()
cameraExecutor = Executors.newSingleThreadExecutor()
}
/**
* 请求权限的回调
*/
override fun onRequestPermissionsResult(
requestCode: Int, permissions: Array<String>, grantResults:
IntArray
) {
if (requestCode == REQUEST_CODE_PERMISSIONS) {
if (allPermissionsGranted()) {
// 如果所有权限都授权成功了
startCamera()
} else {
Toast.makeText(
this,
"Permissions not granted by the user.",
Toast.LENGTH_SHORT
).show()
finish()
}
}
}
/**
* 拍照
*/
private fun takePhoto() {
// 获取对 ImageCapture 用例的引用
val imageCapture = imageCapture ?: return
// 创建文件来保存图像。添加时间戳,以便文件名是唯一的。
val photoFile = File(
outputDirectory,
SimpleDateFormat(FILENAME_FORMAT, Locale.US).format(System.currentTimeMillis()) + ".jpg"
)
// 指定将输出内容保存在我们刚刚创建的文件中
val outputOptions = ImageCapture.OutputFileOptions.Builder(photoFile).build()
// 拍照
imageCapture.takePicture(
outputOptions,
ContextCompat.getMainExecutor(this),
object : ImageCapture.OnImageSavedCallback {
override fun onError(exception: ImageCaptureException) {
Log.e(TAG, "Photo capture failed: ${exception.message}", exception)
}
override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) {
val savedUri = Uri.fromFile(photoFile)
val msg = "Photo capture succeeded: $savedUri"
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
Log.d(TAG, msg)
}
}
)
}
/**
* 打开相机
*/
private fun startCamera() {
// 定义配置,创建用例实例,用于将相机的生命周期绑定到生命周期所有者,这消除了打开和关闭相机的任务,因为 CameraX 具有生命周期感知能力。
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
// 向 cameraProviderFuture 添加一个监听器。
// 参数一:Runnable
// 参数二:ContextCompat.getMainExecutor() 将返回一个在主线程上运行的 Executor。
cameraProviderFuture.addListener(Runnable {
// 用于将相机的生命周期绑定到应用程序进程中的 LifecycleOwner
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
// 初始化预览对象,取景器是用来让用户预览照片的,CameraX Preview 实现取景器
val preview = Preview.Builder().build().also {
it.setSurfaceProvider(viewFinder.surfaceProvider)
}
imageCapture = ImageCapture.Builder().build()
val imageAnalyzer = ImageAnalysis.Builder()
.build()
.also {
it.setAnalyzer(cameraExecutor, LuminosityAnalyzer { luma ->
Log.d(TAG, "Average luminosity: $luma")
})
}
// 默认选择后置摄像头
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
try {
// 在重新绑定之前取消绑定用例
cameraProvider.unbindAll()
// 将cameraSelector 和预览对象绑定到 cameraProvider
cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageCapture, imageAnalyzer)
} catch (exc: Exception) {
// 绑定失败
Log.e(TAG, "Use case binding failed", exc)
}
}, ContextCompat.getMainExecutor(this))
}
private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all {
ContextCompat.checkSelfPermission(baseContext, it) == PackageManager.PERMISSION_GRANTED
}
private fun getOutputDirectory(): File {
val mediaDir = externalMediaDirs.firstOrNull()?.let {
File(it, resources.getString(R.string.app_name)).apply {
mkdirs()
}
}
return if (mediaDir != null && mediaDir.exists()) mediaDir else filesDir
}
override fun onDestroy() {
super.onDestroy()
cameraExecutor.shutdown()
}
companion object {
private const val TAG = "CameraXBasic"
private const val FILENAME_FORMAT = "yyyy-MM-dd-HH"
private const val REQUEST_CODE_PERMISSIONS = 10
private val REQUIRED_PERMISSIONS = arrayOf(Manifest.permission.CAMERA)
}
private class LuminosityAnalyzer(private val listener: LumaListener) : ImageAnalysis.Analyzer {
private fun ByteBuffer.toByteArray(): ByteArray {
// 倒带
rewind()
val data = ByteArray(remaining())
get(data)
return data
}
override fun analyze(image: ImageProxy) {
val buffer = image.planes[0].buffer
val data = buffer.toByteArray()
val pixels = data.map { it.toInt() and 0xFF }
val luma = pixels.average()
listener(luma)
image.close()
}
}
}
typealias LumaListener = (luma: Double) -> Unit
保存了图片后,可以从手机文件中找到该图片,地址如下图:
分析日志如下:
上述只是简单照着官方文档练习,具体更为详细的还是需要再多去看看官方文档研究下的,api 很灵活。多尝试尝试。
附:
CameraX 开发者社区 「论坛」:
https://groups.google.com/a/android.com/g/camerax-developers
CameraX API 最佳实践:
https://github.com/android/camera-samples
官方介绍地址:
https://developer.android.com/training/camerax?hl=zh-cn