Skip to content Skip to sidebar Skip to footer

Android Barcode Scanner Using Zxing

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 throu

Solution 1:

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!


Solution 2:

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

Post a Comment for "Android Barcode Scanner Using Zxing"