Skip to content

Commit

Permalink
Replace barcode reading library
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMikes committed May 12, 2024
1 parent 809763d commit 6715eee
Show file tree
Hide file tree
Showing 4 changed files with 1,123 additions and 1,575 deletions.
48 changes: 21 additions & 27 deletions assets/controllers/barcode_scanner_controller.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
import { Controller } from '@hotwired/stimulus';
import Quagga from 'quagga';
import { BrowserMultiFormatReader, NotFoundException } from '@zxing/library';

export default class extends Controller {
static targets = ["stream"];
static targets = ["video"]

connect() {
Quagga.init({
inputStream: {
name: "Live",
type: "LiveStream",
target: this.streamTarget, // This is where the video will be attached
constraints: {
facingMode: "environment"
},
},
decoder: {
readers: ["ean_reader"]
},
}, (err) => {
if (err) {
console.error(err);
return;
}
Quagga.start();
});

Quagga.onDetected(this.onDetected.bind(this));
this.reader = new BrowserMultiFormatReader();
this.startScanner();
}

onDetected(result) {
console.log(result.codeResult.code); // Log barcode to console or handle as needed
// Optionally, send the code to your backend here
async startScanner() {
try {
const videoInputDevices = await this.reader.listVideoInputDevices();
const selectedDeviceId = videoInputDevices[0].deviceId;
this.reader.decodeFromVideoDevice(selectedDeviceId, this.videoTarget, (result, err) => {
if (result) {
console.log(result);
// Process result here (e.g., fetch data from backend)
}
if (err && !(err instanceof NotFoundException)) {
console.error(err);
}
});
} catch (error) {
console.error('Error with ZXing:', error);
}
}

disconnect() {
Quagga.stop();
this.reader.reset();
}
}
Loading

0 comments on commit 6715eee

Please sign in to comment.