QR Code Scanning and Reading

Hey there Bubblers.

I’ve been searching in forum for a proper QR Code scanning functionality and I saw that there a lot of people waiting for that feature to come or at least to have a proper manual how to implement this.

So I’ve spent whole day and did “something”.
Disclaimer: this might looking not the most “optimized”, native or something else, BUT it does what need to be done.
This example might look overcomplicated but if you’ll get idea - you can handle many other scenarios.

So we need couple of plugin to make things working:

It might be some that all of those plugins will be not necessary in your case and you can found “programmable” workaround but yet still.

Proof of concept idea is: Scan QR Code of entity (Part) and attach it to Current User - Current AirPlane field.

  1. First we need to generate QR Code that we will scan after. Make sure your database entity holds Image field. In my case it will be: Entity: Part QRCode: Image
  2. Just create button - Generate and add Step 1 - Create New Part - Step 2 - Change entry that we created in Step 1 and add QR Code based on ID
  3. For a testing purpose I’m assigning Create part in Step 1 in Page State and then Displaying generated QR code right away
    image

OK So we have something to scan. Let’s create another page which will be using for scanning.
Since we’re be scanning with our mobile phone - let’s make mobile focused design.

  1. Switch your page to Mobile width

  2. Add HTML element with this code.
    <video id="preview"></video>
    This element will hold our preview image from the front camera.

  3. Add LOADERR KW plugin and JavaScript to Bubble ellements. Don’t worry - they won’t be vissible on your page.

  4. Click on LOADER KW ellement and add this link to Script URL - https://rawgit.com/schmich/instascan-builds/master/instascan.min.js and Set Execution on page load to No

  5. Now click on Javascript to Bubble element and name it for example: Results. So your variable will be called - bubble_fn_result. Make sure you’re remember it. Check box right next to Publish Value. This element will hold our scanning results so make sure you’re set proper Data Type of what your’re scanning. In our case this entity - Part, but in your case it might be anything else.

  6. Now add Scan button and go to Workflow

  7. On first step we’re Loading remote js file and on Step 2 we’re adding 1000 time out so that our script would loud.


    Sure we can pre-load this script on page load but I’m trying to use thing only when those are really needed

  8. On Step 3 we’re adding JavaScript that will handle scanning itself and will push scanned value to our Javascript to Bubble variable

    let scanner = new Instascan.Scanner({ video: document.getElementById(‘preview’), mirror: false});
    scanner.addListener(‘scan’, function (content) {
    // Adding scanned content to Javascript to Bubble variable in Step 5
    bubble_fn_result(content);
    });
    Instascan.Camera.getCameras().then(function (cameras) {
    if (cameras.length > 1) {
    scanner.start(cameras[1]);
    } else {
    console.error(‘No cameras found.’);
    }
    }).catch(function (e) {
    console.error(e);
    });

This script is scanning cameras on your phone and if founds front camera - switches to it without mirroring and pushes image to our HTML VIDEO element

  1. Now we’re creating separate Workflow that will wait for our Javascript to Bubble value appear and do something with that
  2. It might look like this
    image
    After value in JavaScript to Bubble element appears Do something with that. In our case: I will add scanned Part to Current User Part
  3. So next just browse this page with your mobile phone, accept dialog box after you click Scan (one time action) and scan generated QR COde.

So that’s it. After that you can do whatever you want to do: navigate user to a different page, hide preview screen or anything else.

Since I was not able to stop camera from scanning (even though there is documented how to stop it - GitHub - schmich/instascan: HTML5 QR code scanner using your webcam I couldn’t made it. So the workaround is to either refresh page or navigate user to a different page. But I’m sure workaround will be found here very fast.

Thanks for reading. I hope this tutorial will help to achieve your goals.

P.S. I’m not sure if this works on iOS devices since I don’t have one to test.

5 Likes

Ey up,

I 've now added a free plugin based on Instascan that does all of this without the need for any Javascript, and even allows you to select the camera you want to use from a list (as all devices use different camera IDs unfortunately!) Also the original Instascan doesn’t work with the Back camera on iOS devices but I use a modified version of it which fixes that and still works on all other devices too.

You just need to add the QR Code Scanner element to the page where you want the video preview to be, and it will select the correct camera, read the QR code and save it as a state on the element, along with the image of the frame used to decode the QR code.

You can check it out @ https://bubble.io/plugin/qr-code-scanner-1593701658260x162495678044438530

Stuart

1 Like

@talkingoldfish thanks for posting up info about this new Plugin.
QR Code Scanner uses same instascan JS plugin as in mine instruction but wrapped up as a plugin and added additional functionality.

Kudos to Stuart B - https://bubble.io/contributor/stuart-b-1571239627796x203644270136879680

Thanks, I was doing it the same way as you for an app I created too and it has been working great for a while now. But I just started learning to create plugins and thought that 1 element on a page with simple workflows made more sense than having the HTML element, the Javascript to Bubble element and the Javascript workflows, plus amending the Javascript was a pain (specially as I was developing for a company that uses iOS devices to scan voucher QRs when I only use Android!) I also created a free Colour QR generator plugin for the same app which works pretty well too, (that uses the excellent but easy to use QRious library at https://github.com/neocotic/qrious).

There are paid apps out there that do QR scanning already but I figure it’s always good to have a free alternative!

Hello @talkingoldfish. I have a static qrcode image on the page. That qrcode image has a link to other service. I’d like to build a feature on that page that allows user click on icon/button to scan that image and open it’s link. By that way the user does not need to use another phone to scan the image.
I wonder if your Plugin captioned here can help me with that feature or not? If yes, pls advise me how to do it.
Thank you very much for help!
P.N.Quang

unfortunately, after step 7 I didn’t understand what to do …
could you help me with it?