?? Adding OpenCV to Your Kotlin Android Project: A Comprehensive Step-by-Step Guide ??

?? Adding OpenCV to Your Kotlin Android Project: A Comprehensive Step-by-Step Guide ??



Step-by-Step Guide to Adding OpenCV Library to Kotlin:

Firstly, we search for "release opencv" on Google. Then we click on the GitHub Release opencv/opencv link:


Google Search Results for OpenCV


Secondly, when we enter GitHub, we choose the latest version and then click on the opencv-4.10.0-android-sdk.zip link to download the OpenCV SDK for Android as a zip file:


OpenCV Github Repository


Thirdly, after downloading, we open the zip file. We return to Android Studio to add the OpenCV library. Here, follow the steps to add the library and configure Android Studio.

In Android Studio, we click on 'File > New > Import Module ' .


Android Studio Screenshot


In the "Import Module from Source" screen, click the file browsing button to choose the downloaded OpenCV library:

We select the opencv file, then sdk, and click open. Here is an image showing the selected file:


OpenCV SDK Selection


In Android Studio, import the module from the source. We select the downloaded OpenCV and give a name to the library:


Import Module from Source Screeenshot


We switch from the Android directory to the Project directory. The project should look like this:



Project Directory

As you can see, the OpenCV library is added to the project, but there is an error because the OpenCV library adding process is not completed yet. To fix the error, we continue configuring the project:

Firstly, open settings.gradle:


settings.gradle


Cut (Command + X or Ctrl + X) the code from the project and delete the settings.gradle file. Right-click on the settings file, then Settings.gradle > Open In > Finder, and delete settings.gradle in the project file:


OpenCVSample Project in Finder


Next, add include(":opencv") to settings.gradle.kts , then Sync:

Settings.gradle.kts

After we click File > Project Structure in Android Studio. Select the Module part to check if the Compile Sdk Version and Build Tool Version are the same in the OpenCV library and app module:


App Module Properties


and


OpenCV Properties


When the dependencies are similar, apply and click OK.

In build.gradle.kts(:app) , add OpenCV as implementation to dependencies:


build.gradle.kts


After that, Sync.

Now, we apply OpenCV to MainActivity. We check if there is an OpenCV library in the project with this code:


MainActivity OpenCV Library Code


When we run the project on a real device, we get this error:


Build Time Error


The error text form is that:


Execution failed for task ':opencv:compileDebugKotlin'.
> 'compileDebugJavaWithJavac' task (current target is 1.8) and 'compileDebugKotlin' task (current target is 17) jvm target compatibility should be set to the same Java version.
Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain        


The solution for this error is to change sourceCompatibility and targetCompatibility to JJavaVersion.VERSION_17 and change jvmTarget to "17" in build.gradle.kts(Module: app). Here's the changed code snippet:


// build.gradle.kts(Module: app)
android {
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = "17"
    }
   // the rest...
}
        


And apply similar changes in build.gradle(:opencv) :


// build.gradle(:opencv)
android {
   // The rest...
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
}        


After that, Sync the project. Then run the project.

Here's the screenshot of the project running on a device:



Phone mirroring image


Finally, to add the OpenCV library to a Kotlin project in Android Studio, you need to download the SDK from GitHub, import the module into your project, adjust the settings, and ensure compatibility between Java versions. After setting up and syncing, you can integrate OpenCV functionality into your application. Finally, resolving any build errors by aligning Java and Kotlin compatibility settings ensures smooth execution.



The help resources:


Source Code Link: https://github.com/ahmetbostanciklioglu/Open_CV_Template.git


Happy Coding. ??


Medium: https://medium.com/@ahmetbostanciklioglu

LinkedIn: https://www.dhirubhai.net/in/ahmetbostanciklioglu/

GitHub: https://github.com/ahmetbostanciklioglu





要查看或添加评论,请登录

Ahmet Bostanciklioglu的更多文章

社区洞察

其他会员也浏览了