In this tutorial, we’ll be integrating OpenCV in our Android Application. We have already discussed the basics of CameraX here. So today, we’ll see how to run OpenCV image processing on live camera feed.

Android CameraX

CameraX which comes with Android Jetpack is all about use cases. Basically, a camera can be built using three core use cases – Preview, Analyse, Capture.

Having already done preview and capture previously, today our main focus is on Analysis.

Hence, we’ll be using OpenCV to analyze frames and process them in real-time.

What is OpenCV?

OpenCV is a computer vision libraries which contains more than 2000 algorithms related to image processing. It has been written in C++.

Thankfully, a lot of high-level stuff in OpenCV can be done in Java. Besides, we can always use the JNI interface to communicate between Java and C++.

We can download and import the OpenCV SDK from their official GitHub repository. It’s a pretty big module. Due to size and project space constraints, we’ll be using a Gradle dependency library shown below:

Did you know?Android applications that use OpenCV modules have a large APK size.

In the following section using CameraX and OpenCV, we’ll convert the color spaces to give the camera a totally different outlook.

Android OpenCV Project Structure



Android Camerax Opencv Project Structure

Android OpenCV Code

The code for the activity_main.xml layout is given below:

The code for the is given below:

In the above code, inside Image Analysis use case, we retrieve the Bitmap from the TextureView.

Utils.bitmapToMat is used to convert the Bitmap to Mat object. This method is a part of OpenCV android.

Mat class is basically used to hold the image. It consists of a matrix header and a pointer to the matrix which contains pixels values.

In our image analysis, we convert the mat color space from one type to another use ImgProc.cvtColor.

Having converted the mat to a different color space, we then convert it a bitmap and show it on the screen in an ImageView.

By default, the image is of RGB type. Using the menu options we can convert the image to types GRAY, LAB, HSV.

Let’s look at the output of the application in action.

Android Camerax Opencv Output New

Android Camerax Opencv Output New

So we were able to analyze, view the captured frame and optionally save it in our internal storage directory.

That brings an end to this tutorial. You can download the project from the link below or view the full source code in our Github repository.

By admin

Leave a Reply

%d bloggers like this: