Kotlin Android 第23天,从 0 到 ML - CameraX

前言:

   今天来介绍如何创建使用 CameraX 来显示预览
    

大纲 :

build.gradle(app)

plugins{
    id 'kotlin-android-extensions'
}

dependencies {
    implementation "androidx.camera:camera-camera2:1.1.0-alpha08"
    implementation "androidx.camera:camera-lifecycle:1.1.0-alpha08"
    implementation 'androidx.camera:camera-view:1.0.0-alpha28'
}

**AndroidManifet.xml 宣告 CAMERA 权限 **

<uses-feature android:name="android.hardware.camera.any" />
<uses-permission android:name="android.permission.CAMERA" />

xml

…..
<androidx.camera.view.PreviewView
    android:id="@+id/previewView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

<ImageButton
    android:id="@+id/capture_button"
    android:layout_width="72dp"
    android:layout_height="72dp"
    android:layout_margin="24dp"
    app:srcCompat="@android:drawable/ic_menu_camera"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
…..

Activity:

typealias LumaListener = (luma: Double) -> Unit

class Day23Activity : 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_day23)

    // Request camera permissions
    if (allPermissionsGranted()) {
        startCamera()
    } else {
        ActivityCompat.requestPermissions(
            this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS)
    }

    // Set up the listener for take photo button
    capture_button.setOnClickListener { takePhoto() }

    outputDirectory = getOutputDirectory()

    cameraExecutor = Executors.newSingleThreadExecutor()

}


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
        val preview = Preview.Builder()
            .build()
            .also {
                it.setSurfaceProvider(previewView.surfaceProvider)
            }

        imageCapture = ImageCapture.Builder()
            .build()

        val imageAnalyzer = ImageAnalysis.Builder()
            .build()
            .also {
                it.setAnalyzer(cameraExecutor, LuminosityAnalyzer { luma ->
                    Log.d(TAG, "Average luminosity: $luma")
                })
            }

       
        // Select back camera as a default
        val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA

        try {
            // Unbind use cases before rebinding
            cameraProvider.unbindAll()

            // Bind use cases to camera
            cameraProvider.bindToLifecycle(
                this, cameraSelector, preview)

        } catch(exc: Exception) {
           
            Log.e(TAG, "Use case binding failed", exc)
        }

    }, ContextCompat.getMainExecutor(this))
}

执行结果:
https://ithelp.ithome.com.tw/upload/images/20210928/2012164332ScSH6S3o.png

参考:

https://developer.android.com/codelabs/camerax-getting-started


<<:  Day 14 : PHP - 如何更改XAMPP里的phpMyAdmin的密码?

>>:  D-17 路由 ? Routing

生存法则二:快速学习的能力

第一项技能是让我们尽早观察到风向方向的转变,让自己有时间做出反应,但终究观察和预测还是不足以让我们搭...

[Day 8] 2D世界中的数学 (一)

今日目标 基本的数学函式库(向量与阵列) 要多少才够 从另一个角度看,我认为游戏中的从小小的让角色移...

[经验分享] 从开发转QA工程师?为何想要转职?开发与QA的差异?

大家好!本篇将会以我转职的心路历程作为主轴,我为什麽转职成QA?当开发与当QA差很多吗?当QA该注意...

团队文化

谈完「人」之後,我们来谈另一个跟人很高度相关的题目:文化。 我大学时,曾经在一家加拿大的新创实习。...

【Day8】 将Function当成state传给子类别套用在事件上吧≖‿≖

相信各位看官们很熟悉各种Html的Events事件, 这篇呢~我们要透过上一篇所提到的State传入...