QR Code Scanner在Android Studio中打开浏览器

时间:2017-03-28 15:25:04

标签: java android eclipse qr-code

我目前正在开发一款扫描QR码的应用,一旦扫描了二维码,就会转到谷歌表格。我已经包含了权限和ZXing库。当扫描QR码时,它会举起一个祝酒词,虽然我已将其包含在我的代码中。我不完全确定如何更改扫描QR码的结果。任何帮助将非常感激。

这是我的Android清单

apply plugin: 'com.android.application'

repositories {
    maven { url 'http://repo1.maven.org/maven2' }
    jcenter { url "http://jcenter.bintray.com/" }
}


android {
    compileSdkVersion 25
    buildToolsVersion "25.0.1"
    defaultConfig {
        applicationId "com.example.a934238.qrcodeapp"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner     "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),         'roguard-rules.pro'
    }
    debug {
        debuggable true
    }
}

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-    core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'junit:junit:4.12'
compile 'com.google.zxing:core:3.2.1'
compile 'com.journeyapps:zxing-android-embedded:3.2.0@aar'

活动

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

public class readerActivity extends AppCompatActivity {

// initialise local variables
private Button scan_btn;
private Button gen_Page;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_reader);

    // casting button variable to a button on the screen
    scan_btn = (Button) findViewById(R.id.scan_btn);
    gen_Page = (Button) findViewById(R.id.next_btn);
    final Activity activity = this;


    // add onClickListener to the button so that it has an action when pressed; in this case, prompting
    // and initialising the scanning of the QR code
    scan_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            IntentIntegrator integrator = new IntentIntegrator(activity);
            integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
            integrator.setPrompt("Scan");
            integrator.setCameraId(0);
            integrator.setBeepEnabled(false);
            integrator.setBarcodeImageEnabled(false);
            integrator.initiateScan();
        }
    });

    //onClickListener to move from one activity to another
    gen_Page.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(activity, generatorActivity.class);
            startActivity(i);
        }
    });


}


// method which collects the data from the barcode and produces a toast to show the user the code information
// else it informs the user that the scan was cancelled
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if(result != null){

        if(result.getContents() == null){

            Toast.makeText(this, "You have cancelled the scanning...", Toast.LENGTH_LONG).show();

        }else{

            Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();

        }

    }else {

        super.onActivityResult(requestCode, resultCode, data);

    }
}

0 个答案:

没有答案