Giter VIP home page Giter VIP logo

vlsomers / native-opencv-android-template Goto Github PK

View Code? Open in Web Editor NEW
277.0 14.0 66.0 959 KB

A tutorial for setting up OpenCV 4.6.0 (and other 4.x.y version) for Android in Android Studio with Native Development Kit (NDK) support for C++ development.

License: MIT License

Kotlin 41.81% CMake 14.59% C++ 7.59% Java 36.01%
opencv4android opencv android computer-vision ndk jni kotlin kotlin-android android-studio java

native-opencv-android-template's People

Contributors

alexcohn avatar vlsomers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

native-opencv-android-template's Issues

Reducing app size when not used

If I add opencv to my project but don't use it, it adds 50MB of size to my app. Is there a way to remove unused functions in C++?

Does not seem to work with OpenCV 4.9.0 or a bit lower

I have followed the instructions to start using this project, including changing the location of where you have installed OpenCV, so that the project can find it easily, amongst other things. However, the app refuses to build, and throws out so many errors, mostly relating to Ninja:

Task :app:buildCMakeDebug[arm64-v8a] FAILED
C/C++: ninja: error: 'C:/Users/AhmedWK/Documents/OpenCv-4.9.0/OpenCV-android-sdk/sdk/native/jni/libs/arm64-v8a/libopencv_java4.so', needed by 'D:/USERS_WINDOWS/user_AhmedWk/AndroidProjects/OPENCV_TEST/app/build/intermediates/cxx/Debug/422x5r34/obj/arm64-v8a/libnative-lib.so', missing and no known rule to make it

ninja: error: 'C:/Users/AhmedWK/Documents/OpenCv-4.9.0/OpenCV-android-sdk/sdk/native/jni/libs/arm64-v8a/libopencv_java4.so', needed by 'D:/USERS_WINDOWS/user_AhmedWk/AndroidProjects/OPENCV_TEST/app/build/intermediates/cxx/Debug/422x5r34/obj/arm64-v8a/libnative-lib.so', missing and no known rule to make it

Task :app:buildCMakeDebug[armeabi-v7a] FAILED
C/C++: ninja: error: 'C:/Users/AhmedWK/Documents/OpenCv-4.9.0/OpenCV-android-sdk/sdk/native/jni/libs/armeabi-v7a/libopencv_java4.so', needed by 'D:/USERS_WINDOWS/user_AhmedWk/AndroidProjects/OPENCV_TEST/app/build/intermediates/cxx/Debug/422x5r34/obj/armeabi-v7a/libnative-lib.so', missing and no known rule to make it

ninja: error: 'C:/Users/AhmedWK/Documents/OpenCv-4.9.0/OpenCV-android-sdk/sdk/native/jni/libs/armeabi-v7a/libopencv_java4.so', needed by 'D:/USERS_WINDOWS/user_AhmedWk/AndroidProjects/OPENCV_TEST/app/build/intermediates/cxx/Debug/422x5r34/obj/armeabi-v7a/libnative-lib.so', missing and no known rule to make it

Task :app:buildCMakeDebug[x86] FAILED
C/C++: ninja: error: 'C:/Users/AhmedWK/Documents/OpenCv-4.9.0/OpenCV-android-sdk/sdk/native/jni/libs/x86/libopencv_java4.so', needed by 'D:/USERS_WINDOWS/user_AhmedWk/AndroidProjects/OPENCV_TEST/app/build/intermediates/cxx/Debug/422x5r34/obj/x86/libnative-lib.so', missing and no known rule to make it

ninja: error: 'C:/Users/AhmedWK/Documents/OpenCv-4.9.0/OpenCV-android-sdk/sdk/native/jni/libs/x86/libopencv_java4.so', needed by 'D:/USERS_WINDOWS/user_AhmedWk/AndroidProjects/OPENCV_TEST/app/build/intermediates/cxx/Debug/422x5r34/obj/x86/libnative-lib.so', missing and no known rule to make it

