8

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

arcworks27
  • 83
  • 1
  • 4
  • did you find any solution? – iamkdblue Jul 19 '20 at 17:34
  • @iamkdblue No i didn't... actually there is no production release in camerax to use it for video capture or applying any kind of filters on video preview. It seems using camera2 is the only way as of now. – arcworks27 Jul 22 '20 at 07:57
  • What about the real-time filter in the image using camerax? How did you implement that efficiently ? – Astha Garg Jan 10 '23 at 05:50

1 Answers1

12

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

Xi 张熹
  • 10,492
  • 18
  • 58
  • 86