Capture a QRCode with the ZXing library on Android

1

I try to make a reader QRCode using the library ZXing but I can not find the necessary dependencies to embed and a basic example for start.

I want only that the camera with the red bar comes out and returns the QRCode scanned.

I just found a .jar link

    
asked by Webserveis 15.06.2017 в 18:53
source

2 answers

2

You have to go to your "build.gradle" file in the "Gradle Scripts" folder and in the dependencies add:

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

It would be something like:

dependencies {
    .
    .
    compile 'com.journeyapps:zxing-android-embedded:3.5.0'
    .
    .
}

Then you click on the button that appears at the top right "Sync Now"

That's what you do for the Adroid Studio to add the library to the project.

I leave you a simple code of how you can get the QR in an Activity

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

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

public class ScannerActivity extends AppCompatActivity {

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

        IntentIntegrator intent = new IntentIntegrator(this);
        intent.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);

        intent.setPrompt("Scan");
        intent.setCameraId(0);
        intent.setBeepEnabled(false);
        intent.setBarcodeImageEnabled(false);
        intent.initiateScan();
    }

    @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, "Cancelaste el escaneo", Toast.LENGTH_LONG).show();
            }
            else{
                Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();
            }
        }
        else
        {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
    
answered by 15.06.2017 / 19:08
source
1

As a complement to the answer of the companion @Casca

Installation Installation of the unit:

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

Start capture

To initialize a quick capture of a Activity

new IntentIntegrator(this).initiateScan();

If fragment is used

IntentIntegrator.forFragment(this).initiateScan();

Personalization

Can be customized, such as the types of codes to be scanned with setDesiredBarcodeFormats the text that is displayed per screen setPrompt the rear camera 0 or the front 1

IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
integrator.setPrompt("Scan a barcode");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(true);
integrator.initiateScan();

Read Result

To intercept the scan result is done with onActivityResult

@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, "Cancelled", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }

}

Extracted from: link

More examples

This Project-of-test repository contains several examples of use

answered by 15.06.2017 в 19:30