Task :app:buildCMakeDebug[x86_64] FAILED
C/C++: ninja: error: 'C:/Users/AhmedWK/Documents/OpenCv-4.9.0/OpenCV-android-sdk/sdk/native/jni/libs/x86_64/libopencv_java4.so', needed by 'D:/USERS_WINDOWS/user_AhmedWk/AndroidProjects/OPENCV_TEST/app/build/intermediates/cxx/Debug/422x5r34/obj/x86_64/libnative-lib.so', missing and no known rule to make it

ninja: error: 'C:/Users/AhmedWK/Documents/OpenCv-4.9.0/OpenCV-android-sdk/sdk/native/jni/libs/x86_64/libopencv_java4.so', needed by 'D:/USERS_WINDOWS/user_AhmedWk/AndroidProjects/OPENCV_TEST/app/build/intermediates/cxx/Debug/422x5r34/obj/x86_64/libnative-lib.so', missing and no known rule to make it


Furthermore, MainActivity has 8 errors, down from an initial 33 when I first cloned the project, the build output shows the following which I have tried to solve, to no avail:

Task :app:compileDebugKotlin FAILED
e: D:\USERS_WINDOWS\user_AhmedWk\AndroidProjects\OPENCV_TEST\app\src\main\kotlin\com\example\nativeopencvandroidtemplate\MainActivity.kt: (12, 27): Unresolved reference: BaseLoaderCallback
e: D:\USERS_WINDOWS\user_AhmedWk\AndroidProjects\OPENCV_TEST\app\src\main\kotlin\com\example\nativeopencvandroidtemplate\MainActivity.kt: (14, 27): Unresolved reference: LoaderCallbackInterface
e: D:\USERS_WINDOWS\user_AhmedWk\AndroidProjects\OPENCV_TEST\app\src\main\kotlin\com\example\nativeopencvandroidtemplate\MainActivity.kt: (22, 44): Unresolved reference: BaseLoaderCallback
e: D:\USERS_WINDOWS\user_AhmedWk\AndroidProjects\OPENCV_TEST\app\src\main\kotlin\com\example\nativeopencvandroidtemplate\MainActivity.kt: (23, 9): 'onManagerConnected' overrides nothing
e: D:\USERS_WINDOWS\user_AhmedWk\AndroidProjects\OPENCV_TEST\app\src\main\kotlin\com\example\nativeopencvandroidtemplate\MainActivity.kt: (25, 17): Unresolved reference: LoaderCallbackInterface
e: D:\USERS_WINDOWS\user_AhmedWk\AndroidProjects\OPENCV_TEST\app\src\main\kotlin\com\example\nativeopencvandroidtemplate\MainActivity.kt: (34, 27): Unresolved reference: onManagerConnected
e: D:\USERS_WINDOWS\user_AhmedWk\AndroidProjects\OPENCV_TEST\app\src\main\kotlin\com\example\nativeopencvandroidtemplate\MainActivity.kt: (92, 26): Unresolved reference: initAsync
e: D:\USERS_WINDOWS\user_AhmedWk\AndroidProjects\OPENCV_TEST\app\src\main\kotlin\com\example\nativeopencvandroidtemplate\MainActivity.kt: (95, 48): Unresolved reference: LoaderCallbackInterface

Please advise on what can be done to mitigate this issue. Thanks.

Enable SURF

Could you please clarify how to build openCV with contrib modules and enable SURF/SIRF and other non-free modules. As I understand they are disabled by default, so it's needed special CMAKE option OPENCV_ENABLE_NONFREE.

And thank you for this tutorial!

Can't use OpenCV's core / imgcodecs

Hi @VlSomers,
Great tutorial! thanks for uploading it :)

I followed the tutorial, with the exception of the Camera, which I don't use in my Android app.
I'm using the same gradle version you used and same OpenCV version, on Ubuntu 16.04 Linux / Galaxy S10.

I have a fairly simple native-lib.cpp file:

#include <jni.h>
#include <string>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>

extern "C" JNIEXPORT jstring JNICALL
Java_com_ocv_MainActivity_ocvLoadImage(
        JNIEnv *env,
        jobject thiz) {
    using namespace cv;
    using namespace std;
    Mat image;
    string imageName = "/Pictures/images/image-001.jpg";
    image = imread(imageName, IMREAD_COLOR);
    std::string returnString = "OpenCV load image: ";
    if (! image.empty()){
        returnString.append(imageName);
    }
    return env -> NewStringUTF(returnString.c_str());
}

