Gajanand Swamy
Gajanand Swamy

Reputation: 2108

How to read 1D barcode scanners in Zxing and Googles Mobile Vision API?

enter image description hereI am using Zxing for reading barcode scanners. i am able to read QR codes but unable to read 1D barcodes.and also i tried with Google play services mobile api but unable to read 1D barcode scanners there as well.

code of Zxing here

    public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {

    private static final int REQUEST_CAMERA = 1;
    private ZXingScannerView mScannerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
        mScannerView = new ZXingScannerView(MainActivity.this);
        setContentView(mScannerView);




    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mScannerView.stopCamera();
    }

    private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
        new android.support.v7.app.AlertDialog.Builder(MainActivity.this)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", null)
                .create()
                .show();
    }

    @Override
    public void onResume() {
        super.onResume();
        // Register ourselves as a handler for scan results.
        mScannerView.setResultHandler(this);
        // Start camera on resume
        mScannerView.startCamera();
    }

    @Override
    public void handleResult(Result rawResult) {
        final String result = rawResult.getText();
        Log.d("QRCodeScanner", rawResult.getText());
        Log.d("QRCodeScanner", rawResult.getBarcodeFormat().toString());
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Scan Result");
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mScannerView.resumeCameraPreview(MainActivity.this);
            }
        });
        builder.setNeutralButton("Visit", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com "+result.trim()));
                startActivity(browserIntent);
            }
        });
        builder.setMessage(rawResult.getText());
        AlertDialog alert1 = builder.create();
        alert1.show();
    }
}

help me to read 1D barcodes in both of these libraries.? above is the 1D barcode image

Upvotes: 0

Views: 994

Answers (1)

Abhishek kumar
Abhishek kumar

Reputation: 4445

I am using this Library Zxing on my project, i faced so much problem with this library,If you are using this library make sure following things :

  1. The Activity in which you are using Scanner don't put class inside any your own package in Android studio, just create Activity inside your app package.

  2. If you are customizing the scanner screen please do once , if you change the screen time to time it will create issue Not to scan.

  3. Also i figureout this library gives some time wrong result if it taking more time to scan same barcode.

Now, i'm sharing my Code :

inside onCreate method :

    //Scanner
    mScannerView = new ZXingScannerView(this);
    RelativeLayout rl = (RelativeLayout) findViewById(R.id.relative_scan_take_single);
    rl.addView(mScannerView);
    mScannerView.setResultHandler(this);
    mScannerView.startCamera();
    mScannerView.setSoundEffectsEnabled(true);
    mScannerView.setAutoFocus(true);
  }


 @Override
    public void onResume() {
        super.onResume();
        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        mScannerView.startCamera();          // Start camera on resume
    }

@Override
public void onPause() {
    super.onPause();
    mScannerView.stopCamera();           // Stop camera on pause
}

 @Override
 public void handleResult(Result rawResult) {
    // Do something with the result here
    Log.e(TAG, rawResult.getText()); // Prints scan results
    Log.e(TAG, rawResult.getBarcodeFormat().toString());

    Log.e("SCAN_RESULT", "" + rawResult.getText());
    //dataSingle.put("0",rawResult.getText());

Upvotes: 1

Related Questions