How to implement OpenCV-4.5.1 to Android projects

Mehmet Emin Gülşen
3 min readJan 17, 2021

--

I will try to explain how to import OpenCV-4.5.1 to your Android project step-by-step.

Step-1

Download OpenCV-4.5.1 for Android. You can also choose other versions of OpenCV.

OpenCV Releases

OpenCV-4.5.1 for Android

Step-2

Create an Android project. If you already have a project, you can pass this step and continue with Step-3.

  • Create New Project like given in the below.
  • Choose “Empty Activity”
  • Name your project. In my case it was “OpenCV-Android-Example” with package name “com.example.opencv_android_example”.

Step-3

  • Import OpenCV module to your project by following the path :

File > New > Import Module

  • You should choose the folder in the following path

OpenCV-android-sdk > sdk

  • After selecting the OpenCV folder, ADT Import Preferences will be showed and in this step you can select all and finish.

Step-4

  • Open build.gradle file of your project that should be look like given below and type “implementation project(path: ‘:opencv451’)”, “:opencv-451” can be changing based on your module name. (When importing module it automatically name it to “sdk”, so I have changed it to “opencv451” by right clicking on sdk module and refactoring module name)
  • After typing “implementation project(path:’opencv451’)”, you should Sync Project and it should be ready to go.
  • You should also check that minimum sdk version of both opencv and your app should be same. In my case, it was 21.

Step-5

So, we are ready to go now. Let’s check if everything works correctly. In this regard, we should write a simple application which has only a Button and ImageView, my activity_main.xml file like below.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageView
android:id="@+id/image_view"
android:layout_width="300dp"
android:layout_height="300dp"
android:scaleType="centerInside"
android:layout_centerInParent="true"/>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/white"
android:text="Click"
android:textColor="@color/black"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/image_view"/>
</RelativeLayout>

And, the MainActivity.java look like this.

package com.example.opencv_android_example;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;

public class MainActivity extends AppCompatActivity {


Button button;
ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OpenCVLoader.initDebug();
button = findViewById(R.id.button);
imageView = findViewById(R.id.image_view);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hello();
}
});
}


private void hello(){
Mat emptyMat = new Mat(500,500, CvType.CV_8UC3);
Imgproc.circle(emptyMat,new Point(250,250),25,new Scalar(255,255,255));
Bitmap bitmap = Bitmap.createBitmap(500,500, Bitmap.Config.RGB_565);
Utils.matToBitmap(emptyMat,bitmap);
imageView.setImageBitmap(bitmap);
}

}

So, when you run the code above. You should see something like this.

I have tried to explain how to implement OpenCV-4.5.1 into your projects. I hope it helps :)

--

--