but when I run it I get the following errors:

undefined reference to cv::imread
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

Removing the call to imread works OK.

Think you can help?

Unable to Resolve Dependency: Could not resolve project :opencv

ERROR: Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve project :opencv.
Show Details
Affected Modules: app

ERROR: Unable to resolve dependency for ':app@debugAndroidTest/compileClasspath': Could not resolve project :opencv.
Show Details
Affected Modules: app

Incorrect orientation

On my pixel 4 the image is 90 degrees off when the phone is used in portrait mode.
It works just fine in landscape mode.

couldn't find "libnative-lib.so"

I am trying to implement openCV 4.4.0 in my android application but it's not working. Open CV is installed successfully and I am getting success log on create while calling OpenCVLoader.initDebug();
but I am getting error on calling System.loadLibrary("native-lib");
This what is the error message:
2020-12-16 12:22:27.774 19087-19087/com.learn.opencvapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.learn.opencvapplication, PID: 19087
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.learn.opencvapplication-2SgMMN8HNLPD9LVjRA9Xiw==/base.apk"],nativeLibraryDirectories=[/data/app/com.learn.opencvapplication-2SgMMN8HNLPD9LVjRA9Xiw==/lib/arm64, /data/app/com.learn.opencvapplication-2SgMMN8HNLPD9LVjRA9Xiw==/base.apk!/lib/arm64-v8a, /system/lib64]]] couldn't find "libnative-lib.so"
Previously I have used CV3 and it was working fine, CV 4 has this issue and I need CV 4.

Please update gradle plugin, gradle version and configure kotlin version

It looks like that last version is a bit broken, here is a patch:

diff --git a/app/build.gradle b/app/build.gradle
index 07efaff..91819f2 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -5,12 +5,11 @@ apply plugin: 'kotlin-android'
 apply plugin: 'kotlin-android-extensions'
 
 android {
-    compileSdkVersion 30
-    buildToolsVersion "29.0.3"
+    compileSdkVersion 31
     defaultConfig {
         applicationId "com.example.nativeopencvandroidtemplate"
         minSdkVersion 28
-        targetSdkVersion 30
+        targetSdkVersion 31
         versionCode 1
         versionName "1.0"
         testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 15c699e..39df5f5 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -15,7 +15,8 @@
             android:roundIcon="@mipmap/ic_launcher_round"
             android:supportsRtl="true"
             android:theme="@style/AppTheme">
-        <activity android:name=".MainActivity">
+        <activity android:name=".MainActivity"
+            android:exported="true">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN"/>
 
diff --git a/build.gradle b/build.gradle
index 8337c22..b67d2ca 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1,14 +1,14 @@
 // Top-level build file where you can add configuration options common to all sub-projects/modules.
 
 buildscript {
-    ext.kotlin_version = '1.3.72'
+    ext.kotlin_version = '1.6.0'
     repositories {
         google()
         jcenter()
         
     }
     dependencies {
-        classpath 'com.android.tools.build:gradle:4.1.0'
+        classpath 'com.android.tools.build:gradle:7.0.4'
         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
         // NOTE: Do not place your application dependencies here; they belong
         // in the individual module build.gradle files
diff --git a/gradle.properties b/gradle.properties
index 491bf7d..3d8ce0c 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -15,5 +15,3 @@ org.gradle.jvmargs=-Xmx1536m
 kotlin.code.style=official
 android.useAndroidX=true
 android.enableJetifier=true
-
-opencvsdk=/Users/Example/Downloads/OpenCV-android-sdk
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
index ab3a807..f242983 100644
--- a/gradle/wrapper/gradle-wrapper.properties
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
-#Sat Oct 17 14:23:13 CEST 2020
+#Mon Jan 03 10:29:39 ART 2022
 distributionBase=GRADLE_USER_HOME
+distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
 distributionPath=wrapper/dists
-zipStoreBase=GRADLE_USER_HOME
 zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
+zipStoreBase=GRADLE_USER_HOME

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.