nisrulz / qreader

:white_square_button: [Android Library] Read QR codes using google's mobile vision api, but without the hassle
http://nisrulz.github.io/qreader
Apache License 2.0
371 stars 108 forks source link

How can I read QR code once and stop QrReader #64

Closed MrSilverstein closed 3 years ago

MrSilverstein commented 5 years ago

As in the examples, the QREeader reads continous. When I once read

 qreader = QREader.Builder(view.context, surface_view, QRDataListener { data ->
            printReadQrCode(data)
        }).facing(QREader.BACK_CAM)
                .enableAutofocus(true)
                .height(surface_view.height)
                .width(surface_view.width)
                .build()
    }

Here it prints continueous the read data object as long as I hold the scanner to the qr code. I would like to call printReadQrCode(data) only once it was read successfully.. then stop the qreader and resume if required.

also

myTextView.post(new Runnable() {
                @Override
                public void run() {
                    myTextView.setText(data);
                }
            });

wont help, since I do not have any Views and I do not want to add artificially some View elements..

MahmoudMabrok commented 5 years ago

call qReader.stop() after first scan. and use qReader.start() when need to start scanning again.

BhagirathParmar commented 4 years ago

private void SetupQREader() { qrEader = new QREader.Builder(this, id_SurfaceView, new QRDataListener() { @Override public void onDetected(final String data) { Log.d("QREader", "Value : " + data);

            result.post(new Runnable() {
                @Override
                public void run() {
                    String msg;
                    result.setText(data);
                    qrEader.stop();
                    msg = result.getText().toString();
                    Log.e("XXXXX", msg);
                    Intent intent = new Intent(ac, QrCodeResult.class);
                    intent.putExtra("RESULT", msg);
                    startActivity(intent);
                }
            });
        }
    }).facing(QREader.BACK_CAM)
            .enableAutofocus(true)
            .height(id_SurfaceView.getHeight())
            .width(id_SurfaceView.getWidth())
            .build();
}

still getting Log two times ? why ? not understanding. and what is the use of qrEader.wait(); and how do i use and where to use?

call qReader.stop() after first scan. and use qReader.start() when need to start scanning again.

trix0 commented 4 years ago
        public void onDetected(final String data) {
            Log.d("QREader", "Value : " + data);
            Gson Gson = new Gson();
            try {
                QrCodeMessage qrCodeMessage = Gson.fromJson(data, QrCodeMessage.class);
                if(qrCodeMessage!=null){
                    if(qrCodeMessage.company.equals("MyTestData")){
                        Log.d(TAG,"Valid QR code");
                        qrEader.stop();
                    }
                }
            }
            catch (Exception e){
                Log.d(TAG,"Couldn't parse");
            }
        }

@Override
public void onResume() {
    super.onResume();
    qrEader.initAndStart(mySurfaceView);
    qrEader.start();
    mySurfaceView.setOnClickListener((click)->{
        qrEader.start();
    });
}

onclick throws java.lang.IllegalStateException: Camera already started!

but It Doesn't scan again. Any idea how to fix this ?

MahmoudMabrok commented 4 years ago

@trix0 u start it before click, why start it again surround it with try /catch

trix0 commented 4 years ago

Yes, when I find correct QR code, I stop it in the callback. If the QR code is not valid I want to click on window and start it again. Moment I click on window I get an error. If I do check if camera is running, it won't work at all.

First start in on resume is when window is loaded or no?

I understand that if I put it inside try catch it won't kill my app but it will not start scanning again.

On Fri, 4 Sep 2020, 16.43 Mahmoud Mabrok Fouad, notifications@github.com wrote:

@trix0 https://github.com/trix0 u start it before click, why start it again surround it with try /catch

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/nisrulz/qreader/issues/64#issuecomment-687190633, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFZ7IXJ3W35YA5JUWB24WLTSED4KZANCNFSM4HDKM63Q .

MahmoudMabrok commented 4 years ago

@trix0
with try and catch it will first try to start if it failed which mean it already started so catch will be called so app is safe and also app function is right

nisrulz commented 3 years ago

The behavior is correct. The library when it was initially written didn't allow for restarting without stopping it. This issue is too old now and the library would undergo upgrades soon. Open a new one if the issue persists.