0

I am new to android. I am trying to develop a barcode scanner, that works as follow: Take image(of barcode) via camera and scan this barcode image. my question is how i can do it? Thanks in advance

Here is my code:

TextView result_text;
Button scan_btn;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    result_text = (TextView)findViewById(R.id.Result_tv);

    scan_btn = (Button)findViewById(R.id.Barcode_Scan_Button);
    scan_btn.setOnClickListener( new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent scanIntent= new Intent("com.google.zxing.client.android.SCAN");
            scanIntent.putExtra("SCAN_MODE", "QR_CODE_MODE");

            startActivityForResult(scanIntent, 0);

        }
    });
}

public void onActivityResult(int requestCode, int resultCode, Intent intent) {

        if (requestCode == 0) {


          if (resultCode == RESULT_OK) {

            result_text.setText(intent.getStringExtra("SCAN_RESULT"));

          } else if (resultCode == RESULT_CANCELED) {

            result_text.setText("Scan cancelled.");

          }

        }

      }

    }
zain
  • 11
  • 1
  • 3

3 Answers3

4

@Zain the approach you are taking is strictly not recommended.

As per their guidelines you should not explicitly call Scan Intent.

You must used new IntentIntegrator class released by zxing.

Here you go

First add code to invoke the Intent:

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

Download IntentIntegrator class from below url.

http://code.google.com/p/zxing/source/browse/trunk/android-integration/src/com/google/zxing/integration/android/IntentIntegrator.java

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

@Override   
protected 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
  • i am confused about how to get camera image and pass it to scanner for scanning... – zain Jul 04 '12 at 05:41
  • IntentIntegrator integrator = new IntentIntegrator(yourActivity); integrator.initiateScan(); i have call it on onCreate, i am getting error when i try to replace yourActivity with my avtivity class name. – zain Jul 04 '12 at 05:42
  • @Zain try the example application that they have provided.That will give you directions. – Vipul Jul 04 '12 at 05:49
  • when i click on scan button, getting message install barcode scanner. I don't want this approch ... – zain Jul 04 '12 at 07:23
2

Study http://code.google.com/p/zxing/

Anyway, the problem is quite complex, this is IIRC done using fouriers, edge detection, matrix computations etc. Plus there are many standards. Prepare for a month(s) of work.

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
0

Firebase ML kit barcode scanning API can be used to build barcode scanning feature in android apps. To get barcode value, upright image containing barcode needs to be passed to Firebase ML kit barcode scanning API.

Here is an example app with barcode scanning feature.

Arnav Rao
  • 6,692
  • 2
  • 34
  • 31