Skip to content

Commit 4a9f3f1

Browse files
committed
Add HSP color model scan attempt w/detecting barcodes.
1 parent 4239c40 commit 4a9f3f1

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# bedrock-vue-barcode-scanner ChangeLog
22

3+
## 1.5.0 - 2025-04-dd
4+
5+
### Added
6+
- Add a parallel scan attempt using the HSP (hue, saturation,
7+
"perceived brightness") color model when scanning for barcodes in a video
8+
frame.
9+
310
## 1.4.0 - 2025-04-03
411

512
### Changed

lib/barcodes.js

+35-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ export function detectBarcodes({barcodeDetector, video, signal} = {}) {
1212

1313
async function _detect({barcodeDetector, video, signal, resolve, reject}) {
1414
try {
15-
// detect barcodes in the current video frame
16-
const barcodes = await barcodeDetector.detect(video);
15+
// try both default barcode detection and detection w/an HSP color model
16+
const [result1, result2] = await Promise.all([
17+
barcodeDetector.detect(video),
18+
_detectWithHspLuminance({barcodeDetector, video})
19+
]);
20+
const barcodes = result1.concat(result2);
1721
if(barcodes.length > 0) {
1822
return resolve(barcodes);
1923
}
@@ -27,3 +31,32 @@ async function _detect({barcodeDetector, video, signal, resolve, reject}) {
2731
reject(error);
2832
}
2933
}
34+
35+
async function _detectWithHspLuminance({barcodeDetector, video}) {
36+
// grab image from video and update it using HSP luminance values
37+
const canvas = document.createElement('canvas');
38+
const width = video.videoWidth
39+
const height = video.videoHeight;
40+
canvas.width = width;
41+
canvas.height = height;
42+
const ctx = visibleCanvas.getContext('2d');
43+
ctx.drawImage(video, 0, 0);
44+
const imageData = ctx.getImageData(0, 0, width, height);
45+
const {data} = imageData;
46+
for(let i = 0; i < data.length; i += 4) {
47+
const luminance = _hspLuminance(
48+
data[i], data[i + 1], data[i + 2]);
49+
data[i] = data[i + 1] = data[i + 2] = luminance;
50+
}
51+
ctx.putImageData(imageData, 0, 0);
52+
53+
// try to detect barcode in updated image
54+
return barcodeDetector.detect(canvas);
55+
}
56+
57+
// HSP = hue, saturation, and "perceived brightness" -- a potential improvement
58+
// over HSL (L = "lightness") and HSV (V = "value")
59+
// see: https://alienryderflex.com/hsp.html
60+
function _hspLuminance(r, g, b) {
61+
return Math.sqrt(0.299 * r * r + 0.587 * g * g + 0.114 * b * b);
62+
}

0 commit comments

Comments
 (0)