Search code examples
androidtimerqr-codezxingscanning

Stop Scaning for Barcodes by Zxing in Android


I'm developing an Android application using Zxing library to read QR codes. Now I'm almost done and only one question is left.

I need app to stop scanning for 10 seconds after certain event. I have put the thread to sleep 10 seconds and it's not the way I wanted. I want to app to act as everything is normal. But it shouldn't let users know that it's not scanning anything.

Can you explain how to do that. I can use timer method. But which method in Zxing should I stop for 10 seconds?


Solution

  • I made it working as I expected. All the decoding things are done by handleDecode method in Capture activity class. So I initialized a boolean variable inside onCreate,

    boolean isTensecondsFinished = ture;
    

    Then I check the isTensecondsFinished variable for decode QR code. Just like this,

    public void handleDecode(Result rawResult, Bitmap barcode) {
        inactivityTimer.onActivity();
        lastResult = rawResult;
        ResultHandler resultHandler = ResultHandlerFactory.makeResultHandler(
                this, rawResult);
    
        boolean fromLiveScan = barcode != null;
        if (fromLiveScan) {
            // historyManager.addHistoryItem(rawResult, resultHandler);
            // Then not from history, so beep/vibrate and we have an image to
            // draw on
            if (isTenSecondsFinished) {
                isTenSecondsFinished = false;
                Timer tenSecondsTimer = new Timer();
                tenSecondsTimer.schedule(new TimerTask() {
    
                    @Override
                    public void run() {
                        isTenSecondsFinished = true;
    
                    }
                }, 10 * 1000);
                                //Do the decoding stuff here then.
                        }
                }
    }
    

    I think this is the simplest solution.