Edit image with Android and OpenCV and JNI (C/C++)

Requires:
  • opencv android SDK (2.4.13.2)
  • android ndk (r14b) to build JNI
  • eclipse + ADT plugin
Note: tools version above while writing this blog.
After tutorial you can:
  • call C/C++ library in Java in your app
  • edit image with android and opencv
  • embed features wrote by C/C++ to Android (Java) app
  • improve your app speed by using native (C/C++)
  • idea to apply some secure method by using native code
Source code: https://github.com/vmtram/OpencvJni

Relative video: https://youtu.be/qCCC9Y1ldUs

Create android project add a image view and a button to main layout, then import opencv library.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_green_dark"
    android:orientation="vertical"
    tools:context="com.tramvm.opencvjni.MainActivity" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5" >

        <ImageView
            android:id="@+id/imgView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="45dp" 
            android:src="@drawable/test1"/>

        <Button
            android:id="@+id/btnProcess"
            android:layout_width="80dp"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="2dip"
            android:background="#fff"
            android:text="Process"
            android:textColor="#ff6347" />
    </RelativeLayout>

</LinearLayout>

Create 2 native methods prototype:
#include <jni.h>

extern "C" {

void Java_com_tramvm_opencvjni_MyNative_doSomething(JNIEnv* env, jobject thiz,
  jlong lOrigin);


void Java_com_tramvm_opencvjni_MyNative_drawRandom(JNIEnv* env, jobject thiz,
  jlong lOrigin, jint posX, int posY, int red, int green, int blue);
}

The implement of native code in MyNative.cpp
doSomething => just add a circle with yellow color at position 300x300 with radius 100 pixel drawRandom => draw circle with given position and color. These value were random in java code.
void Java_com_tramvm_opencvjni_MyNative_doSomething(JNIEnv* env, jobject thiz,
  jlong lOrigin) {

 LOGI("Call MyNative.doSomething");
 // Get object
 Mat& origin = *(Mat*) lOrigin;

 int px = 300;
 int py = 300;
 int radius = 100;
 int thickness = 50;

 circle(origin, Point(px, py), radius, Scalar(255, 255, 0), thickness); // this is yellow color
}

void Java_com_tramvm_opencvjni_MyNative_drawRandom(JNIEnv* env, jobject thiz,
  jlong lOrigin, jint posX, int posY, int red, int green, int blue) {

 LOGI("Call MyNative.drawRandom");
 // Get object
 Mat& origin = *(Mat*) lOrigin;

 int px = posX;
 int py = posY;
 int radius = 100;
 int thickness = 50;

 circle(origin, Point(px, py), radius, Scalar(red, green, blue), thickness);

}

Build the C/C++ code to .so file using command ndk-build
Note: if error command not found, please check your environment.


Create 2 java native method in com.tramvm.opencvjni.MyNative to communicate with native code:
package com.tramvm.opencvjni;

public class MyNative {
 public native void doSomething(long origin);
 public native void drawRandom(long origin, int posX, int posY, int red, int green, int blue);
}

Note: the function pattern in files .h and .cpp
Java_your_package_YourNativeClass_functionName()

Load opencv library and custom library in class MyOpencv
static {
  Log.i(TAG, "Load native");
  if (!OpenCVLoader.initDebug()) {
   Log.e(TAG, "Unable to load OpenCV");
  } else {
   Log.i(TAG, "OpenCV loaded");
  }
  System.loadLibrary("my_native");
  Log.i(TAG, "OpenCV my_native");
 }

Note: the generate file is libs/armeabi-v7a/libmy_native.so but when load this library just loadLibrary("my_native.so")

Big thanks for your time.

Please leave your comments if have any suggest or question :)

Comments

Popular posts from this blog

Recognize text from image with Python + OpenCV + OCR

Install OpenCV to work with Python