CameraX 使用

作者: ZoranLee | 来源:发表于2021-12-06 16:42 被阅读0次

    优点

    • 简单易用:比Camera和Camera2 更简洁
    • 生命周期管理:CameraX 和 Lifecycle 结合在一起,方便管理。比camera2 少大量样板代码。
    • 兼容性:CameraX 基于Camera2 API 实现,兼容至 Android L (API 21)
    • 扩展性:可以使用和原生摄像头应用同样的功能(如:人像、夜间模式、HDR、滤镜、美颜)
    • 自动化测试:对摄像头功能进行深度测试

    使用

    • 添加Gradle依赖
    def camerax_version = "1.0.1"
    // CameraX core library using camera2 implementation
    implementation "androidx.camera:camera-camera2:$camerax_version"
    // CameraX Lifecycle Library
    implementation "androidx.camera:camera-lifecycle:$camerax_version"
    // CameraX View class
    implementation "androidx.camera:camera-view:1.0.0-alpha27"
    
    • Java 8
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    
    
    • 布局
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       xmlns:app="http://schemas.android.com/apk/res-auto"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       tools:context=".MainActivity">
    
       <Button
           android:id="@+id/camera_capture_button"
           android:layout_width="100dp"
           android:layout_height="100dp"
           android:layout_marginBottom="50dp"
           android:scaleType="fitCenter"
           android:text="Take Photo"
           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" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
    
    • 权限
    <uses-feature android:name="android.hardware.camera.any" />
    <uses-permission android:name="android.permission.CAMERA" />
    

    请求完权限之后

    • startCamera
    private fun startCamera() {
       val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
    
       cameraProviderFuture.addListener(Runnable {
           // Used to bind the lifecycle of cameras to the lifecycle owner
           val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
    
           // Preview
           preview = Preview.Builder()
               .build()
    
           // Select back camera
           val cameraSelector = CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build()
    
           try {
               // Unbind use cases before rebinding
               cameraProvider.unbindAll()
    
               // Bind use cases to camera
               camera = cameraProvider.bindToLifecycle(
                   this, cameraSelector, preview)
               preview?.setSurfaceProvider(viewFinder.createSurfaceProvider(camera?.cameraInfo))
           } catch(exc: Exception) {
               Log.e(TAG, "Use case binding failed", exc)
           }
    
       }, ContextCompat.getMainExecutor(this))
    }
    
    

    1、创建的实例ProcessCameraProvider
    2、侦听器添加到中cameraProviderFuture
    3、添加ProcessCameraProvider,用于将相机的生命周期绑定到应用程序进程中的LifecycleOwner
    4、初始化Preview

    preview = Preview.Builder().build()
    

    5、CameraSelector 配置前后置镜头

    val cameraSelector = CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build()
    
    

    6、try、catch将cameraSelector和预览对象绑定到cameraProvider。将viewFinder的Surface提供程序附加到preview

    • takePhoto 拍照
    private fun takePhoto() {
       // Get a stable reference of the modifiable image capture use case
       val imageCapture = imageCapture ?: return
    
       // Create timestamped output file to hold the image
       val photoFile = File(
           outputDirectory,
           SimpleDateFormat(FILENAME_FORMAT, Locale.US
           ).format(System.currentTimeMillis()) + ".jpg")
    
       // Create output options object which contains file + metadata
       val outputOptions = ImageCapture.OutputFileOptions.Builder(photoFile).build()
    
       // Setup image capture listener which is triggered after photo has
       // been taken
       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)
               }
           })
    }
    
    

    1、获得ImageCapture
    2、创建一个文件来保存图像
    3、takePicture

    相关文章

      网友评论

        本文标题:CameraX 使用

        本文链接:https://www.haomeiwen.com/subject/fmbwtrtx.html