I am creating a node.js QR code reader. From experience, I have found that the most reliable and efficient reader is the built-in Chrome barcode detector API. Thus, I decided to build this application with a Puppeteer instance running Chrome. I successfully made the app pass in an image with a QR code to a Puppeteer browser instance, scan the QR code, and then return the result. It works as intended on my Mac. I packaged this functionality in an express app to make a scanning API. Currently, I am trying to deploy it on the web using a service that requires a docker container. However, I am struggling to make a Docker file that can runs the application.
I have tried installing Chrome in these three ways: 1, 2, 3. All of them have failed. Upon further searching, I discovered that Chrome supports the Barcode Detection API only on OS devices. Therefore, I need a way to ensure the docker container runs an OS version of Chrome.
If it is helpful, here is the function I am running with Puppeteer that works locally:
const puppeteer = require('puppeteer');
function scanQR = async (src) => {
try {
const browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
const page = await browser.newPage();
await page.setJavaScriptEnabled(true);
await page.setCacheEnabled(true);
await page.goto('https://nimiq.github.io/qr-scanner/demo/');
const barcodeDetectorPromise = page.evaluate(
async (src) =>
new Promise(async (resolve, reject) => {
const image = new Image();
image.src = src;
// eslint-disable-next-line no-undef
const barcodeReader = new BarcodeDetector({ formats: ['qr_code'] });
await new Promise((resolve, reject) => (image.onload = resolve));
resolve(await barcodeReader.detect(image));
}),
src
);
const timeoutPromise = new Promise((resolve, reject) => {
setTimeout(() => resolve([]), 2000);
});
const result = await Promise.race([barcodeDetectorPromise, timeoutPromise]);
await browser.close();
return result;
} catch {
return [];
}
};
The src is a link to an image with a QR code such as a base64URL whose image data includes a QR.