Android Studio中安装OpenCV SDK

来源:互联网 发布:有域名怎么免费建网站 编辑:程序博客网 时间:2024/04/28 04:13

Android Studio中安装OpenCV SDK

Open Source Computer Vision (OpenCV) is a library used for computer vision and machine learning. It has many optimized algorithms which can be used to perform tasks easily. In the first part of this tutorial we will setup OpenCV Android SDK in our Android Studio project. In the next one we will change the brightness of a Image in real time using OpenCV.

这里写图片描述

OpenCV Android SDK can be downloaded from opencv.org. The latest version at the time of writing is 3.1.0.

这里写图片描述

这里写图片描述

  1. Extract the downloaded zip file.

  2. Open Android Studio and create a new project with package of your choice. Here I have created a new project with com.learn2crack.opencvdemo

  3. Then select File ->New -> Import Module

  4. You need to select the OpenCV SDK location. Select OpenCV-android-sdk/sdk/java. Then select Next and Finish. OpenCV sdk is imported as a module.

这里写图片描述

But it may throw error. Lets see how to fix this.

  1. In the project explorer change the project view from Android to Project. Open Project -> openCVLibrary -> build.gradle

  2. Change the compileSdkVersion, targetSdkVersion and buildToolsVersion value to the latest version you use. Here I use 23, 23 and 23.0.2. Then sync the project. The errors will be gone.

这里写图片描述


7. Switch back to Android view in Project explorer. Right click on the app module and select Open Module Settings.

这里写图片描述


8. For the app module in the Dependencies tab, select Add -> Module Dependency -> openCVLibrary

这里写图片描述

  1. Now we need to add native JNI libraries in our project. These libraries should be added in jniLibs directory. Create a new jniLibs directory in app-> src -> main.

  2. Open the extracted OpenCV SDK directory. Switch to OpenCV-android-sdk/sdk/native/libs directory.

  3. You will find directories for many CPU architectures. Copy the required architecture directory to the jniLibs directory. Here I copied x86_64 and armeabi-v7a because my Android emulator has x86_64 architecture and my OnePlus One has armeabi-v7a architecture. Delete all files except libopencv_java3.so.

  4. Open your gradle.properties file and enter the following code.

    android.useDeprecatedNdk=true

    Now we have setup SDK in our Android Studio project. Lets test whether OpenCV is loaded.



这里写图片描述

We use OpenCVLoader to test whether OpenCV is loaded. The Activity code is given as,


这里写图片描述

package com.learn2crack.opencvdemo;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import org.opencv.android.OpenCVLoader;public class MainActivity extends AppCompatActivity {    private static final String TAG = "MainActivity";    static {        if(!OpenCVLoader.initDebug()){            Log.d(TAG, "OpenCV not loaded");        } else {            Log.d(TAG, "OpenCV loaded");        }    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }}

Now run the project. Check log, You will see message OpenCV loaded and many other OpenCV debug messages similar to,

V/StaticHelper: General configuration for OpenCV 3.1.0 =====================================
I/OpenCV/StaticHelper: Version control: 3.1.0
I/OpenCV/StaticHelper: Platform:
I/OpenCV/StaticHelper: Host: Darwin 15.0.0 x86_64
I/OpenCV/StaticHelper: Target: Android 1 x86_64

0 0
原创粉丝点击