欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Opencv(3.3) & ZXing(Modify for scan QR code) for Android environment on Android Studio

程序员文章站 2022-07-14 17:56:21
...

From:https://blog.csdn.net/lldbuaa/article/details/80333718

I must be crazy!

when I promise someone that I will finish an app with Opencv&ZXing in 2 weeks ,I didnot know what an activity is!

cause I got a lot from web,many people gave advice for other people and I got something right and something wrong…

So If this blog help you,I will be happy and give me five.

1.OpenCV Environment WITHOUT OPENCV_Manager

this link
Opencv4Android doc
Opencv4Android Environment doc

Cause I use Android Studio so I fellow the steps below

(1).Import Module->”opencv/sdk/java”->then get the model like this

Opencv(3.3) & ZXing(Modify for scan QR code) for Android environment on Android Studio

(2).Add depends->Opencv(3.3) & ZXing(Modify for scan QR code) for Android environment on Android Studio

Opencv(3.3) & ZXing(Modify for scan QR code) for Android environment on Android Studio
There, I need to say,when I use Opencv in ZXing(Modify),I will add the depends to ZXing Module,and you will read more.

(3).Modify the build.gradle(openCVLibrary330)& (app)->

//build gradle(opencv)
apply plugin: 'com.android.library'

android {
    compileSdkVersion 27         //Need to Modify
    buildToolsVersion "27.0.3"   //Need to Modify
    //The actual version depends on your build.gradle(app) fellowed

    defaultConfig {
        minSdkVersion 23        //Need to Modify
        targetSdkVersion 27     //Need to Modify
        //Same with the app build.gradle(app)
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}
//build.gradle(app)
apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    buildToolsVersion "27.0.3"
    defaultConfig {
        applicationId "com.example.lld.hellowopencv"
        minSdkVersion 23
        targetSdkVersion 27
        versionCode 1
        versionName "1.1"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar')
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    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(':openCVLibrary330')
}
//Add the fellow code
task nativeLibsToJar(type: Jar, description: 'create a jar archive of the native libs') {
    destinationDir file("$buildDir/native-libs")  //native-libs is the neme of jar
    baseName 'native-libs'                        //same with the up
    from fileTree(dir: 'libs', include: '**/*.so')
    into 'lib/'
}

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

(4).Copy opencv/sdk/native 2 libs(IF you do not have JNI)

Opencv(3.3) & ZXing(Modify for scan QR code) for Android environment on Android Studio

(IF you have JNI) read the link as fellow

Opencv4Android Environment doc

(5).Init OpenCV static

static {
    if (!OpenCVLoader.initDebug()) {
        // Handle initialization error
    } else {
        System.loadLibrary("my_jni_lib1");
        System.loadLibrary("my_jni_lib2");
    }
}

Maybe you will find No Wrong with your code.
When your Module which depends OpenCV is the app,It will work,you will GET the OpenCV Load like this:
Opencv(3.3) & ZXing(Modify for scan QR code) for Android environment on Android Studio
But when your Module which depends OpenCV is the other app, it will not work,and you will GET Cannot load Opencv/Opencv Info/…
You need
create the jniLibs folder and copy opencv/sdk/native to the folder
Opencv(3.3) & ZXing(Modify for scan QR code) for Android environment on Android Studio
you will get
Opencv(3.3) & ZXing(Modify for scan QR code) for Android environment on Android Studio
It means you get the OpenCV environment.

2.ZXing Environment

(1)New Module ->ZXing

(2)Copy files into ZXing/src/main/java/com/

like the fellowing
Opencv(3.3) & ZXing(Modify for scan QR code) for Android environment on Android Studio
and modify the res folder
Opencv(3.3) & ZXing(Modify for scan QR code) for Android environment on Android Studio

(3)Add depends to App Module

(4)Cover the Mainfiest(app)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.lld.checkapp">

    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.FLASHLIGHT"/>
    <uses-feature android:name="android.hardware.camera"/>
    <uses-feature android:name="android.hardware.camera.autofocus"/>
    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".main"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.lld.zxing.Activity.CaptureActivity">//register the next Activity
        </activity>
    </application>

</manifest>

(5).Add core-3.3.0.jar to zxing/libs/

Opencv(3.3) & ZXing(Modify for scan QR code) for Android environment on Android Studio

(6).You Get the ZXing environment (Modify for scan QR code)

3.The Test Pic

Opencv(3.3) & ZXing(Modify for scan QR code) for Android environment on Android Studio
Opencv(3.3) & ZXing(Modify for scan QR code) for Android environment on Android Studio
Opencv(3.3) & ZXing(Modify for scan QR code) for Android environment on Android Studio

4.The download link

OpenCV Environment Only密码: uncv
ZXing Environment Only 密码cn5a
Two Environment in One 密码bdhm

5.If the blog helps you,please make something for others

Maybe Give Me Five is a Good Thing.
Opencv(3.3) & ZXing(Modify for scan QR code) for Android environment on Android Studio