Little refactor of barcode scanner

This commit is contained in:
= 2021-01-17 20:50:48 +01:00
parent 26c4406401
commit 52dfb0522f

View File

@ -106,7 +106,6 @@ const BarcodeScanner = () => {
const processSnapshot = (snapshot) => {
const bars = [];
const pixels = [];
let pixelBars = [];
@ -116,11 +115,11 @@ const BarcodeScanner = () => {
}
// normalize and convert to binary
const minPixel = Math.min(...pixels);
const maxPixel = Math.max(...pixels);
const minPixelValue = Math.min(...pixels);
const maxPixelValue = Math.max(...pixels);
const binary = pixels.reduce((arr, val) => {
const binaryValue = Math.round((val - minPixel) / (maxPixel - minPixel) * 255) > config.threshold ? 1 : 0
const binaryValue = Math.round((val - minPixelValue) / (maxPixelValue - minPixelValue) * 255) > config.threshold ? 1 : 0
return [...arr, binaryValue]
}, []);
@ -145,12 +144,12 @@ const BarcodeScanner = () => {
}
// find starting sequence
let startIndex = 0;
const minFactor = 0.5;
const maxFactor = 1.5;
let startIndex = 0;
for (let i = 3; i < pixelBars.length; i++) {
const refLength = (pixelBars[i] + pixelBars[i-1] + pixelBars[i-2]) / 3;
const refLength = (pixelBars[i] + pixelBars[i - 1] + pixelBars[i - 2]) / 3;
if (
(pixelBars[i] > (minFactor * refLength) || pixelBars[i] < (maxFactor * refLength))
&& (pixelBars[i-1] > (minFactor * refLength) || pixelBars[i-1] < (maxFactor * refLength))
@ -162,19 +161,14 @@ const BarcodeScanner = () => {
}
}
console.log("startIndex: " + startIndex );
// return if no starting sequence found
if (startIndex === 0) {
return;
}
// discard leading and trailing patterns
pixelBars = pixelBars.slice(startIndex, startIndex + 3 + 24 + 5 + 24 + 3);
console.log("pixelBars: " + pixelBars );
// calculate relative widths
const ref = (pixelBars[0] + pixelBars[1] + pixelBars[2]) / 3;
@ -187,20 +181,13 @@ const BarcodeScanner = () => {
}
const analyzePattern = (bars) => {
console.clear();
console.log("analyzing");
// determine parity first digit and reverse sequence if necessary
const first = normalize(bars.slice(3, 3 + 4), 7);
if (!isOdd(Math.round(first[1] + first[3]))) {
bars = bars.reverse();
}
// split into digits
const digits = [
normalize(bars.slice(3, 3 + 4), 7),
normalize(bars.slice(7, 7 + 4), 7),
@ -216,8 +203,6 @@ const BarcodeScanner = () => {
normalize(bars.slice(52, 52 + 4), 7)
]
console.log("digits: " + digits);
// determine parity and reverse if necessary
const parities = [];
@ -252,17 +237,13 @@ const BarcodeScanner = () => {
}
}
console.log("result: " + result);
// check digit
const checkDigit = check[parities.join('')];
// output
console.log("quality: " + quality);
if(quality < config.quality) {
setDecodedBarcode(checkDigit + result.join(''))
const barcode = checkDigit + result.join('')
setDecodedBarcode(barcode)
}
}