opencv android3.4.2环境搭建

环境:
1.mac os
2.android studio 3.1.3
3.opencv-3.4.2-android-sdk.zip
下载地址:

https://sourceforge.net/projects/opencvlibrary/files/opencv-android/

1.新建android项目

注意:选择Include C++ support

opencv android3.4.2环境搭建_第1张图片

2.导入openCVLibrary342

导入opencv java依赖库

File -> New -> Import Module

opencv android3.4.2环境搭建_第2张图片

3.导入openCV so库和静态库

将OpenCV-android-sdk-> sdk -> native 目录下
libs和staticlibs目录中的文件拷贝到android libs目录中

opencv android3.4.2环境搭建_第3张图片

导入到android libs中

opencv android3.4.2环境搭建_第4张图片

4.添加依赖

选择 app模块 右键 Project Structure

opencv android3.4.2环境搭建_第5张图片

app/build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "cn.xylink.ocr.image"
        minSdkVersion 14
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++14 -frtti -fexceptions"
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }

    packagingOptions{
        doNotStrip '*/mips/*.so'
        doNotStrip '*/mips64/*.so'
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar')
    implementation 'com.android.support.constraint:constraint-layout:1.1.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: '**/*')
    into 'lib/'
}

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn(nativeLibsToJar)
}

buildscript {

    repositories {
        maven { url 'https://maven.aliyun.com/repository/jcenter' }
//        jcenter()
        google()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
//        jcenter()
        maven { url 'https://maven.aliyun.com/repository/jcenter' }
        google()
    }
}

build.gradle

buildscript {

    repositories {
        maven { url 'https://maven.aliyun.com/repository/jcenter' }
//        jcenter()
        google()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'

    }
}

allprojects {
    repositories {
        maven { url 'https://maven.aliyun.com/repository/jcenter' }
        google()
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

local.properties

## This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Tue Aug 28 12:03:32 CST 2018
ndk.dir=/Users/hecj/profile/android-sdk-macosx/ndk-bundle
sdk.dir=/Users/hecj/profile/android-sdk-macosx

5.加载opencv lib

在Activity onResume中初始化

   @Override
    protected void onResume() {
        super.onResume();
        if (!OpenCVLoader.initDebug()){
        }
    }

注:此方法无需在额外安卓opencv Mannager apk了

你可能感兴趣的:(opencv,OpenCV之Java学习之路)