Android Studio中安装OpenCV SDK

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值