CameraX
CameraX的介紹
CameraX 是一個 Jetpack 支持庫,旨在幫助您簡化相機應用的開發工作。它提供了一個一致且易於使用的API界面,可以兼容至android5.0。(API 級別 21)
CameraX的基礎使用
本文章使用kotlin代碼
第一步
要使用CameraX首先要在build.gradle(module:app)的dependencies{}裏添加如下代碼:
def camerax_version = "1.0.0-beta07"
// 使用camera2實現的CameraX核心庫
implementation "androidx.camera:camera-camera2:$camerax_version"
// CameraX生命週期庫
implementation "androidx.camera:camera-lifecycle:$camerax_version"
// CameraX View類
implementation "androidx.camera:camera-view:1.0.0-alpha14"
然後進行gradle同步(這裏可能需要開代理)
第二步
在主要佈局文件裏創建一個Button和一個androidx.camera.view.PreviewView(使用自定義的Button會更好)如下:
<ImageButton
android:id="@+id/camera_capture_button"
android:layout_width="92dp"
android:layout_height="92dp"
android:layout_marginBottom="80dp"
android:scaleType="fitCenter"
android:background="@drawable/ic_shutter"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:elevation="2dp"
/>
<androidx.camera.view.PreviewView
android:id="@+id/viewFinder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
第三步
在AndroidManifest.xml文件中申請權限
<uses-feature android:name="android.hardware.camera.any" />
<uses-permission android:name="android.permission.CAMERA" />
在第四步中要在activity的kt文件中添加請求權限的方法
第四步
創建mainactivity的kt文件,代碼如下:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.Manifest
import android.content.pm.PackageManager
import android.net.Uri
import android.util.Log
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import java.util.concurrent.Executors
import androidx.camera.core.*
import androidx.camera.lifecycle.ProcessCameraProvider
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
typealias LumaListener = (luma: Double) -> Unit
class MainActivity : AppCompatActivity() {
private var imageCapture: ImageCapture? = null
private lateinit var outputDirectory: File
private lateinit var cameraExecutor: ExecutorService
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 請求相機權限
if (allPermissionsGranted()) {
startCamera()
} else {
ActivityCompat.requestPermissions(
this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS)
}
// 設置拍照的監聽按鈕
camera_capture_button.setOnClickListener { takePhoto() }
outputDirectory = getOutputDirectory()
cameraExecutor = Executors.newSingleThreadExecutor()
}
private fun takePhoto() {
// 獲得可修改的圖像捕獲用例
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(exc: ImageCaptureException) {
Log.e(TAG, "Photo capture failed: ${exc.message}", exc)
}
override fun onImageSaved(output: 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() {
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
cameraProviderFuture.addListener(Runnable {
// 用於將攝像機的生命週期綁定到生命週期所有者
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
val preview = Preview.Builder()
.build()
.also {
it.setSurfaceProvider(viewFinder.createSurfaceProvider())
}
imageCapture = ImageCapture.Builder()
.build()
// 默認選擇後置攝像頭,這邊可以設置方法選擇攝像頭
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
try {
cameraProvider.unbindAll()
// 將用例綁定到相機
cameraProvider.bindToLifecycle(
this, cameraSelector, preview, imageCapture)
} 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
}
//申請權限的方法
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 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-mm-ss-SSS"
private const val REQUEST_CODE_PERMISSIONS = 10
private val REQUIRED_PERMISSIONS = arrayOf(Manifest.permission.CAMERA)
}
}
到此就可以利用CameraX實現一個可拍照儲存的相機
CameraX的進階使用
CameraX可以和MLKit或Tensorflow Lite一起使用,來對圖像進行實施分析。
如果要使用Tensorflow Lite,首先要在build.gradle(module:app)添加Tensorflow Lite的依賴,並進行gradle同步。
implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly'
implementation 'org.tensorflow:tensorflow-lite-gpu:0.0.0-nightly'
implementation 'org.tensorflow:tensorflow-lite-support:0.0.0-nightly'
然後在ImageAnalysis.Analyzer接口的類中重寫函數。
imageAnalysis.setAnalyzer(executor, ImageAnalysis.Analyzer { image ->
if (!::bitmapBuffer.isInitialized) {
// 在分析儀開始運行後,纔開始初始化圖像旋轉和RGB圖像緩衝區
imageRotationDegrees = image.imageInfo.rotationDegrees
bitmapBuffer = Bitmap.createBitmap(
image.width, image.height, Bitmap.Config.ARGB_8888)
}
// 提前退出
if (pauseAnalysis) {
image.close()
return@Analyzer
}
// 將圖像轉換爲RGB並將其放置在我們的共享緩衝區中
image.use { converter.yuvToRgb(image.image!!, bitmapBuffer) }
// 在Tensorflow中處理圖像
val tfImage = tfImageProcessor.process(tfImageBuffer.apply { load(bitmapBuffer) })
// 對當前幀執行對象檢測
val predictions = detector.predict(tfImage)
// 報告最正確的預測
reportPrediction(predictions.maxBy { it.score })
// 計算FPS
val frameCount = 10
if (++frameCounter % frameCount == 0) {
frameCounter = 0
val now = System.currentTimeMillis()
val delta = now - lastFpsTimestamp
val fps = 1000 * frameCount.toFloat() / delta
Log.d(TAG, "FPS: ${"%.02f".format(fps)}")
lastFpsTimestamp = now
}
})
參考文檔:android官方文檔
原文作者:李宇恆
原文鏈接