@@ -12,8 +12,12 @@ export function detectBarcodes({barcodeDetector, video, signal} = {}) {
12
12
13
13
async function _detect ( { barcodeDetector, video, signal, resolve, reject} ) {
14
14
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 ) ;
17
21
if ( barcodes . length > 0 ) {
18
22
return resolve ( barcodes ) ;
19
23
}
@@ -27,3 +31,32 @@ async function _detect({barcodeDetector, video, signal, resolve, reject}) {
27
31
reject ( error ) ;
28
32
}
29
33
}
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