日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android: AndroidStudio使用OpenCV-Native

發(fā)布時間:2023/12/31 Android 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android: AndroidStudio使用OpenCV-Native 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

參考文章:https://blog.csdn.net/a390641326/article/details/80495754

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

參考:https://blog.csdn.net/wishchin/article/details/90205661

在AS中新建一個C++ native工程,默認(rèn)JNI庫的名稱為native-lib或者其他;

?

2.到opencv官網(wǎng)去下載一個版本的安卓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)# 設(shè)置include文件夾的地址 #include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp)# 設(shè)置opencv的動態(tài)庫 #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.cppCheckR.cpp# BallTrack.cpp)#動態(tài)方式加載 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源代碼,發(fā)現(xiàn)找不到自身包含.

不用在意這些細(xì)節(jié),也不必要在這個問題上浪費時間,AS習(xí)慣抽風(fēng),過一會就正常了.

?

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.測試結(jié)果

配置OpenCV,測了一天時間,蛋疼.

?

?

總結(jié)

以上是生活随笔為你收集整理的Android: AndroidStudio使用OpenCV-Native的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。