สร้าง OpenCV สำหรับ Android และใช้กับ NDK

บริบท : ฉันกำลังพัฒนาแอปบน Android Studio สำหรับแว่นตาเสมือนจริง Moverio BT 200 ฉันใช้ OpenCV และโดยเฉพาะโมดูล arUco ของไลบรารี โมดูลนี้จะต้องใช้กับ NDK นอกจากนี้ยังไม่ได้อยู่ในรุ่นที่เสถียร ดังนั้นฉันจึงรวบรวมไลบรารีด้วยตัวเอง (ใช้คำแนะนำนี้: https://zami0xzami.wordpress.com/2016/03/17/building-opencv-for-android-from-source/) การก่อสร้างห้องสมุดดำเนินไปด้วยดี หลังจากนั้นฉันก็สร้างโปรเจ็กต์ android studio (customOCVtest) ฉันทำแบบที่ฉันเคยทำเมื่อใช้ OpenCV กับ Android Studio และ NDK ยกเว้นคราวนี้เป็นกับรุ่นที่กำหนดเอง ฉันตรวจสอบว่าโหลดไลบรารีอย่างถูกต้องหรือไม่:

private static final String OCVdevTAG = "OCVmainAct";

static {
    System.loadLibrary("native-lib");
    if(!OpenCVLoader.initDebug()) {
        Log.d(OCVdevTAG, "OpenCV not loaded");
    } else {
        Log.d(OCVdevTAG, "OpenCV loaded");
    }
}

ห้องสมุดถูกโหลดจริง ๆ เมื่อฉันสร้าง

ตอนนี้ปัญหามา: เมื่อฉันพยายามใช้โมดูล arUco ในโค้ดเนทีฟของฉัน (นี่คือ Native-lib.cpp ของฉัน):

#include <jni.h>
#include <string>
#include <opencv2/aruco.hpp>

extern "C" {
jstring
Java_com_jambonsama_customocvtest_MainActivity_stringFromJNI(
    JNIEnv *env,
    jobject /* this */) {
std::string hello = "Hello from C++";
cv::Ptr<cv::aruco::Dictionary> dict = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_250);
cv::Mat marker;
cv::aruco::drawMarker(dict, 25, 200, marker, 1);
return env->NewStringUTF(hello.c_str());
}
}

การซิงค์ gradle ใช้งานได้ แต่ฉันไม่สามารถสร้างได้ ฉันได้รับข้อผิดพลาดต่อไปนี้:

