如何android stdio 3.3 + openCV3.4.5生成可以在手机上运行的app

假定是你的android stdio已经配置好的情况下。

1,首先在android上建立一个空工程。 

点击next后,填写好你的工程名如下图

点击finish后开始添加opencv库,File->new->import Module

选择opencv安装文件夹下的OpenCV-android-sdk\sdk\java文件夹

我的是安在D:\opencv-3.4.5-android-sdk\OpenCV-android-sdk\sdk\java ,OK之后如下图

如果没出来module name就说明你路径不对,但我尝试opencv4.0.1时就没有,退而求其次安了3.4.5,点击Next

直接点完finish工程建立完成。这时点击那个小锤子,会提示下出的错误。

主要是因为opencv3.4.5中把<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" />

这名话写在了 AndroidManifest.xml中,去这个文件中把其删除就可以了。

 

 

2、在File->Project Struct中进行设定

 

app中-》dependencies->点击+号加入opencvlibrary345

点击OK即可。代码很简单,如下所示new Mat() ,再释放。

package testopencv.com.myredheaddocumentdetect;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        testOpenCV();
    }
    public void testOpenCV()
    {
        boolean bInitOk= OpenCVLoader.initDebug();
        if(bInitOk)
        {
            Mat src =new Mat();
            src.release();
           

        }

    }

}
然后把工程中的build.gradle(Module:OpenCVLibrary345)中的compileSdkVersion 、minSdkVersion targetSdkVersion 改成和build.gradle(Module:App)的一样,不然又是坑,下面是初始的build.gradle(Module:OpenCVLibrary345)
apply plugin: 'com.android.library'

android {
    compileSdkVersion 14
    buildToolsVersion "28.0.3"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 21
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

下面是我们改进完成的build.gradle(Module:OpenCVLibrary345)
apply plugin: 'com.android.library'

android {
    compileSdkVersion 28
    buildToolsVersion "28.0.3"

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 28
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

这个时候应该可编译安装进手机了,但是安装后APP一闪就退出了,是因为我们没有将opencv的库编译进去。

3、将Opencv文件夹下的D:\opencv-3.4.5-android-sdk\OpenCV-android-sdk\sdk\native\libs中的arm64-v8a拷贝到你工程目录中的app\libs下面。不同机型可能用的库不一样,如果都copy打出的包太大 了。如果是基于arm的新机型都可以,我是在小米pad4上实测的。光把库拷贝了,还不行,还要改一下配置文件

4、打开build.gradle(Module:App),下面的始初的文件

 

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "testopencv.com.myredheaddocumentdetect"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    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(':openCVLibrary345')
}

 

下面的是改进后的文件

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "testopencv.com.myapplication"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

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

 

增加了几个地方

(1)

implementation fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar')

(2)

 

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)
}

之后保存,并编译,就可以直接在真机上运行了。打出来的包大约9M左右,因为有库文件libopencv_java3.so。然后就可以找例程,自己写代码了 
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值