Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 267
arcworks27 Avatar asked Jul 07 '20 16:07

arcworks27


People also ask

Should I use CameraX?

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.

How do you use filters on Android?

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.

What is CameraX Android?

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.


1 Answers

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

like image 191
Xi 张熹 Avatar answered Oct 17 '22 08:10

Xi 张熹