Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ryanmortier's avatar

Has anyone implemented a barcode scanner for their webapp before?

I'm wondering what the best way to approach this is and if there are any packages out there that make this easier. I'm looking to scan QR codes with a handheld industrial barcode scanner into a webapp with PHP/Laravel.

I see some people just build a traditional form and autofocus the barcode field then auto-submit the form on keyup from the barcode scanner. This seems like it'll work most of the time but might have issues if for some reason the input was unfocused for some reason like perhaps if there are other fields on the form which I think there will be.

Is there a better way? The barcode scanner can prefix/affix characters so another way I thought I could go about capturing the data from the scan is to listen for all keyboard input in JavaScript and know that it was scanned based on the prefix/affix.

The form will have a few other fields I'm thinking like quantity entry so they don't need to scan 400 of the same barcode.

Any thoughts?

0 likes
4 replies
LaryAI's avatar

Certainly! Here’s a summary and solution for implementing barcode/QR code scanning in a webapp, especially when using PHP/Laravel on the backend.

1. Understanding Barcode Scanners

Most industrial barcode scanners act as "keyboard wedges"—they send keystrokes to the focused input, just like a user typing. This means you can use a simple <input> field and capture the scanned value.

2. Basic Approach (Autofocus Input)

You can use a dedicated input field, autofocus it, and auto-submit on input. This is the most common and reliable approach, but as you mentioned, it can fail if the input loses focus.

Example:

<input id="barcode" autofocus>
<script>
document.getElementById('barcode').addEventListener('change', function() {
    // Submit the form or handle the scanned value
});
</script>

3. Advanced Approach (Global Keyboard Listener)

If your scanner can prefix/suffix scans (e.g., with ^ at the start and $ at the end), you can listen for all keyboard input and detect scans regardless of focus.

Example:

let buffer = '';
let scanning = false;
const prefix = '^';
const suffix = '$';

document.addEventListener('keydown', function(e) {
    if (e.key.length === 1) { // Only process printable characters
        buffer += e.key;
        // Start scanning if prefix detected
        if (!scanning && buffer.endsWith(prefix)) {
            scanning = true;
            buffer = '';
        }
        // End scanning if suffix detected
        if (scanning && buffer.endsWith(suffix)) {
            scanning = false;
            const scannedValue = buffer.slice(0, -1); // Remove suffix
            buffer = '';
            // Handle scannedValue (e.g., send to your Laravel backend)
            console.log('Scanned:', scannedValue);
        }
    }
});

Make sure to configure your scanner to send the prefix/suffix.

4. Using JavaScript Libraries for Camera-based Scanning

If you want to scan QR codes using a device camera (not a hardware scanner), consider libraries like QuaggaJS or html5-qrcode.

Example with html5-qrcode:

<div id="reader"></div>
<script src="https://unpkg.com/html5-qrcode"></script>
<script>
const html5QrCode = new Html5Qrcode("reader");
html5QrCode.start(
  { facingMode: "environment" },
  { fps: 10, qrbox: 250 },
  qrCodeMessage => {
    // Handle scanned QR code
    console.log(qrCodeMessage);
  }
);
</script>

5. Backend (Laravel)

On the backend, just handle the scanned value as you would any form input.

Example Route:

Route::post('/scan', function (Request $request) {
    $barcode = $request->input('barcode');
    // Process barcode
});

6. Best Practices

  • Always provide a clear UI indication of which field is active.
  • Consider a global keyboard listener if input focus is unreliable.
  • If using a camera, use a proven JS library.
  • Validate and sanitize all scanned input on the backend.

Summary:
For most industrial barcode scanners, a focused input field works well. For more robustness, use a global keyboard listener with prefix/suffix detection. For camera-based scanning, use a JS library. Integrate with Laravel as you would any form submission.

Let me know if you need a more detailed example for any of these approaches!

Tray2's avatar

Depending on the model of scanner it works a little differently.

Scanners read the barcode/datamatrix/QR and converts that into text, which it then enters into the focused element on your screen.

The scanner can be configured to function in many different ways, for example which prefixes it should read, or if it should only read one barcode, or multiple ones in one go.

They are basically just another type of keyboard.

If you are using Zebra (TC8300) scanners with Android 10 then you can use a webkiosk, that fires an onScan function in your javascript code.

https://github.com/bdamage/webkiosk

It doesn't work with Android 13 and up due to restrictions implemented by Google.

ryanmortier's avatar

@Tray2 Interesting, this is very cool but the Android 13 restriction worry me as I don't necessarily want to be updating the application if they update the hand held scanner.

Tray2's avatar

@ryanmortier The update itself is pretty easy, it just removing the config file, and hard code the values instead.

The biggest issue is to make it build at all with the correct versions of a shitload of files that Android studio needs.

We did that update last year, sorry that I cant share it.

Please or to participate in this conversation.