Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zxing scanner Android Studio [closed]

Tags:

android

zxing

Hi i know how to import a minimal Android library project to scan a qr code but after it scanned the qr code i would like to have a result of what the QR code get (url for example) but i really don't know how to retrieve the results so that's why i'am asking your help.

I'm trying to use this : https://github.com/embarkmobile/zxing-android-minimal#custom-layout

I use this to start the scanner :

IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setCaptureLayout(R.layout.custom_layout);
integrator.initiateScan();

Thanks in advance i did this for the webview

wb  = (WebView)findViewById(R.id.webView2);
        wb.loadUrl(re);
like image 434
BtAndro Avatar asked Nov 28 '22 13:11

BtAndro


1 Answers

First, your Activity must implement the method Activity.onActivityResult(int, int, Intent) and include a line of code like this:

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
   IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
   if (scanResult != null) {
     // handle scan result
   }
   // else continue with any other code you need in the method
   ...
 }

This is where you will handle a scan result.

Second, just call this in response to a user action somewhere to begin the scan process:

IntentIntegrator integrator = new IntentIntegrator(yourActivity);
 integrator.initiateScan();

Note that initiateScan() returns an AlertDialog which is non-null if the user was prompted to download the application. This lets the calling app potentially manage the dialog. In particular, ideally, the app dismisses the dialog if it's still active in its Activity.onPause() method.

You can use setTitle(String) to customize the title of this download prompt dialog (or, use setTitleByID(int) to set the title by string resource ID.) Likewise, the prompt message, and yes/no button labels can be changed.

Finally, you can use addExtra(String, Object) to add more parameters to the Intent used to invoke the scanner. This can be used to set additional options not directly exposed by this simplified API.

By default, this will only allow applications that are known to respond to this intent correctly do so. The apps that are allowed to response can be set with setTargetApplications(List). For example, set to TARGET_BARCODE_SCANNER_ONLY to only target the Barcode Scanner app itself.

For more details, please refer here.

Sample code:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

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

public class MainActivity extends Activity {

    private Button mButton;

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

        // Scanner

        mButton = (Button) findViewById(R.id.assistant_button);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
                integrator.initiateScan();
            }
        });

    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        if (scanResult != null) {
            String re = scanResult.getContents();
            Log.d("code", re);
        }
        // else continue with any other code you need in the method

    }
 }

One button in your xml, and click it, scan a barcode, the it will return the raw content of barcode.

like image 87
bjiang Avatar answered Dec 15 '22 14:12

bjiang