Android Zxing Barcode Scanner扫描不正确

时间:2016-03-29 10:37:39

标签: android barcode zxing barcode-scanner

在我的Android application中,我正在使用Barcode扫描功能。对于此功能,我使用Android https://github.com/dm77/barcodescanner库进行扫描Barcodes。它工作得很好但是如果我在我的项目中反复扫描,有时它会返回wrong value(not the actual value)条形码。我想知道它为什么会发生以及如何解决这个问题。我用谷歌搜索了,但不幸的是我没有找到更好的解决方案。任何人请指导我解决这个问题。

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

1 个答案:

答案 0 :(得分:1)

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