0

i read QR/BarCode using Zxing library , Currently my project is reading Qr/Barcode from Sdcard and Decoding it

i want to use my device camera to scann code and then decode , how do i change my code so tht it works here .

Here is my code

  public static class Global
{
    public static String text=null;
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Bitmap bMap = BitmapFactory.decodeFile("/sdcard/2.gif");
    TextView textv = (TextView) findViewById(R.id.mytext);
    View webbutton=findViewById(R.id.webbutton);
    LuminanceSource source = new RGBLuminanceSource(bMap); 
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    Reader reader = new MultiFormatReader();
    try {
         Result result = reader.decode(bitmap);
         Global.text = result.getText(); 
            byte[] rawBytes = result.getRawBytes(); 
            BarcodeFormat format = result.getBarcodeFormat(); 
            ResultPoint[] points = result.getResultPoints();
            textv.setText(Global.text);
            webbutton.setOnClickListener(this);
    } catch (NotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ChecksumException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FormatException e) {
        // TODO Auto-generated catch block
e.printStackTrace();


    }   
}

@Override
public void onClick(View v) {
    Uri uri = Uri.parse(Global.text); 
    Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
    startActivity(intent);




}
sara
  • 131
  • 4
  • 14

2 Answers2

0

It depends whether you like to use external application like zxing ( in this case you just send intent and it scans barcode for you and returns result ), or do everything in your application. IN this case you need code to manage preview images, snap a picture and feed data into ZXing ( or whatever ) library.

This project demos: http://sourceforge.net/projects/javaocr/

contains everything up to parsing barcode ( image data is feed to OCR engine instead )

Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35
0

You need to call Zxing intent in your Activity and relax.

First add code to invoke the Intent:

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

Second, add this to your Activity to handle the result:

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

Invest your time to go through wiki page of Zxing.They have explained it very nicely.

http://code.google.com/p/zxing/w/list

http://code.google.com/p/zxing/wiki/ScanningViaIntent

Here is sample application demonstrating how to call Zxing intent.

http://code.google.com/p/zxing/source/browse/trunk/androidtest/src/com/google/zxing/client/androidtest/ZXingTestActivity.java

Finally Test Project + Library is located at

http://code.google.com/p/zxing/source/browse/trunk#trunk%2Fandroid-integration%253Fstate%253Dclosed

Vipul
  • 27,808
  • 7
  • 60
  • 75