Search code examples
androidkotlinandroid-camerax

Is it possible to apply real time filter to android-camerax?


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


Solution

  • 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