How to scan QR code (QR code) with zxing

1. Introduction: Use Google's open source library Zxing , but most of the online tutorials are relatively early. Here are some basic codes and usage rules I summarized:

First of all, you must go to the official website to see:

The address of github-Zxing official library
github-zxing-android-embedded A very useful android tool

 

2. Import

If you are using android studio, add the following to the gradle file:

compile 'com.google.zxing:core:3.2.1'

or

compile group: 'com.google.zxing', name: 'core', version: '3.2.1'

 Then import ZXing Android Embedded

compile 'com.journeyapps:zxing-android-embedded:3.3.0'

 

3. Scan:

When using the scan, the system services are used, which is to jump from the current MainActivity to the CustomScanActivity

Scan style can be customized

During MainActivity:

// You can also use the simple scan function, but the style and behavior of the general scan can be customized, here is the code about the customization
// You can use this method as a click event
public void customScan(){
        new IntentIntegrator(this)
        .setOrientationLocked(false)
        .setCaptureActivity(CustomScanActivity.class) // Set the custom activity to be CustomActivity
        .initiateScan(); // initialize scan
    }

 This will jump to the CustomActivity scan. If you don't jump, you don't need .setCaptureActivity(CustomScanActivity.class), and scan directly on the current page.

 

The following methods get the result:

@Override
// Get the scanned value through the onActivityResult method
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode,resultCode,data);
        if(intentResult != null) {
            if(intentResult.getContents() == null) {
                Toast.makeText(this,"The content is empty",Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this,"scan successfully",Toast.LENGTH_LONG).show();
                // ScanResult is the obtained string
                String ScanResult = intentResult.getContents();
            }
        } else {
            super.onActivityResult(requestCode,resultCode,data);
        }
    }

 

 

Here is the xml style:

<!-- I just modified some styles here in the overall situation, but in fact, the various laser bars and borders in the scan frame can be changed. Interested students can search for themselves -->
<!-- This control is the scanning window-->
    <com.journeyapps.barcodescanner.DecoratedBarcodeView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/dbv_custom"
        app:zxing_framing_rect_width="200dp"
        app:zxing_framing_rect_height="50dp"

        app:zxing_preview_scaling_strategy="fitXY"
        app:zxing_use_texture_view="true"
        >
    </com.journeyapps.barcodescanner.DecoratedBarcodeView>

 

refer to:

1.http://blog.csdn.net/qq_28057541/article/details/52034988

2.http://blog.csdn.net/u013718120/article/details/51683125

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326036958&siteId=291194637