欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android studio 3.4 与 opencv-4.1.0-android-sdk

程序员文章站 2022-05-16 08:41:55
...

本篇简要5步--展示如何简单调用 opencv

1.创建android工程

android studio 3.4 与 opencv-4.1.0-android-sdk

 

 

2.拷贝opencv 的 native文件目录

android studio 3.4 与 opencv-4.1.0-android-sdk

 

 

3.添加 CMakelist.txt

android studio 3.4 与 opencv-4.1.0-android-sdk

内容:

build.gradle


    externalNativeBuild {

        cmake {

            path file('CMakeLists.txt')

        }

    }



-------------

CMakelists.txt


# For more information about using CMake with Android Studio, read the

# documentation: https://d.android.com/studio/projects/add-native-code.html



# Sets the minimum version of CMake required to build the native library.



cmake_minimum_required(VERSION 3.4.1)



# Creates and names a library, sets it as either STATIC

# or SHARED, and provides the relative paths to its source code.

# You can define multiple libraries, and CMake builds them for you.

# Gradle automatically packages shared libraries with your APK.




include_directories(../native/jni/include)  #添加头文件位置

aux_source_directory(src/main/cpp MainCPP)



add_library( # Sets the name of the library.

             test_lib #.so库名 可自定义



             # Sets the library as a shared library.

             SHARED



             # Provides a relative path to your source file(s).

            ${MainCPP} ) #源文件所在目录

# Searches for a specified prebuilt library and stores the path as a

# variable. Because CMake includes system libraries in the search path by

# default, you only need to specify the name of the public NDK library

# you want to add. CMake verifies that the library exists before

# completing its build.

find_library( # Sets the name of the path variable.

              log-lib

              # Specifies the name of the NDK library that

              # you want CMake to locate.

              log )

# Specifies libraries CMake should link to your target library. You

# can link multiple libraries, such as libraries you define in this

# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.

                       test_lib #.so库名 可自定义

                       # Links the target library to the log library

                       # included in the NDK.

                       ${log-lib} )


 

4.添加cpp目录和内容,java测试类

android studio 3.4 与 opencv-4.1.0-android-sdk

内容:

test.java

package com.example.myapplication;

public class test {

    static {

        try {

            System.loadLibrary("test_lib");

            System.out.println("load:");



        } catch (Throwable e) {

            System.out.println("err :"+e.toString());

        }

    }

    public static  native String img2gray(String str);
}

--------
com_example_myapplication_test.h


/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_myapplication_test */

#ifndef _Included_com_example_myapplication_test
#define _Included_com_example_myapplication_test
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_example_myapplication_test
 * Method:    img2gray
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_example_myapplication_test_img2gray
  (JNIEnv *, jclass, jstring);

#ifdef __cplusplus
}
#endif
#endif



--------
com_example_myapplication_test.cpp

#include "com_example_myapplication_test.h"

#include <opencv2/opencv.hpp>

#include <Android/log.h>
#include <Android/asset_manager.h>
#include <Android/asset_manager_jni.h>

#define TAG "cn.linjk.opencvtest.jni"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,TAG ,__VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,TAG ,__VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,TAG ,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,TAG ,__VA_ARGS__)
#define LOGF(...) __android_log_print(ANDROID_LOG_FATAL,TAG ,__VA_ARGS__)

extern "C" {

JNIEXPORT jstring JNICALL Java_com_example_myapplication_test_img2gray
    (JNIEnv *env , jclass objClass, jstring imgFilePath) {

        LOGI("OpenCV version: %s", CV_VERSION);
        return imgFilePath;
    }

}

 

5.调用与测试

android studio 3.4 与 opencv-4.1.0-android-sdk

android studio 3.4 与 opencv-4.1.0-android-sdk

 

最后附上参考资料:


Android studio项目中使用JNI调用opencv的c++程序 https://blog.csdn.net/iamqianrenzhan/article/details/83013342
Android平台下OpenCV移植与使用---基于C/C++   https://www.cnblogs.com/linjk/p/6063936.html
--erro   javah -jni   字符错误   --  编码保存为ansi

多目录工程的CmakeLists.txt编写 --https://blog.csdn.net/ktigerhero3/article/details/70313350

windows - AndroidStudio3.4 与 .so    --https://blog.csdn.net/glc22/article/details/90716726

相关标签: android opencv