Jetpack之CameraX的使用
引入依賴
下面,就使用該庫來打造一個簡單的相機(jī)應(yīng)用吧~
首先引入依賴
def camerax_version = "1.1.0-beta03"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
implementation "androidx.camera:camera-video:${camerax_version}"
implementation "androidx.camera:camera-view:${camerax_version}"
implementation "androidx.camera:camera-extensions:${camerax_version}"
預(yù)覽
將 PreviewView 添加到布局中
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.camera.view.PreviewView android:id="@+id/previewView" android:layout_width="match_parent" android:layout_height="match_parent" /> <Button android:id="@+id/take" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="拍照" /> </RelativeLayout>
獲取 cameraProvider
val cameraProviderFuture = ProcessCameraProvider.getInstance(this) cameraProviderFuture.addListener({ val cameraProvider = cameraProviderFuture.get() bindViewAndLifecycle(cameraProvider) }, ContextCompat.getMainExecutor(this))
配置 ImageCapture
private val imageCapture: ImageCapture by lazy { ImageCapture.Builder().build() }
bindViewAndLifecycle:綁定視圖和Lifecycle
private fun bindViewAndLifecycle(cameraProvider: ProcessCameraProvider) { val previewView = findViewById<PreviewView>(R.id.previewView) val cameraSelector = CameraSelector.Builder() //如果是前置,可使用CameraSelector.LENS_FACING_FRONT .requireLensFacing(CameraSelector.LENS_FACING_BACK) .build() val preview = Preview.Builder().build() preview.setSurfaceProvider(previewView.surfaceProvider) cameraProvider.bindToLifecycle( this as LifecycleOwner, cameraSelector, imageCapture, preview ) }
這樣,就完成了相機(jī)的預(yù)覽功能了。
拍攝
實(shí)現(xiàn)拍照功能也很簡單,只需要創(chuàng)建文件,然后調(diào)用imageCapture的takePicture方法。
private fun takePicture() { val path = getExternalFilesDir(null)?.absolutePath + File.separator + System.currentTimeMillis() + ".jpg" val photoFile = File(path) if (!photoFile.exists()) { photoFile.createNewFile() } val fileOptions = ImageCapture.OutputFileOptions.Builder(photoFile).build() imageCapture.takePicture( fileOptions, ContextCompat.getMainExecutor(this), object : ImageCapture.OnImageSavedCallback { override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) { Toast.makeText(this@MainActivity, "保存成功", Toast.LENGTH_SHORT).show() } override fun onError(exception: ImageCaptureException) { Log.e(tag, "onError message:${exception.message}") } }) }
點(diǎn)擊按鈕時調(diào)用此方法即可
findViewById<Button>(R.id.take).setOnClickListener { takePicture() }
注意:作為相機(jī)應(yīng)用,可別忘了申請CAMERA權(quán)限哦。
這樣,一個簡單的相機(jī)應(yīng)用就完成啦~
到此這篇關(guān)于Jetpack之CameraX的使用的文章就介紹到這了,更多相關(guān)Jetpack CameraX內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
android顯示TextView文字的倒影效果實(shí)現(xiàn)代碼
這篇文章主要介紹了android顯示TextView文字的倒影效果實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-02-02Android Studio導(dǎo)入so文件到項(xiàng)目中的實(shí)例詳解
這篇文章主要介紹了Android Studio導(dǎo)入so文件到項(xiàng)目中的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09輕松實(shí)現(xiàn)可擴(kuò)展自定義的Android滾輪時間選擇控件
這篇文章主要為大家詳細(xì)介紹了可擴(kuò)展自定義的Android滾輪時間選擇控件,結(jié)合WheelView實(shí)現(xiàn)滾輪選擇日期操作,感興趣的小伙伴們可以參考一下2016-07-07Android獲取清單文件中的meta-data,解決碰到數(shù)值為null的問題
這篇文章主要介紹了Android獲取清單文件中的meta-data,解決碰到數(shù)值為null的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03關(guān)于Kotlin的自動類型轉(zhuǎn)換詳解
這篇文章主要給大家介紹了關(guān)于Kotlin的自動類型轉(zhuǎn)換的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Android自定義控件之圓形/圓角的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android自定義控件之圓形/圓角的實(shí)現(xiàn)代碼,感興趣的小伙伴們可以參考一下2016-03-03