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 set up to perform image processing using native code for best performance.
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, but you must perform Step 5 after doing so.
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/java as the source directory for the import. Click Next, then click Finish, using the default import settings.
After the import completes, a new folder openCVLibrary{VERSION_NUMBER} 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:
compileSdkVersion 14 -> compileSdkVersion27
minSdkVersion 8 -> minSdkVersion 23
targetSdkVersion 21 -> targetSdkVersion 27
Create a new folder called jniLibs in the project's app/src/main folder. Then, copy everything in OpenCV-android-sdk/sdk/native/libs to the jniLibs directory you just created.
Lastly, you just need to modify two strings in the CMakeList file to ensure your native libraries are linked correctly. Open the project's app/CMakeLists.txt file. The first three lines should look like this:
# Path definitions
set(pathToProject /Users/jonathanreynolds/Documents/Projects/AndroidOpenCVCamera)
set(pathToOpenCv /Users/jonathanreynolds/Documents/Projects/OpenCV-android-sdk)
Change /Users/jonathanreynolds/Documents/Projects/AndroidOpenCVCamera to be the path to the project, and change /Users/jonathanreynolds/Documents/Projects/OpenCV-android-sdk to be the path to your OpenCV Android SDK folder.
For Windows, make sure you are careful with the format of strings you use. You need to use forward slashes between directories, and backslashes should only be used to escape whitespace characters. Example:
C:\Users\Jonathan Reynolds\Documents\GitHub\AndroidOpenCVCamera
becomes set(pathToProject C:/Users/Jonathan\ Reynolds/Documents/GitHub/AndroidOpenCVCamera)
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 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.