Skip to content

A boilerplate camera app for processing camera preview frames in real-time using the OpenCV Android SDK and the native OpenCV library.

License

Notifications You must be signed in to change notification settings

AndrewBloom/AndroidOpenCVCamera

 
 

Repository files navigation

AndroidOpenCVCamera

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.

AndroidOpenCVCamera screenshot

Features

  • 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

Setup

[Tested with OpenCV 4.8]

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.

Step 1 - Download the OpenCV Android SDK

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.

Step 2 - Import the OpenCV Android SDK as a module in this project

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.

Step 3 - Modify the OpenCV module's imported build.gradle

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 }

Step 4 - Build and run

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.

Credits

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.

Original Credits

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.

About

A boilerplate camera app for processing camera preview frames in real-time using the OpenCV Android SDK and the native OpenCV library.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 71.0%
  • C++ 19.8%
  • CMake 9.2%