0

I've managed to get a qr scanner powered by google vision working and placing the qrcode into a text view on the same activity.

The end goal is to have the url in the qrcode open up in a webview in another activity (QRWebActivity) as soon as a qrcode is detected.

At this stage I was able to move the qrcode into a string and push across and open in the webview using intents activated by sendMessage2 on button click.

But I really want to find a way to have it just automatically open the QRWebActivity and send the webview to the qrCode on 'if(qrCodes.size()!=0).

Any help would be amazing.

Really sorry if I'm not using the right terminology, I just don't know what I'm doing but really keen to finish this app by the end of the week for release and I'm so close.

        barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
            @Override
            public void release() {

            }

            @Override
            public void receiveDetections(Detector.Detections<Barcode> detections) {
                final SparseArray<Barcode> qrCodes = detections.getDetectedItems();

                if(qrCodes.size()!=0)
                {
                    textView.post(new Runnable() {
                        @Override
                        public void run() {
                            Vibrator vibrator = (Vibrator)getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
                            vibrator.vibrate(1000);
                            textView.setText(qrCodes.valueAt(0).displayValue);
                        }

                    });
                }
            }
        });

    }
    public void sendMessage2 (View view)
    {
        String qrmessage = textView.getText().toString();

        Intent intent2 = new Intent(view.getContext(),QRWebActivity.class);
        intent2.putExtra("EXTRA_QRMESSAGE",qrmessage);
        startActivity(intent2);
    }
}

If I could just simulate pressing the button and triggering 'sendMessage2' when qrCodes !=0 that would do me... even though I'm sure there's a more elegant way.

  • Why don't you just call the code in `sendMessage2`...? Everything you need is there – PPartisan May 07 '19 at 13:38
  • @PPartisan Thanks, yeah that's exactly what I was hoping to do. I just know so little about coding in Java I don't know how and can't find a tutorial for something similar. Going to have a go at Joachim's suggestion, but i have a feeling that 'view.getContext' does not work without the (View view) above it. – Daniel Young May 07 '19 at 23:00
  • [link](https://stackoverflow.com/questions/5701666/can-i-click-a-button-programmatically-for-a-predefined-intent). I've tried to implement a 'button.performClick()' but I don't really know how to go about it. – Daniel Young May 07 '19 at 23:34

2 Answers2

0

From your code, here is my solution:

@Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
    final SparseArray<Barcode> qrCodes = detections.getDetectedItems();

    if (qrCodes.size() != 0) {
        textView.post(new Runnable() {
            @Override
            public void run() {
                Vibrator vibrator = (Vibrator)getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
                vibrator.vibrate(1000);
                textView.setText(qrCodes.valueAt(0).displayValue);
                sendMessage2(textView);
            }
        });
    }
}
Son Truong
  • 13,661
  • 5
  • 32
  • 58
  • Mate, solved it! This is exactly what I was after... More issues for me to solve now, the url gets sent to the webview like 20 times before it opens so when you press back it goes through about 20 of the same page before getting back to the camera. Hopefully just adding a line to turn off the camera fixes it. Thanks again! – Daniel Young May 08 '19 at 05:02
-1

All the code is already there, just dont split it up:

        public void receiveDetections(Detector.Detections<Barcode> detections) {
            final SparseArray<Barcode> qrCodes = detections.getDetectedItems();

            if(qrCodes.size()!=0)
            {
                Intent intent2 = new Intent(view.getContext(),QRWebActivity.class);
                intent2.putExtra("EXTRA_QRMESSAGE",qrCodes.valueAt(0).displayValue);
                startActivity(intent2);
                textView.post(new Runnable() {
                    @Override
                    public void run() {

                        Vibrator vibrator = (Vibrator)getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
                        vibrator.vibrate(1000);
                        textView.setText(qrCodes.valueAt(0).displayValue);
                    }

                });
            }
        }
Joachim Haglund
  • 775
  • 5
  • 15
  • Thanks for the suggestion, I tried a lot similar to this but there is something wrong with _(view.getContext()_. 'Intent intent2 = new Intent(view.getContext(),QRWebActivity.class);' The for view is in red and i have an error message "error: cannot find symbol variable view" when trying to run and "cannot resolve symbol: view" when hovering over the word. Is there a fine tweak to be made or is this method a dead end? – Daniel Young May 07 '19 at 23:07