Android Zxing 条码扫描器扫描不正确

Android Zxing Barcode Scanner is not scanning correctly

在我的 Android application 中,我有 Barcode 扫描功能。对于此功能,我使用 Android https://github.com/dm77/barcodescanner 库进行扫描 Barcodes。它运行良好,但如果我在我的项目中反复扫描,有时它会返回 barcode.I 的 wrong value(not the actual value) 想知道它发生的原因以及如何解决这个问题。我用谷歌搜索,但不幸的是我没有找到更好的 solution.Anyone 请指导我解决问题。

注意:我使用的是最新版本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);
    }
}

我建议您使用 Zbar(来自同一个仓库)。 我们在 Zxing 上遇到了一些性能问题,不得不切换到 Zbar。 在生产中使用大约 2 年 - 没有问题。