117.info
人生若只如初见

android camerax如何处理图像保存

Android CameraX 是一个用于简化 Android 相机功能开发的库

  1. 添加依赖项:

在 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'
}

同时,确保添加了以下权限:




  1. 配置 CameraX:

在 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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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 {

未经允许不得转载 » 本文链接:https://www.117.info/ask/fef39AzsIAQVRBw.html

推荐文章

  • android viewstub有哪些限制

    Android ViewStub 是一种轻量级的视图,它在初始化时不会立即加载,而是在需要时才显示。这有助于提高应用程序的性能,特别是在加载大型布局时。然而,ViewStub ...

  • android viewstub如何简化代码

    ViewStub 是一个轻量级的占位符视图,它在需要时才显示出来。它可以用来延迟加载布局资源,从而减少应用的启动时间和内存占用。要简化代码,你可以遵循以下步骤:...

  • android viewstub能嵌套布局吗

    Android ViewStub 不能嵌套布局。ViewStub 是一个轻量级的占位符视图,它本身不具备布局功能,用于在需要的时候延迟加载布局资源。因为 ViewStub 本质上只是一个...

  • android viewstub怎样提高性能

    Android ViewStub 是一种轻量级的占位符视图,它在需要时才显示出来。这可以减少应用的初始启动时间,从而提高性能。要提高 ViewStub 的性能,可以遵循以下几点建...

  • android camerax怎样实现图像裁剪

    使用Android CameraX库进行图像裁剪非常简单。首先,确保您已经在项目中添加了CameraX库的依赖项。然后,您可以按照以下步骤实现图像裁剪: 添加依赖项 在项目的...

  • android camerax如何处理图像旋转

    Android CameraX 是一个用于简化 Android 相机功能开发的库 添加 CameraX 和 AndroidX Core 依赖项: 在 app/build.gradle 文件中添加以下依赖项:
    depende...

  • android camerax怎样优化性能

    要优化Android CameraX的性能,可以采取以下措施: 使用合适的图像处理器:根据项目需求选择合适的图像处理器,例如使用ProcessCameraProvider来处理图像。 减少...

  • android camerax如何处理多摄像头

    在Android中使用CameraX处理多摄像头时,你可以采用以下步骤: 获取摄像头设备列表:
    要获取设备上的所有摄像头,你需要使用CameraManager类。首先,需要在...