Error:FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
Error while executing 'C:\Users\JambonSama\AppData\Local\Android\Sdk\cmake\3.6.3155560\bin\cmake.exe' with arguments {--build C:\Users\JambonSama\AndroidStudioProjects\customOCVtest\app\.externalNativeBuild\cmake\debug\mips64 --target native-lib}
[1/1] Linking CXX shared library ..\..\..\..\build\intermediates\cmake\debug\obj\mips64\libnative-lib.so
FAILED: cmd.exe /C "cd . && C:\Users\JambonSama\AppData\Local\Android\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe  -target mips64el-none-linux-android -gcc-toolchain C:/Users/JambonSama/AppData/Local/Android/sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/windows-x86_64 --sysroot=C:/Users/JambonSama/AppData/Local/Android/sdk/ndk-bundle/platforms/android-21/arch-mips64 -fPIC -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -fno-exceptions -fno-rtti -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -fno-exceptions -fno-rtti  -O0 -fno-limit-debug-info -O0 -fno-limit-debug-info  -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ..\..\..\..\build\intermediates\cmake\debug\obj\mips64\libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o  C:/Libs/opencv_src/opencv/platforms/build_android_arm/install/sdk/native/libs/armeabi-v7a/libopencv_core.a C:/Libs/opencv_src/opencv/platforms/build_android_arm/install/sdk/native/libs/armeabi-v7a/libopencv_aruco.a -llog -lm "C:/Users/JambonSama/AppData/Local/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/mips64/libgnustl_static.a" && cd ."
C:/Users/JambonSama/AppData/Local/Android/sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/windows-x86_64/lib/gcc/mips64el-linux-android/4.9.x/../../../../mips64el-linux-android/bin\ld: C:/Libs/opencv_src/opencv/platforms/build_android_arm/install/sdk/native/libs/armeabi-v7a/libopencv_core.a(alloc.cpp.o): Relocations in generic ELF (EM: 40)
  C:/Users/JambonSama/AppData/Local/Android/sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/windows-x86_64/lib/gcc/mips64el-linux-android/4.9.x/../../../../mips64el-linux-android/bin\ld: C:/Libs/opencv_src/opencv/platforms/build_android_arm/install/sdk/native/libs/armeabi-v7a/libopencv_core.a(alloc.cpp.o): Relocations in generic ELF (EM: 40)
  C:/Users/JambonSama/AppData/Local/Android/sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/windows-x86_64/lib/gcc/mips64el-linux-android/4.9.x/../../../../mips64el-linux-android/bin\ld: C:/Libs/opencv_src/opencv/platforms/build_android_arm/install/sdk/native/libs/armeabi-v7a/libopencv_core.a(alloc.cpp.o): Relocations in generic ELF (EM: 40)
  C:/Users/JambonSama/AppData/Local/Android/sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/windows-x86_64/lib/gcc/mips64el-linux-android/4.9.x/../../../../mips64el-linux-android/bin\ld: C:/Libs/opencv_src/opencv/platforms/build_android_arm/install/sdk/native/libs/armeabi-v7a/libopencv_core.a(alloc.cpp.o): Relocations in generic ELF (EM: 40)
  C:/Users/JambonSama/AppData/Local/Android/sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/windows-x86_64/lib/gcc/mips64el-linux-android/4.9.x/../../../../mips64el-linux-android/bin\ld: C:/Libs/opencv_src/opencv/platforms/build_android_arm/install/sdk/native/libs/armeabi-v7a/libopencv_core.a(alloc.cpp.o): Relocations in generic ELF (EM: 40)
  C:/Users/JambonSama/AppData/Local/Android/sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/windows-x86_64/lib/gcc/mips64el-linux-android/4.9.x/../../../../mips64el-linux-android/bin\ld: C:/Libs/opencv_src/opencv/platforms/build_android_arm/install/sdk/native/libs/armeabi-v7a/libopencv_core.a(alloc.cpp.o): Relocations in generic ELF (EM: 40)
  C:/Users/JambonSama/AppData/Local/Android/sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/windows-x86_64/lib/gcc/mips64el-linux-android/4.9.x/../../../../mips64el-linux-android/bin\ld: C:/Libs/opencv_src/opencv/platforms/build_android_arm/install/sdk/native/libs/armeabi-v7a/libopencv_core.a(alloc.cpp.o): Relocations in generic ELF (EM: 40)
  C:/Users/JambonSama/AppData/Local/Android/sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/windows-x86_64/lib/gcc/mips64el-linux-android/4.9.x/../../../../mips64el-linux-android/bin\ld: C:/Libs/opencv_src/opencv/platforms/build_android_arm/install/sdk/native/libs/armeabi-v7a/libopencv_core.a(alloc.cpp.o): Relocations in generic ELF (EM: 40)
  C:/Users/JambonSama/AppData/Local/Android/sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/windows-x86_64/lib/gcc/mips64el-linux-android/4.9.x/../../../../mips64el-linux-android/bin\ld: C:/Libs/opencv_src/opencv/platforms/build_android_arm/install/sdk/native/libs/armeabi-v7a/libopencv_core.a(alloc.cpp.o): Relocations in generic ELF (EM: 40)
  C:/Libs/opencv_src/opencv/platforms/build_android_arm/install/sdk/native/libs/armeabi-v7a/libopencv_core.a: error adding symbols: File in wrong format
  clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
  ninja: build stopped: subcommand failed.


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

โดยเฉพาะอย่างยิ่งข้อความแสดงข้อผิดพลาดนี้บอกฉันว่าฉันพยายามสร้างด้วยอาร์กิวเมนต์ต่อไปนี้:

