Android Barcode scanner using Zxing

2019-02-26 20:51发布

I want to integrate Zxing in a android project (I'm a beginner).

I have seen that the way described here http://bit.ly/nBszrL is discouraged and that the best way to do it is through Intents as said in this post http://bit.ly/o29Uma

p.s : I don't want the barcode scanner installed on my device

I have Included on my project the required class : http://bit.ly/16pKMKx

my test code :

package com.example.barcodescanner;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    private static final int REQUEST_BARCODE = 0;

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    // when my button is clicked
    public void scanBarCode(View view) {
        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
        intent.putExtra("SCAN_MODE", "PRODUCT_MODE");  

        startActivityForResult(intent, REQUEST_BARCODE);

        Toast toast = Toast.makeText(this, "Start scanning Barcode", Toast.LENGTH_SHORT);
        toast.show();
    }

}

and I really don't understand how to do it by this way : http://bit.ly/18v7K2O (I really don't get it and that's what I want to use)

Do you have any idea about how to do it ?

Thank you.

2条回答
闹够了就滚
2楼-- · 2019-02-26 21:15

The Android system was built to so that people could write apps that do one particular thing well and other developers could use them when they need to. Barcode scanning is a great example. ZXing makes a great scanner and lets other apps use it via Intents. Basically you tell the OS that you want to scan a barcode and ZXing says, "Yes, I can do that!" They scan the barcode and return the information to you. The great thing about doing it this way is that you don't have to worry about when they update their stuff. The user just gets notified that it's updated and you get to use the latest and greatest. The one potential downside is that the user has another app on their phone but I don't really see that as a draw back. To do it this way you really only need the two files you linked to and then you just place this in your code To start the scan:

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

And this bit gets the answer from the barcode scanner:

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
  ...
}

Your only other option is to pull down all of the code for the barcode scanner and lump that into your project and then figure out how it all works and where you need to tie in to bring it into your app. Then you'll have to do that all over again every time ZXing makes an update. It's a mess!

查看更多
你好瞎i
3楼-- · 2019-02-26 21:17
    You can use like this.It works in Activity as well as Fragment.
    Add this code in click listner of any view:


     scan.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  IntentIntegrator integrator = new IntentIntegrator(getActivity());


       integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
                  integrator.setCameraId(0);
                  integrator.setBeepEnabled(true);
                  integrator.setBarcodeImageEnabled(true);
                  integrator.forFragment(InsuranceInfo.this).initiateScan();
                  System.out.println("CLick");
              }




On the Acivity Result Method  add this:-
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        System.out.println((intentResult.getContents()+""));

        super.onActivityResult(requestCode, resultCode, data);
        if (intentResult != null) {
            //passing result to another Activity.
            //    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(intentResult.getContents() + ""));
         //   Toast.makeText(getActivity(),"Scan Results="+(intentResult.getContents()+""),Toast.LENGTH_SHORT).show();
            System.out.println((intentResult.getContents()+""));
            Snackbar.make(getActivity().findViewById(android.R.id.content),
                    ("Scan Results="+ (intentResult.getContents()+"")), Snackbar.LENGTH_LONG).show();
         ScanResult= (intentResult.getContents()+"");
         insurance.setText(ScanResult);

        }
        else {
            Toast.makeText(getActivity(), " Empty Result ", Toast.LENGTH_SHORT).show();
        }
    }
查看更多
登录 后发表回答