Skip to content

Commit 0c36fb6

Browse files
committed
support CC-B
1 parent fc84053 commit 0c36fb6

File tree

1 file changed

+33
-7
lines changed
  • scenarios/read-and-parse-GS1-AI/scan-using-rtu-api

1 file changed

+33
-7
lines changed

scenarios/read-and-parse-GS1-AI/scan-using-rtu-api/index.html

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@
9292

9393
while(!parseSuccess){
9494

95-
let result;
95+
let result, is_EANUPC;
9696
try{
97-
result = await funcOpenScanner();
97+
[result, is_EANUPC] = await funcOpenScanner();
9898
}catch(ex){
9999
alert(`Scanner error: ${ex.message || ex}`);
100100
// if scanner throw error, most time it can't work anymore, just stop then debug
@@ -109,7 +109,7 @@
109109
await funcHandleBarcodeImageAndText(result);
110110

111111
try{
112-
await funcParseResult(result);
112+
await funcParseResult(result, is_EANUPC);
113113
parseSuccess = true;
114114
}catch(ex){
115115
// maybe just because format not supported
@@ -124,6 +124,8 @@
124124
};
125125

126126
const funcOpenScanner = async () => {
127+
let is_EANUPC = false;
128+
127129
// Initialize the Dynamsoft Barcode Scanner
128130
const barcodeScanner = new Dynamsoft.BarcodeScanner({
129131
// Please don't forget to replace YOUR_LICENSE_KEY_HERE
@@ -136,8 +138,22 @@
136138
showCloseButton: false,
137139
cameraSwitchControl: "toggleFrontBack",
138140
},
139-
onInitReady: (components) => {
141+
onInitReady: async(components) => {
140142
// 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+
141157
// Set the scan laser to be visible in cameraView
142158
components.cameraView.setScanLaserVisible(true);
143159

@@ -154,7 +170,7 @@
154170
});
155171

156172
// Launch the scanner and wait for the result
157-
return await barcodeScanner.launch();
173+
return [await barcodeScanner.launch(), is_EANUPC];
158174
};
159175

160176
const funcHandleBarcodeImageAndText = async(result)=>{
@@ -178,15 +194,25 @@
178194

179195
}
180196

181-
const funcParseResult = async(result)=>{
197+
const funcParseResult = async(result, is_EANUPC)=>{
182198

183199
const barcodeResult = result.barcodeResults[0];
200+
let bytes = barcodeResult.bytes;
184201

185202
const strNotValidGS1AI = "Not a GS1-compliant barcode.";
186203
// Parse GS1 barcode
187204
let parseResult;
188205
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);
190216
}catch(ex){
191217
console.error(ex);
192218
throw Error(strNotValidGS1AI);

0 commit comments

Comments
 (0)