6, opencv ndk environment building (cmake mode)

opencv full name open source compute vision, open source visual Library

1, Preparations

The steps to import opencv are relatively simple. The preparation steps are divided into the following three steps:

  1. Download opencv android sdk https://opencv.org/releases.html
  2. Copy opencv Android SDK / SDK / native / LIBS to jniLibs of the project
  3. Copy opencv Android SDK / SDK / native / JNI / include to src/main/cpp directory (if cpp directory does not exist, create a new one)

At this time, your project directory should be as follows:

2, Establish the binding relationship between include and so library, CMakeLists.txt

cmake_minimum_required(VERSION 3.6)
set(CMAKE_VERBOSE_MAKEFILE on)
set(libs "${CMAKE_SOURCE_DIR}/src/main/jniLibs")
add_library(native-lib
        SHARED
        src/main/cpp/Hello.cpp ) #Add to.c source file

include_directories(src/main/cpp/include) #Add location of header file

#Add library objects and set reference mode
add_library(libopencv_java3 SHARED IMPORTED )
#Set the location of libopencv? Java3 Library
set_target_properties(libopencv_java3 PROPERTIES
        IMPORTED_LOCATION "${libs}/${ANDROID_ABI}/libopencv_java3.so")

target_link_libraries( # Specifies the target library.
        native-lib -ljnigraphics libopencv_java3
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib} )

3, Method to call opencv

First look at MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView contentTv = findViewById(R.id.tv_content);
        contentTv.setText(getVersion());
    }
    static {
        System.loadLibrary("native-lib");
    }
    public native String getVersion();
}

Hello.h will include the header file of opencv

#ifndef PRACTICLE_HELLO_H
#define PRACTICLE_HELLO_H

#include <jni.h>
#include <android/bitmap.h>
#include <opencv/cv.h>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_a6_1opencv2_MainActivity_getVersion(JNIEnv *env, jobject instance);
#endif //PRACTICLE_HELLO_H

Looking at Hello.cpp

#include <jni.h>
#include "Hello.h"

#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT jstring JNICALL
Java_com_example_a6_1opencv2_MainActivity_getVersion(JNIEnv *env, jobject instance) {
    const char *version = CV_VERSION;
    return env->NewStringUTF(version);;
} ;

#ifdef __cplusplus
}
#endif

4, Operation results

code:https://github.com/HumorSmith/NDKPraticle/tree/master/6_opencv2

Keywords: OpenCV SDK Android Java

Added by superpimp on Fri, 20 Dec 2019 22:03:31 +0200