Use of OpenCV in the Android environment -- environment construction (1)

1. OpenCV SDK download

openCv official website download
insert image description here

2. Create a C++ project in Android Studio

insert image description here

3. The so library of openCV is loaded into the project

  1. Create a new jniLibs under the src->main directory
  2. Download the so library of the platform corresponding to the opencv-4.1.0-android-sdk\OpenCV-android-sdk\sdk\native\libs directory of the openCv sdk

insert image description here

4. Load the header file

Load the header file of the header file directory include under the sdk directory into the project
insert image description here
insert image description here

5. Configure the CmakeLists file



cmake_minimum_required(VERSION 3.10.2)


project("open")


include_directories(include)

add_library(
        open


        SHARED


        native-lib.cpp)



find_library(
        log-lib

        log)


set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_SOURCE_DIR}/../jniLibs/${CMAKE_ANDROID_ARCH_ABI}")

target_link_libraries(
        open
        opencv_java4
        ${
    
    log-lib})

6. Gradle file configuration

 defaultConfig {
    
    
     
        externalNativeBuild {
    
    
            cmake {
    
    
                cppFlags ''
                abiFilters 'armeabi-v7a'
            }
        }

        ndk{
    
    
            abiFilters 'armeabi-v7a'
        }

    }

7. Verify openCV in jni

#include <jni.h>
#include <string>


#include "opencv2/opencv.hpp"

using namespace cv;

extern "C" JNIEXPORT jstring JNICALL
Java_com_xyx_open_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    
    
    std::string hello = "Hello from C++";

    Mat src=imread("sdcard/test_20220811161338.png");

    cvtColor(src,src,COLOR_BGR2GRAY);

    imwrite("/sdcard/test1.png",src);

    src.release();

    return env->NewStringUTF(hello.c_str());
}

8. Project compilation

Simple test, a photo in the sdcard directory is processed by opencv, the output is successful, and the use environment of openCV on Android is no problem.

Guess you like

Origin blog.csdn.net/baidu_31956557/article/details/127481571