简体   繁体   中英

Implement a QR code Scanner to my app

I'm working on an android app using web server to store users and data. I have already generated a Qr code for each user with PHP. I need to scan that Qr Code to make users login. I have already tried to implement Zxing.

This is the function I use to call the ZXing QR code.

 btnLogin.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

            Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
            startActivityForResult(intent,0);


        }

    });

This is my manifest file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dahmani.javagose.cameratest" >

<uses-feature android:name="android.hardware.Camera" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:name=".app.AppController"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <activity android:name=".LoginActivity"
        android:launchMode="singleTop"
        android:windowSoftInputMode="adjustPan"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" >
    </activity>


    <activity
        android:name=".PhotoInfoActivity"
        android:label="@string/title_activity_photo_info"
        android:theme="@style/AppTheme.NoActionBar" >
    </activity>


</application>

and this my build.gradle.app

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
    applicationId "com.dahmani.javagose.cameratest"
    minSdkVersion 9
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    debug {
        debuggable true
    }
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }
}

dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])
  testCompile 'junit:junit:4.12'
  compile 'com.android.support:appcompat-v7:23.1.1'
  compile 'com.android.support:design:23.1.1'
  compile 'com.google.android.gms:play-services:8.4.0'
  compile 'com.mcxiaoke.volley:library-aar:1.0.0'
  compile 'me.dm7.barcodescanner:zxing:1.8.4'

}

However, when I click the login btn , the app crashes.

and this what shows android monitor

 03-07 16:10:15.293 18916-18916/? E/AndroidRuntime: FATAL EXCEPTION: main
03-07 16:10:15.293 18916-18916/? E/AndroidRuntime: Process: com.dahmani.javagose.cameratest, PID: 18916
03-07 16:10:15.293 18916-18916/? E/AndroidRuntime: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.google.zxing.client.android.SCAN (has extras) }
03-07 16:10:15.293 18916-18916/? E/AndroidRuntime:     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1727)
03-07 16:10:15.293 18916-18916/? E/AndroidRuntime:     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1511)
03-07 16:10:15.293 18916-18916/? E/AndroidRuntime:     at android.app.Activity.startActivityForResult(Activity.java:3424)
03-07 16:10:15.293 18916-18916/? E/AndroidRuntime:     at android.app.Activity.startActivityForResult(Activity.java:3385)
03-07 16:10:15.293 18916-18916/? E/AndroidRuntime:     at com.dahmani.javagose.cameratest.LoginActivity$1.onClick(LoginActivity.java:81)
03-07 16:10:15.293 18916-18916/? E/AndroidRuntime:     at android.view.View.performClick(View.java:4499)
03-07 16:10:15.293 18916-18916/? E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:18571)
03-07 16:10:15.293 18916-18916/? E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:733)
03-07 16:10:15.293 18916-18916/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
03-07 16:10:15.293 18916-18916/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136)
03-07 16:10:15.293 18916-18916/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5021)
03-07 16:10:15.293 18916-18916/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
03-07 16:10:15.293 18916-18916/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
03-07 16:10:15.293 18916-18916/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
03-07 16:10:15.293 18916-18916/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
03-07 16:10:15.293 18916-18916/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

The problem might be that you don't have zxing scanner app. The standard library doesn't offer you and embedded way of scanning QR codes, at least none that I'm aware of.

So, you have 2 ways of doing it. Install zxing scanner app (or request user to install it if it does not exist on the phone, redirecting him/her to google play). This is the ugly way.

Or, you can use another library:

embedded zxing

I used it for embedding scanner into my app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM