"OpenCV for Android" Android Studio integrated OpenCV

Source: Internet
Author: User

Preparatory work

1. Download and install Android Studio(process slightly).

2. Download android OpenCV:https://opencv.org/releases.html, find Android pack click to download.

Download and extract the following files, where the SDK folder is the file required for Android Studio integration OpenCV .

Integration Steps

1. Create the project in Android Studio and import the OpenCV in the import module , file-New modules,

In source directory, select the SDK--- Java, which was just extracted from the OpenCV file path ,as below, click Finish button.

You may see the following error: Click the blue font, install the relevant files.

After the module is successfully imported, the following directory appears in the project folder:

2. Add dependencies, click File--project structure, click moudle under apps, select dependencies,

Select the OpenCV library that you just downloaded,

Add the following after success.

After adding the dependency, we can use the OpenCV API in the project , but if you run the APPnow, youwill be prompted to install OpenCV Manager, indicating that OpenCV integration has not been successful because of its own APP cannot install independently must use OpenCV Manager this APP in order to avoid this tedious condition, we continue to perform the following steps.

Modify the build.gradle file

1. Modify some of the parameters in the Opencvlibrary build.gradle file to be build.gradle with the app the same in the file (the part that the red box is marked with is the part that needs to be modified):

Click the Sync button and the following prompt appears:

Follow the prompts to comment out the corresponding content,

Click Sync to display the build success.

2. Copy the files in the OpenCV folder directory under SDK, native, Libs to the project's lib directory.

3. Add the following code to the app 's gradle file:

The full code for the Build.gradle file is as follows:

Apply plugin: ' Com.android.application 'Android {compilesdkversion28Defaultconfig {ApplicationID"Com.example.white.opencvmyapplication"minsdkversion15targetsdkversion28Versioncode1Versionname"1.0"Testinstrumentationrunner"Android.support.test.runner.AndroidJUnitRunner"Externalnativebuild {cmake {cppflags""}}} buildtypes {release {minifyenabledfalseproguardfiles Getdefaultproguardfile (' Proguard-android.txt '), ' Proguard-rules.pro '}}}dependencies {implementation filetree (include: [' *.jar '], dir: ' Libs ') Implementation' COM.ANDROID.SUPPORT:APPCOMPAT-V7:28.0.0-RC01 'Implementation' com.android.support.constraint:constraint-layout:1.1.2 'Compile Filetree (dir:"$buildDir/native-libs", include: ' Native-libs.jar ')    //testimplementation ' junit:junit:4.12 'Androidtestimplementation ' com.android.support.test:runner:1.0.2 'androidtestimplementation' com.android.support.test.espresso:espresso-core:3.0.2 'Implementation project (': openCVLibrary342 ')}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)}    }}

At this point, the OpenCV configuration is complete, avoiding the NDK 's cumbersome and reliance on third-party APP OpenCV manager, but this configuration OpenCV The load must be statically loaded by the way.

Test

Add the following code to the mainactivity to run the APP,

 Packagecom.example.white.opencvmyapplication;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportOrg.opencv.android.OpenCVLoader; Public classMainactivityextendsappcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //Setcontentview (r.layout.activity_main);System.out.println ("Loading");    Initloadopencvlibs (); }    Private voidinitloadopencvlibs () {BooleanSuccess =Opencvloader.initdebug (); if(Success) {SYSTEM.OUT.PRINTLN ("Loading Success"); LOG.D ("Test", "INITLOADOPENCVLIBS:OPENCV loaded successfully!"); } Else{System.out.println ("Loading Failed"); LOG.D ("Test", "INITLOADOPENCVLIBS:OPENCV load failed!"); }    }}

The console is printed as follows, which indicates that the OPENCV integration was successful.

Reprint Please specify source: https://www.cnblogs.com/White-xzx/p/9563479.html

"OpenCV for Android" Android Studio integrated OpenCV

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.