A boilerplate camera app for processing camera preview frames in real-time using the OpenCV Android SDK and the native OpenCV library.
This is intended to give developers a simple way to prototype real-time image processing techniques on a smartphone. The application is just the simplest demo for using OpenCV and uses a very slow way to grab images, which is through openGL glReadPixels. It's far from being the best practice and it's quite slow.
As an example, this application uses OpenCV to get the Laplacian of each preview frame.
- Modifies preview frame from Camera2 API with OpenCV Android SDK
- Performs image processing with native C++
- Ability to swap between front and back cameras
- Shows FPS in application
This project contains only the Android code necessary for performing native image processing. In order to import and use OpenCV, users must perform a few steps, which are outlined below. You can also reference this guide.
You can obtain the latest version of the OpenCV Android SDK at https://github.com/opencv/opencv/releases, labelled opencv-{VERSION}-android-sdk.zip under Assets. After extracting the contents of the zip file, you should have a directory called OpenCV-android-sdk.
Open the cloned version of this repo as a project in Android Studio. Then, click File -> New -> Import Module. Navigate to where you just extracted the OpenCV Android SDK files, and use OpenCV-android-sdk/sdk/ as the source directory for the import. Click Next, then click Finish, using the default import settings.
After the import completes, a new folder sdk is created in the root of the project. In this directory, there's a file called build.gradle, which you must modify in order to meet the SDK requirements of the application. Make the following changes:
for me, this meant: compileSdkVersion 33 targetSdkVersion 33
compileOptions { sourceCompatibility JavaVersion.VERSION_17 targetCompatibility JavaVersion.VERSION_17 }
buildFeatures { aidl = true buildConfig = true }
The app should build and run now. If you want to modify the behavior of the application, MyGLSurfaceView.onCameraTexture
is the callback used in the Java layer, and it calls processFrame
to do work in the native layer.
I updated the sample from https://github.com/J0Nreynolds/AndroidOpenCVCamera to an update version of OpenCV (4.8), the android app (manifest and gradle) and improved the CMakelist file.
I created this application using OpenCV's Android samples, namely Tutorial 4 - OpenCL. The OpenCL sample demonstrated how to use the OpenCV Android SDK's CameraGLSurfaceView
, which provides a nice interface for intercepting and processing Android camera preview frames.