Android: AndroidStudio使用OpenCV-Native

本文介绍在Android Studio(AS)中为C++ Native工程配置OpenCV的详细步骤。包括新建C++ native工程、下载安卓SDK并引入工程、修改cmake.list文件、在native.cpp里写测试代码等,还提醒配置中可能遇到的问题及解决思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考文章:https://blog.youkuaiyun.com/a390641326/article/details/80495754

1.首先建立一个C++Native工程

参考:https://blog.youkuaiyun.com/wishchin/article/details/90205661

在AS中新建一个C++ native工程,默认JNI库的名称为native-lib或者其他;

 

2.到opencv官网去下载一个版本的安卓SDK,https://opencv.org/releases/page/3/,解压到本地文件夹

 

3.点击File->New->Import Module…把OpenCV-JNI引入到工程中

OpenCV-android-sdk/sdk/native/jni
需要说明的是:在build.gradle(Module: app)的最后添加

task nativeLibsToJar(type: Jar, description: 'create a jar archive of the native libs') {
    destinationDir file("$buildDir/native-libs")
    baseName 'native-libs'
    from fileTree(dir: 'libs', include: '**/*.so')
    into 'lib/'
}

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn(nativeLibsToJar)
}

在dependencies{}中的最后添加

compile fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar')

4.修改cmake.list文件

修改工程src\app目录下的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)

#支持-std=gnu++11
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

#工程路径
set(Project_Path /home/wishchin/Proj/OpenSource/tensorApp/tfMobileNative)

# OpenCV Android SDK Path
set(OpenCV_Path /home/wishchin/Android/OpenCV-android-sdk/sdk/native)

include_directories(${OpenCV_Path}/jni/include)
#include_directories(/home/wishchin/Android/OpenCV-android-sdk/sdk/native/jni/include)

# 设置include文件夹的地址
#include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp)

# 设置opencv的动态库
#add_library(libopencv_java SHARED IMPORTED)
#set_target_properties(libopencv_java PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libopencv_java.so)

# set(OpenCV_STATIC ON)#1
# set(OpenCV_DIR /home/wishchin/Android/OpenCV-android-sdk/sdk/native/jni)#2
# find_package(OpenCV REQUIRED)#3

# 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.

add_library( # Sets the name of the library.
        native-lib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        native-lib.cpp
#        rec3D.cpp
        #         cameraCalib.cpp
        #         FrameProcess.cpp
        #         markerLocation.cpp
        #         p3pMy.cpp
        #         polynom_solverMy.cpp
                 CheckR.cpp
        #         BallTrack.cpp
        )

#动态方式加载
add_library( lib_opencv SHARED IMPORTED )

#引入libopencv_java.so文件
#set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${Project_Path}/app/src/main/jniLibs/${ANDROID_ABI}/libopencv_java.so)
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${Project_Path}/app/libs/${ANDROID_ABI}/libopencv_java.so)

# 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  #libopencv_java

        # 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.
        native-lib

本人的配置和原文的有一些不一样,不知道为什么.

Tips:

配置完工程,有一段时间若打开opencv源代码,发现找不到自身包含.

不用在意这些细节,也不必要在这个问题上浪费时间,AS习惯抽风,过一会就正常了.

 

5.在native.cpp里面写测试代码

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_tfmobilenative_MainActivity_testCV(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Congratulation,Boy !The native lib is success!";
    cv::Mat image;//
    image= cv::Mat::zeros(224,224,CV_8UC1);
    return env->NewStringUTF(hello.c_str());
}

 

6.测试结果

配置OpenCV,测了一天时间,蛋疼.

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值