Jamal
Jamal

Reputation: 976

Android Zxing Barcode Scanner is not scanning correctly

In my Android application, I am having Barcode scanning functionality. For this functionality I am using Android https://github.com/dm77/barcodescanner library for scanning Barcodes. It is working good, But if I scanned repeatedly in my project, sometimes it is returning a wrong value(not the actual value) of the barcode.I would like to know why it is happening and how to resolve this issue. I have googled but unfortunately I didn't find any better solution.Anyone please guide me to fix the issue.

note: I am using latest version 1.8.4

SimpleScannerActivity.java

import com.google.zxing.Result;

import me.dm7.barcodescanner.core.IViewFinder;
import me.dm7.barcodescanner.core.ViewFinderView;
import me.dm7.barcodescanner.zxing.ZXingScannerView;

    public class SimpleScannerActivity extends BaseScannerActivity implements ZXingScannerView.ResultHandler {
        private ZXingScannerView mScannerView;

        LoadingFlowScreen loadingFlowScreen;
        NextScanScreen nextScanScreen;

        String shipmentin,locationin;


        @Override
        public void onCreate(Bundle state) {
            super.onCreate(state);
            setContentView(R.layout.activity_simple_scanner);
            setupToolbar();

            loadingFlowScreen = new LoadingFlowScreen();
            nextScanScreen = new NextScanScreen();



            ViewGroup contentFrame = (ViewGroup) findViewById(R.id.content_frame);
            mScannerView = new ZXingScannerView(this) {
                @Override
                protected IViewFinder createViewFinderView(Context context) {
                    return new CustomViewFinderView(context);
                }
            };
            contentFrame.addView(mScannerView);
        }

        @Override
        public void onResume() {
            super.onResume();
            mScannerView.setResultHandler(this);
            mScannerView.startCamera();
        }

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

        @Override
        public void handleResult(Result rawResult) {


            Intent in = new Intent(SimpleScannerActivity.this, NextScreen.class);//forwaring to another activity once scanned the barcode
            in.putExtra("scannedText",rawResult.getText());//storing the value in prefernce
            startActivity(in);
            finish();            

          mScannerView.resumeCameraPreview(SimpleScannerActivity.this);

        }

        private static class CustomViewFinderView extends ViewFinderView {
            public static final String TRADE_MARK_TEXT = "";
            public static final int TRADE_MARK_TEXT_SIZE_SP = 40;
            public final Paint PAINT = new Paint();

            public CustomViewFinderView(Context context) {
                super(context);
                init();
            }

            public CustomViewFinderView(Context context, AttributeSet attrs) {
                super(context, attrs);
                init();
            }

            private void init() {
                PAINT.setColor(Color.WHITE);
                PAINT.setAntiAlias(true);
                float textPixelSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                        TRADE_MARK_TEXT_SIZE_SP, getResources().getDisplayMetrics());
                PAINT.setTextSize(textPixelSize);
            }

            @Override
            public void onDraw(Canvas canvas) {
                super.onDraw(canvas);
                drawTradeMark(canvas);
            }

            private void drawTradeMark(Canvas canvas) {
                Rect framingRect = getFramingRect();
                float tradeMarkTop;
                float tradeMarkLeft;
                if (framingRect != null) {
                    tradeMarkTop = framingRect.bottom + PAINT.getTextSize() + 10;
                    tradeMarkLeft = framingRect.left;
                } else {
                    tradeMarkTop = 10;
                    tradeMarkLeft = canvas.getHeight() - PAINT.getTextSize() - 10;
                }
                canvas.drawText(TRADE_MARK_TEXT, tradeMarkLeft, tradeMarkTop, PAINT);
            }
        }
    }

BaseScannerActivity.java

public class BaseScannerActivity extends AppCompatActivity {
    public void setupToolbar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        final ActionBar ab = getSupportActionBar();
        if(ab != null) {
            ab.setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Upvotes: 0

Views: 1608

Answers (1)

localhost
localhost

Reputation: 5598

I would suggest you to use Zbar (from same repo). We had some performance issues with Zxing and had to switch to Zbar. Using in production for like 2 years - no issues.

Upvotes: 1

Related Questions