{--build C:\Users\JambonSama\AndroidStudioProjects\customOCVtest\app\.externalNativeBuild\cmake\debug\mips64 --target native-lib}

อาร์กิวเมนต์นี้ถูกสร้างขึ้นโดยอัตโนมัติโดย AS และฉันไม่พบไฟล์ที่ถูกส่งผ่านไป ฉันเชื่อว่าถ้าฉันหามันได้ ฉันจะสามารถสร้างแขน (ซึ่งเป็นสิ่งที่ฉันต้องการ) แทน mips ประเด็นคือผมหามันไม่เจอ (และฉันไม่แน่ใจด้วยซ้ำว่านั่นคือสิ่งที่ฉันกำลังมองหาจริงๆ ฉันแค่พยายามสร้างโปรเจ็กต์ของฉันสำหรับแว่นตาของฉัน)

หลังจากนั้นคือ CMakeLists.txt ของฉัน:

# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.

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 it 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).
             # Associated headers in the same location as their source
             # file are automatically included.
             src/main/cpp/native-lib.cpp )

include_directories(C:/Libs/opencv_src/opencv/platforms/build_android_arm/install/sdk/native/jni/include)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included 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 the
# build script, prebuilt third-party libraries, or system libraries.


target_link_libraries( # Specifies the target library.
                       native-lib
                       C:/Libs/opencv_src/opencv/platforms/build_android_arm/install/sdk/native/libs/armeabi-v7a/libopencv_core.a
                       C:/Libs/opencv_src/opencv/platforms/build_android_arm/install/sdk/native/libs/armeabi-v7a/libopencv_aruco.a

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

นี่คือแอพของฉัน build.gradle :

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.1"
    defaultConfig {
        applicationId "com.jambonsama.customocvtest"
        minSdkVersion 14
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
    sourceSets { main { jni.srcDirs = ['src/main/jni', 'src/main/jniLibs/'] } }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.1'
    testCompile 'junit:junit:4.12'
    compile project(':openCVLibrary310dev')
}

และ openCVLibrary310dev build.gradle ของฉัน

apply plugin: 'com.android.library'

android {
    compileSdkVersion 25
    buildToolsVersion "23.0.2"

    defaultConfig {
        ndk {
            abiFilter("armeabi-v7a")
        }
        minSdkVersion 14
        targetSdkVersion 25
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

สิ่งที่ฉันลองโดยส่วนใหญ่ก็แค่คัดลอกสิ่งที่ช่วยผู้อื่นแก้ไขปัญหาที่ดูเหมือนคล้ายกับของฉัน:

#set(CMAKE_SYSTEM_NAME Android)

or

#set(CMAKE_TOOLCHAIN_FILE "Toolchain file" CACHE FILEPATH "C:/Libs/opencv_src/opencv/platforms/android.toolchain.cmake")

or

#set( CMAKE_CXX_COMPILER "C:/Libs/android-ndk-r13b/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64/bin/arm-linux-androideabi-g++.exe" )

ใน CMakeLists.txt

และ

splits {
    abi {
        enable true
        reset()
        include 'armeabi-v7a'
        universalApk true
    }
}

or

tasks.getByPath(":app:linkMipsDebugRemoteDesktopSharedLibrary").enabled = false

ใน build.gradle (ฉันลองทั้งสองอย่างเพราะฉันไม่รู้ว่าอันไหนควรจะมี)

ยกเว้นโค้ดบิตสุดท้าย (task.getByPath...) การซิงค์ใช้งานได้ แต่ฉันไม่สามารถสร้างได้ อันสุดท้ายฉันไม่สามารถซิงค์ได้เลย

ฉันยังพยายามสร้างไลบรารี่ใหม่สำหรับ mips เนื่องจากสถาปัตยกรรมดูเหมือนจะเป็นปัญหา แต่ฉันไม่รู้ว่าต้องทำอย่างไร

ฉันจะหาไฟล์ที่มีอาร์กิวเมนต์ต่อไปนี้ได้ที่ไหน:

{--build C:\Users\JambonSama\AndroidStudioProjects\customOCVtest\app\.externalNativeBuild\cmake\debug\mips64 --target native-lib}

ผ่านไปตอนสร้าง? และ/หรือฉันจะทำให้ Android Studio สร้างโปรเจ็กต์ของฉันสำหรับสถาปัตยกรรมแบบแขนได้อย่างไร


person JambonSama    schedule 03.12.2016    source แหล่งที่มา


คำตอบ (1)


ดังนั้นปัญหาสถาปัตยกรรม mips สามารถแก้ไขได้โดยเพิ่มโค้ดต่อไปนี้ลงใน gradle.build ของแอปที่ส่วนท้ายของบล็อก android:

productFlavors {
    armv7 {
        ndk {
            abiFilter "armeabi-v7a"
        }
    }
    fat
}

แต่แล้วปัญหาอื่นๆ ก็เกิดขึ้น น่าสังเกตที่ไลบรารีถูกสร้างขึ้นด้วยตัวเลือกแคโรทีนซึ่ง Android Studio ไม่ชอบและจากนั้น gzlib ก็พลาดดังนั้น Android Studio จึงไม่สามารถสร้างและ / หรือรันได้ ดังนั้นหลังจากการทดลองหลายครั้ง ในที่สุดฉันก็บรรลุสิ่งที่ฉันต้องการได้ นั่นคือการคอมไพล์ข้าม OpenCV พร้อมโมดูลพิเศษสำหรับ Android บน Windows และสร้างโปรเจ็กต์ AS ด้วยไลบรารีในตัว เพื่อให้มันใช้งานได้ ฉันบันทึกกระบวนการทั้งหมดของฉัน ตั้งแต่การดาวน์โหลดซอร์ส openCV จนกระทั่งเรียกใช้แอปพลิเคชันของฉันบนอุปกรณ์ที่ออกแบบโดยแขน ฉันจะคัดลอกและวางที่นี่สำหรับทุกคนที่ต้องการทำเช่นนั้นเช่นกัน คำแนะนำต่อไปนี้อิงตาม บทช่วยสอนนี้เป็นหลัก (ขอบคุณ Zamrath Nizam ผู้เขียน) ปัญหาเดียวของบทช่วยสอนนั้นก็คือมันอาจจะเก่าไปหน่อย ดังนั้นจึงจำเป็นต้องแก้ไขตัวเลือก/ขั้นตอนบางอย่าง

CROSS COMPILE OPENCV จากแหล่งที่มาพร้อมโมดูลพิเศษสำหรับ Android จาก WINDOWS10

* FIRST : ข้อกำหนดเบื้องต้น *

  • ดาวน์โหลด OpenCV และแตกไฟล์ (พูดที่ '../opencv-source')
  • ดาวน์โหลดโมดูลพิเศษของ OpenCV และแตกไฟล์ (พูดที่ '../opencv-contrib-source')
  • ดาวน์โหลด Android NDK (พูดที่ '../ndk-dir')
  • ดาวน์โหลด CMake (เพื่อพูดว่า '../cmake-dir') และ MinGW (เพื่อพูดว่า '../mingw-dir')
  • ติดตั้ง Android สตูดิโอ

* SECOND : กำหนดค่าด้วย CMake *

  • ไปที่ '../opencv-source/platforms' และสร้างโฟลเดอร์ชื่อ 'build_android_arm'
  • in CMake, fill the following first two fields with the following paths :
    1. where is the source code : '../opencv-source'
    2. ตำแหน่งที่จะสร้างไบนารี: '../opencv-source/platforms/build_android_arm'
  • add the following options via the 'Add Entry' :
    1. ANDROID_NDK, type 'path', value '../ndk-dir'
    2. ANDROID_NDK_HOST_X64 พิมพ์ 'บูล' ค่า 1
    3. CMAKE_TOOLCHAIN_FILE พิมพ์ 'เส้นทาง' ค่า '../opencv-source/platforms/android/android.toolchain.cmake'
  • กด 'กำหนดค่า'
  • เลือก 'MinGW Makefiles' เป็นคอมไพเลอร์
  • เลือก 'ระบุไฟล์ toolchain สำหรับการคอมไพล์ข้าม'
  • press 'Next', and 'Finish'
    • note 1 : As long as you've got no error message, everything's good. Don't worry about the warning messages (like CMake telling you you're doing deprecated stuff).
    • หมายเหตุ 2 : หากคุณไม่สามารถกำหนดค่าในขั้นตอนนั้นได้ ลองอ่านประเด็นถัดไป อาจช่วยได้
  • เปลี่ยนตัวเลือกเพิ่มเติมต่อไปนี้ผ่านทางช่อง 'ค้นหา' :

    1. EXTRA_MODULE_PATH, type 'path', value '../opencv-contrib-source/modules'
    2. WITH_CAROTENE พิมพ์ 'บูล' ค่า 0
    3. BUILD_ZLIB, type 'bool', value 1
      • note : You MUST have configured already once before this step, because the variables created before are regrouped under the group 'Ungrouped entries', while the following variables are CMake automatically generated variables, and NEED to be grouped in the right groups (which is NOT 'Ungrouped entries').
  • ตรวจสอบว่าตัวเลือกต่อไปนี้ได้รับการตั้งค่าอย่างถูกต้อง (ผ่านช่อง 'ค้นหา'):

    1. ANDROID_NDK_HOST_X64, type 'bool', value 1
    2. CMAKE_MAKE_PROGRAM พิมพ์ 'path' ค่า '../mingw-dir/bin/mingw32-make.exe' ตัวเลือกนี้ จริงๆ แล้วฉันไม่มีมันในการกำหนดค่า CMake ของฉัน หากเมื่อคุณกด 'กำหนดค่า' มันใช้งานไม่ได้ และคุณไม่มีตัวเลือกนี้ คุณควรลองเพิ่มเข้าไป (แต่ฉันไม่รู้ว่าต้องทำอย่างไร) หากคุณไม่มีปัญหาใดๆ เมื่อคุณกด 'กำหนดค่า' ก็ไม่ต้องกังวลกับตัวแปรนั้น การตรวจสอบนี้มาจากบทช่วยสอนดั้งเดิมที่ลิงก์ด้านบน
    3. CMAKE_TOOLCHAIN_FILE พิมพ์ 'เส้นทาง' ค่า '../opencv-source/platforms/android/android.toolchain.cmake'
  • กด 'กำหนดค่า'

  • เลือก 'MinGW Makefiles' เป็นคอมไพเลอร์
  • เลือก 'ระบุไฟล์ toolchain สำหรับการคอมไพล์ข้าม'
  • กด 'ถัดไป' และ 'เสร็จสิ้น'
  • กด 'สร้าง'

* THIRD : คอมไพล์ด้วย mingw *

  • ไปที่ '../mingw-dir/msys/1.0' และเรียกใช้ไฟล์ bash 'msys'
  • นำทางไปยัง '../opencv-source/platforms/android_arm'
  • เรียกใช้คำสั่ง 'mingw32-make'
  • เรียกใช้คำสั่ง 'mingw32-make install'

* ที่สี่ : โครงการ Android *

  • launch Android Studio and create a new project :
    1. select File -> New -> New Project...
    2. กรอก 'ชื่อแอปพลิเคชัน' ด้วยสมมติว่า 'cOCV'
    3. ทำเครื่องหมายที่ช่อง 'รวมการสนับสนุน C ++'
    4. คลิก 'ถัดไป'
    5. เลือก SDK ขั้นต่ำของคุณ (เช่น API 14: Android 4.0 (IceCreamSandwich)
    6. คลิก 'ถัดไป', 'ถัดไป' และ 'เสร็จสิ้น'
  • ไปที่ไฟล์ -> ใหม่ -> โมดูลนำเข้า...

    1. provide '../opencv-source/platforms/android_arm/install/sdk/java'
    2. คลิก 'ถัดไป' และ 'เสร็จสิ้น'
  • เปลี่ยนเป้าหมายในไฟล์ build.gradle ในโฟลเดอร์ openCVLibraryXXX (โมดูลที่นำเข้า):

    1. compileSdkVersion and targetSdkVersion should be the same, greater or equal to 23
    2. minSdkVersioon ควรเหมือนกับที่ระบุไว้เมื่อสร้างโปรเจ็กต์
  • ในไฟล์ build.gradle ในโฟลเดอร์ openCVLibraryXXX (โมดูลที่นำเข้า):

    1. replace 'apply plugin: com.android.application' as, 'apply plugin: com.android.library'
    2. remove the line 'applicationId "org.opencv"'
      • note : That last step is done in order to avoid following error : 'unspecified on project app resolves to an APK archive which is not supported as a compilation dependency'.
  • เพิ่มไลบรารีที่นำเข้าเป็นการพึ่งพาโมดูล 'แอป' ในไฟล์ -> 'โครงสร้างโครงการ'

  • สร้างโฟลเดอร์ jniLibs ใน 'app/src/main' :

    1. right click 'app' in the Android view on the left menu
    2. คลิก 'โฟลเดอร์ใหม่-โฟลเดอร์ JNI'
    3. ตรวจสอบ 'เปลี่ยนตำแหน่งโฟลเดอร์'
    4. ตั้งค่า 'ชุดแหล่งที่มาเป้าหมายเป็น 'app/src/main/jniLibs'
  • คัดลอกไลบรารีของ OpenCV จาก 'opencv-source/platforms/android_arm/install/sdk/native/libs' ลงในโฟลเดอร์ที่สร้างขึ้นใหม่ (jniLibs) ภายในโฟลเดอร์ 'AndroidStudioProjects/cOCV/app/src/main/jniLibs'

    • note : For example, my AndroidStudioProjects folder is located at 'C:\Users\JambonSama\AndroidStudioProjects'.
  • ใน 'opencv-contrib-source/modules/module_you_desperately_need/CMakeLists.txt' เปลี่ยน 'ocv_define_module (module_you_desperately_need opencv โมดูลอื่น ๆ )' สำหรับ 'ocv_define_module (module_you_desperately_need opencv โมดูลอื่น ๆ WRAP java)'

    • note 1 : THIS STEP MAY BE USELESS, because WRAP java is probably already written in the file.
    • หมายเหตุ 2 : หากเขียน 'python' ด้วยเช่นกัน ไม่เป็นไร คุณปล่อยให้มันเขียนในตำแหน่งที่มันอยู่
  • ใน CMakeLists.txt ให้เพิ่ม:

    1. สองบรรทัดต่อไปนี้หลังบล็อก 'add_library' และก่อนบล็อก 'find_library' :

       include_directories(../opencv_src/opencv/platforms/build_android_armn/install/sdk/native/jni/include)
       link_directories(../AndroidStudioProjects/cOCVn/app/src/main/jniLibs/armeabi-v7a)
      
      • note : These two lines pretty straight-forwardly give the paths for the include and link directories
    2. บรรทัดต่อไปนี้หลังบล็อก 'find_library' และก่อนบล็อก 'target_link_libraries' :

       file(GLOB PARTYLIBS "../opencv_src/opencv/platforms/build_android_armn/install/sdk/native/3rdparty/libs/armeabi-v7a/*.a")
       file(GLOB CVLIBS  "../opencv_src/opencv/platforms/build_android_armn/install/sdk/native/libs/armeabi-v7a/*.a")
      
      • note : These are for easier linking commands, see next point.
    3. เส้นทางต่อไปนี้ ตามที่เขียนไว้ ใน 'target_link_libraries' หลังตัวแปร 'native-lib' และก่อนหน้า '${log-lib}' หนึ่ง:

       ${CVLIBS}
       ${PARTYLIBS}
       ${CVLIBS}
      
      • note : THAT IS THE TRICKY PART : because of cyclical dependencies, you have to write CVLIBS, PARTYLIBS, and then CVLIBS a second time, otherwise you won't stop having linking errors. By now, everything should be linked.
  • ใน gradle.build ของแอป ให้เพิ่มโค้ดต่อไปนี้:

         productFlavors {
             armv7 {
                 ndk {
                     abiFilter "armeabi-v7a"
                 }
             }
             fat
        }
    

    ที่ส่วนท้ายของบล็อก Android

    • note : This prevents the mips64 architecture error at build, by specifying once and for all the arm architecture
  • ใน Native-lib.cpp :

    1. ลองใช้รหัสต่อไปนี้ในฟังก์ชัน 'stringFromJNI' (อันนี้ถูกสร้างขึ้นโดยอัตโนมัติโดย AS เมื่อสร้างโครงการ)

       cv::Mat test;
       cv::VideoCapture camera;
       camera.open(0);
       cv::Ptrcv::aruco::Dictionary dict = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_250);
       cv::Mat marker;
       cv::aruco::drawMarker(dict, 25, 200, marker, 1);
       std::string hello = "Hello from C++";
       return env-NewStringUTF(hello.c_str());
      
    2. อย่าลืมสิ่งต่อไปนี้รวมถึง:

       #include jni.h
       #include string
       #include opencv2/aruco.hpp
       #include opencv2/videoio.hpp
      
      • note : Because the string is not generated before the end of the function, you know, when you test, that if the string is indeed displayed on screen, the function has been gone through without problem, and that you're good (since the above provided code use some modules that are not on the stable realease as of 08/12/2016 (8th of december, I write dates with a dd/mm/yyyy format).
  • เวลาในการทดสอบ :

    1. sync the gradle
    2. สร้าง
    3. ทำงานบนอุปกรณ์แขน

คุณไปได้แล้ว \ o \\ O // o /

หมายเหตุเพิ่มเติม :

  • ไฟล์ gradle.build ทั้งหมดและไฟล์ CMakeLists.txt สามารถพบได้ง่ายบน Android View ในเมนูทางด้านซ้ายของหน้าต่างใน AS
  • โปรดจำไว้ว่าหากคุณพบข้อผิดพลาดในการนำทางในหน้าต่างคำสั่ง ซึ่งบางที '/' ของคุณควรเป็น '\' หรือในทางกลับกัน (คำสั่ง 'cd' เพื่อนำทางในหน้าต่างคำสั่ง)
  • ฉันทำงานบน Windows10
  • หากคุณไม่แน่ใจว่าคุณควรเห็นอะไรบนหน้าจอทีละขั้นตอน บทช่วยสอนที่ฉันพูดถึงตอนต้นของคำตอบควรช่วยให้คุณมีความคิดที่ดีว่าคุณควรเห็นอะไรจนกระทั่งการสร้างโปรเจ็กต์ AS จากนั้น คุณสามารถดูบทแนะนำนี้ ซึ่งอธิบายวิธีการตั้งค่า OpenCV SDK ในโปรเจ็กต์ Android Studio สำหรับการเปิดตัวอย่างเป็นทางการ ขั้นตอนที่ฉันให้ไว้แตกต่างจากบทช่วยสอนทั้งสองขั้นตอน แต่ฉันใส่ไว้ที่นี่ เนื่องจากสามารถช่วยให้คุณรู้ว่าหน้าจอ / หน้าต่างของคุณควรมีลักษณะอย่างไร
person JambonSama    schedule 08.12.2016
comment
เนื่องจาก CMake เป็นกลไกการสร้างที่ต้องการใน Android Studio มีวิธีทำทั้งหมดนี้จากภายใน AS หรือไม่ ตัวอย่างเช่น หากมีใครเริ่มต้นด้วยบิลด์เหมือนที่นี่: stackoverflow.com/questions/38958876/ ซึ่งมีการคอมไพล์แบบเนทีฟเกิดขึ้น เพิ่มโมดูลโดยไม่มี CMake แยกต่างหากและ MinGW แยกกันได้อย่างไร - person Dale; 19.05.2017
comment
ฉันต้องใช้สิ่งนี้ CMAKE_TOOLCHAIN_FILE: NDK_HOME\build\cmake\android.toolchain.cmake - person Tomáš Zato - Reinstate Monica; 11.09.2020