Search code examples
androidandroid-camerazxing

Android ZXing Barcode Scanner displays the wrong result


I am designing an app for scan Barcode. There are three activities where I have used Barcode Scanner, only in one Activity the code is working fine and other two activities its taking too much time to scan and some times scans but displaying wrong result.

I am very confused about what the problem is. Same code I have copy paste in all three activities.

build.gradle:

compile 'me.dm7.barcodescanner:zxing:1.9'

Code:

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.google.zxing.Result;

import me.dm7.barcodescanner.zxing.ZXingScannerView;

public class ScanBoxActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {

     private ZXingScannerView scannerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        scannerView = new ZXingScannerView(this);
        setContentView(scannerView);

    }

    @Override
    public void onResume()
    {
        super.onResume();

                if(scannerView== null)
                {
                    scannerView = new ZXingScannerView(this);
                    setContentView(scannerView);
                }
                scannerView.setResultHandler(this);
                scannerView.startCamera();

    }

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

    @Override
    public void handleResult(final Result result) {
        final String scanResult = result.getText();
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Scan Result");
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int i) {
                Toast.makeText(ScanBoxActivity.this,""+scanResult,Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int i) {
                onResume();
            }
        });
        builder.setMessage(scanResult);
        AlertDialog alert = builder.create();
        alert.show();

    }
}

Solution

  • compile 'com.edwardvanraak:MaterialBarcodeScanner:0.0.6-ALPHA'
    
    compile 'com.google.android.gms:play-services-vision:11.0.4'
    

    This library has google support and easy to implement and I have implemented it in one of my app. So, if you face any further issue , ask me freely.