I'm using cameraX for recording video. I need to apply real time filters to the camera preview using android-gpuimage or any other library. Is it possible? if yes kindly provide an example.
@SuppressLint("RestrictedApi")
private fun startCamera() {
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
cameraProviderFuture.addListener(Runnable {
val cameraProvider = cameraProviderFuture.get()
preview = Preview.Builder()
.build()
val cameraSelector = CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build()
videoCapture = VideoCaptureConfig.Builder()
.build()
try {
cameraProvider.unbindAll()
camera = cameraProvider.bindToLifecycle(this as LifecycleOwner, cameraSelector, preview, videoCapture)
preview.setSurfaceProvider(viewFinder.createSurfaceProvider())
} catch (e: Exception) {
Log.e("CameraX", "Use case binding failed!", e)
}
}, ContextCompat.getMainExecutor(this))
}
I am using camerax version 1.0.0-beta06 in this project
For developers who are starting with Android Camera, refreshing their app to the latest, or migrating their app from Camera 1, CameraX is the best tool to get started! CameraX offers key benefits that empower developers, and address the complexities of the ecosystem.
Open the Camera app, and then select Photo. Tap the Filter icon (it looks like a magic wand) in the top right corner of the screen, and then tap My filters.
CameraX is an addition to Jetpack that makes it easier to add camera capabilities to your app. The library provides a number of compatibility fixes and workarounds to help make the developer experience consistent across many devices.
Both video and filtering are not officially supported by CameraX, but you can work around it by encoding the output of ImageAnalysis
to video.
The output of ImageAnalysis
is YUV420
byte array. It can be converted to Bitmap
using this code snippet, and then you can apply GPUImage
filter against the Bitmap
. Then encode a series of Bitmap
to a video. This is inefficient on many levels, but it should work.
You can checkout this code sample for filtering CameraX preview with GPUImage: https://github.com/xizhang/camerax-gpuimage
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With