How to Apply Real-Time AI Filters Using OpenCV in Android

 

Introduction

In today’s world of social media and visual content, real-time image enhancement is a hot feature in many mobile apps. In this tutorial, you'll learn how to create an AI filter app using OpenCV on Android that can apply real-time adjustments like brightness, contrast, and more.




What You'll Learn

  • How to integrate OpenCV into Android Studio

  • How to use CameraBridgeViewBase to access the camera feed

  • How to apply real-time filters (brightness, contrast, gamma)

  • How to use SeekBars to adjust filters dynamically


Tools & Requirements

  • Android Studio (latest version)

  • OpenCV Android SDK

  • Java or Kotlin

  • Basic knowledge of Android UI


Step 1: Set Up OpenCV in Android

  1. Download OpenCV Android SDK from opencv.org

  2. Import it into your Android project:

    • File > New > Import Module > Select OpenCV SDK/sdk/java

  3. Add this line to your build.gradle:

    gradle

    implementation project(':openCVLibrary')

Step 2: Add OpenCV Camera View to Layout

xml

<org.opencv.android.JavaCameraView android:id="@+id/camera_view" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="visible" class="org.opencv.android.JavaCameraView" />

Step 3: Initialize OpenCV in Activity

java

public class MainActivity extends CameraActivity implements CvCameraViewListener2 { private JavaCameraView javaCameraView; private Mat matInput, matOutput; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); javaCameraView = findViewById(R.id.camera_view); javaCameraView.setCvCameraViewListener(this); } @Override public void onCameraViewStarted(int width, int height) { matInput = new Mat(); matOutput = new Mat(); } @Override public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) { matInput = inputFrame.rgba(); Core.multiply(matInput, new Scalar(1.2, 1.2, 1.2), matOutput); // Simple brightness filter return matOutput; } }

Step 4: Add Real-Time Controls

  • Add a SeekBar for each filter (e.g., brightness, contrast)

  • Inside onProgressChanged, update the filter value dynamically

java

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { brightness = progress / 100.0; } });

Apply that brightness in onCameraFrame using:

java

Core.multiply(matInput, new Scalar(brightness, brightness, brightness), matOutput);

Step 5: Build and Run

  • Install the app on a real Android device (camera access required)

  • Test your real-time filters by adjusting SeekBars


Conclusion

This simple AI filter app is just the beginning. You can extend it with face detection, edge detection, or even style transfer models using TensorFlow Lite or MediaPipe. With OpenCV and Android, you're only limited by your imagination.


🔗 Bonus: Full Source Code

You can find the full GitHub source code here:
👉 https://github.com/your-github-link