Android CameraX結合LibYUV和GPUImage自定義相機濾鏡
作者:itfitness 鏈接:https://www.jianshu.com/p/f084082cc0c6
本文目錄:
實現效果
實現步驟
1.引入依賴庫
這裡我引入的依賴庫有CameraX
、GPUImage
(濾鏡庫)、Utilcodex
(一款好用的工具類)
// CameraX core library using camera2 implementation implementation "androidx.camera:camera-camera2:1.0.1" // CameraX Lifecycle Library implementation "androidx.camera:camera-lifecycle:1.0.1" // CameraX View class implementation "androidx.camera:camera-view:1.0.0-alpha27" implementation'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.4.1' implementation 'com.blankj:utilcodex:1.30.6'
2.引入libyuv
這裡我用的是這個案例(https://github.com/theeasiestway/android-yuv-utils)裡面的libyuv
,如下
3.編寫CameraX預覽代碼
佈局代碼如下
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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/viewFinder" android:layout_width="0dp" android:layout_height="0dp" /> </FrameLayout>
Activity中開啟相機預覽代碼如下,基本都是Google官方提供的案例代碼
class MainActivity : AppCompatActivity() { private lateinit var cameraExecutor: ExecutorService override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) cameraExecutor = Executors.newSingleThreadExecutor() // Request camera permissions if (allPermissionsGranted()) { startCamera() } else { ActivityCompat.requestPermissions( this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS) } } private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all { ContextCompat.checkSelfPermission( baseContext, it) == PackageManager.PERMISSION_GRANTED } override fun onRequestPermissionsResult( requestCode: Int, permissions: Array<String>, grantResults: IntArray) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) 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 startCamera() { val cameraProviderFuture = ProcessCameraProvider.getInstance(this) cameraProviderFuture.addListener(Runnable { val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get() val preview = Preview.Builder() .build() .also { it.setSurfaceProvider(viewFinder.surfaceProvider) } val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA try { cameraProvider.unbindAll() cameraProvider.bindToLifecycle( this, cameraSelector, preview) } catch(exc: Exception) { Log.e(TAG, "Use case binding failed", exc) } }, ContextCompat.getMainExecutor(this)) } override fun onDestroy() { super.onDestroy() cameraExecutor.shutdown() } companion object { private const val TAG = "CameraXBasic" private const val REQUEST_CODE_PERMISSIONS = 10 private val REQUIRED_PERMISSIONS = arrayOf(Manifest.permission.CAMERA) } }
到這裡就可以實現相機預覽瞭
4.增加相機數據回調
我們要增加濾鏡效果就必須對相機的數據進行操作,這裡我們通過獲取相機數據回調來獲取可修改的數據
val imageAnalyzer = ImageAnalysis.Builder() //設置回調數據的比例為16:9 .setTargetAspectRatio(AspectRatio.RATIO_16_9) .build() .also { it.setAnalyzer(cameraExecutor,this@MainActivity) }
這裡我們還需要進行綁定
除此之外我們還需要在Activity中實現ImageAnalysis.Analyzer
接口,數據的獲取就在此接口的回調方法中獲取,如下所示,其中ImageProxy
就包含瞭圖像數據
override fun analyze(image: ImageProxy) { }
5.對回調數據進行處理
我們在相機數據回調的方法中對圖像進行處理並添加濾鏡,當然在此之前我們還需要創建GPUImage對象並設置濾鏡類型
private var bitmap:Bitmap? = null private var gpuImage:GPUImage? = null //創建GPUImage對象並設置濾鏡類型,這裡我使用的是素描濾鏡 private fun initFilter() { gpuImage = GPUImage(this) gpuImage!!.setFilter(GPUImageSketchFilter()) } @SuppressLint("UnsafeOptInUsageError") override fun analyze(image: ImageProxy) { //將Android的YUV數據轉為libYuv的數據 var yuvFrame = yuvUtils.convertToI420(image.image!!) //對圖像進行旋轉(由於回調的相機數據是橫著的因此需要旋轉90度) yuvFrame = yuvUtils.rotate(yuvFrame, 90) //根據圖像大小創建Bitmap bitmap = Bitmap.createBitmap(yuvFrame.width, yuvFrame.height, Bitmap.Config.ARGB_8888) //將圖像轉為Argb格式的並填充到Bitmap上 yuvUtils.yuv420ToArgb(yuvFrame,bitmap!!) //利用GpuImage給圖像添加濾鏡 bitmap = gpuImage!!.getBitmapWithFilterApplied(bitmap) //由於這不是UI線程因此需要在UI線程更新UI img.post { img.setImageBitmap(bitmap) //關閉ImageProxy,才會回調下一次的數據 image.close() } }
6.拍攝照片
這裡我們加一個拍照的按鈕
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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/viewFinder" android:layout_width="match_parent" android:layout_height="match_parent" /> <ImageView android:id="@+id/img" android:scaleType="centerCrop" android:layout_width="match_parent" android:layout_height="match_parent"/> <Button android:id="@+id/bt_takepicture" android:layout_gravity="center_horizontal|bottom" android:layout_marginBottom="100dp" android:text="拍照" android:layout_width="70dp" android:layout_height="70dp"/> </FrameLayout>
然後我們在Activity中添加拍照的邏輯,其實就是將Bitmap轉為圖片保存到SD卡,這裡我們使用瞭之前引入的Utilcodex
工具,當我們點擊按鈕的時候isTakePhoto
會變為true
,然後在相機的回調中就會進行保存圖片的處理
bt_takepicture.setOnClickListener { isTakePhoto = true }
並且我們加入變量控制,在拍照的時候不處理回調數據
@SuppressLint("UnsafeOptInUsageError") override fun analyze(image: ImageProxy) { if(!isTakePhoto){ //將Android的YUV數據轉為libYuv的數據 var yuvFrame = yuvUtils.convertToI420(image.image!!) //對圖像進行旋轉(由於回調的相機數據是橫著的因此需要旋轉90度) yuvFrame = yuvUtils.rotate(yuvFrame, 90) //根據圖像大小創建Bitmap bitmap = Bitmap.createBitmap(yuvFrame.width, yuvFrame.height, Bitmap.Config.ARGB_8888) //將圖像轉為Argb格式的並填充到Bitmap上 yuvUtils.yuv420ToArgb(yuvFrame,bitmap!!) //利用GpuImage給圖像添加濾鏡 bitmap = gpuImage!!.getBitmapWithFilterApplied(bitmap) //由於這不是UI線程因此需要在UI線程更新UI img.post { img.setImageBitmap(bitmap) if(isTakePhoto){ takePhoto() } //關閉ImageProxy,才會回調下一次的數據 image.close() } }else{ image.close() } } /** * 拍照 */ private fun takePhoto() { Thread{ val filePath = File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS),"${System.currentTimeMillis()}save.jpg") ImageUtils.save(bitmap,filePath.absolutePath,Bitmap.CompressFormat.PNG) ToastUtils.showShort("拍攝成功") isTakePhoto = false }.start() }
效果如下
保存的圖片在如下目錄
保存的圖片如下
到此這篇關於Android CameraX結合LibYUV和GPUImage自定義相機濾鏡的文章就介紹到這瞭,更多相關Android CameraX自定義相機濾鏡內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!