|
92 | 92 |
|
93 | 93 | while(!parseSuccess){ |
94 | 94 |
|
95 | | - let result; |
| 95 | + let result, is_EANUPC; |
96 | 96 | try{ |
97 | | - result = await funcOpenScanner(); |
| 97 | + [result, is_EANUPC] = await funcOpenScanner(); |
98 | 98 | }catch(ex){ |
99 | 99 | alert(`Scanner error: ${ex.message || ex}`); |
100 | 100 | // if scanner throw error, most time it can't work anymore, just stop then debug |
|
109 | 109 | await funcHandleBarcodeImageAndText(result); |
110 | 110 |
|
111 | 111 | try{ |
112 | | - await funcParseResult(result); |
| 112 | + await funcParseResult(result, is_EANUPC); |
113 | 113 | parseSuccess = true; |
114 | 114 | }catch(ex){ |
115 | 115 | // maybe just because format not supported |
|
124 | 124 | }; |
125 | 125 |
|
126 | 126 | const funcOpenScanner = async () => { |
| 127 | + let is_EANUPC = false; |
| 128 | + |
127 | 129 | // Initialize the Dynamsoft Barcode Scanner |
128 | 130 | const barcodeScanner = new Dynamsoft.BarcodeScanner({ |
129 | 131 | // Please don't forget to replace YOUR_LICENSE_KEY_HERE |
|
136 | 138 | showCloseButton: false, |
137 | 139 | cameraSwitchControl: "toggleFrontBack", |
138 | 140 | }, |
139 | | - onInitReady: (components) => { |
| 141 | + onInitReady: async(components) => { |
140 | 142 | // Do something with the foundational components |
| 143 | + |
| 144 | + let irr = new Dynamsoft.CVR.IntermediateResultReceiver(); |
| 145 | + irr.onDecodedBarcodesReceived=(result, info)=>{ |
| 146 | + if(!info.isSectionLevelResult && result.decodedBarcodes.length > 0){ |
| 147 | + const barcode = result.decodedBarcodes[0]; |
| 148 | + //console.log(barcode) |
| 149 | + if(["EAN_8","EAN_13","UPC_A","UPC_E"].includes(barcode.formatString)){ |
| 150 | + is_EANUPC = true; |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + let irrmanager = components.cvRouter.getIntermediateResultManager(); |
| 155 | + await irrmanager.addResultReceiver(irr); |
| 156 | + |
141 | 157 | // Set the scan laser to be visible in cameraView |
142 | 158 | components.cameraView.setScanLaserVisible(true); |
143 | 159 |
|
|
154 | 170 | }); |
155 | 171 |
|
156 | 172 | // Launch the scanner and wait for the result |
157 | | - return await barcodeScanner.launch(); |
| 173 | + return [await barcodeScanner.launch(), is_EANUPC]; |
158 | 174 | }; |
159 | 175 |
|
160 | 176 | const funcHandleBarcodeImageAndText = async(result)=>{ |
|
178 | 194 |
|
179 | 195 | } |
180 | 196 |
|
181 | | - const funcParseResult = async(result)=>{ |
| 197 | + const funcParseResult = async(result, is_EANUPC)=>{ |
182 | 198 |
|
183 | 199 | const barcodeResult = result.barcodeResults[0]; |
| 200 | + let bytes = barcodeResult.bytes; |
184 | 201 |
|
185 | 202 | const strNotValidGS1AI = "Not a GS1-compliant barcode."; |
186 | 203 | // Parse GS1 barcode |
187 | 204 | let parseResult; |
188 | 205 | try{ |
189 | | - parseResult = await parser.parse(barcodeResult.bytes); |
| 206 | + if (is_EANUPC) { |
| 207 | + if (barcodeResult.formatString === "GS1_COMPOSITE" ){ |
| 208 | + let barcodeText = barcodeResult.text.split('|')[0]; |
| 209 | + // if EAN/UPC 's length is less than 14, add zero to the front |
| 210 | + let zero_count = 14 - barcodeText.length; |
| 211 | + // unshift '01', 48 is '0', 49 is '1' |
| 212 | + bytes = new Uint8Array([48, 49, ...new Array(zero_count).fill(48), ...bytes]); |
| 213 | + } |
| 214 | + } |
| 215 | + parseResult = await parser.parse(bytes); |
190 | 216 | }catch(ex){ |
191 | 217 | console.error(ex); |
192 | 218 | throw Error(strNotValidGS1AI); |
|
0 commit comments