Zxing scanner Android Studio [closed]

孤街浪徒 提交于 2019-11-28 08:49:22

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.

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