Zxing scanner Android Studio [closed]

て烟熏妆下的殇ゞ 提交于 2019-12-28 13:42:10

问题


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);

回答1:


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.



来源:https://stackoverflow.com/questions/27571530/zxing-scanner-android-studio

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!