Skip to content

Commit 36f633c

Browse files
committed
Isolate detectBarcode function.
1 parent 15e8fe2 commit 36f633c

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

components/BarcodeScanner.vue

+23-20
Original file line numberDiff line numberDiff line change
@@ -164,28 +164,9 @@ export default {
164164
return;
165165
} else {
166166
const barcodeDetector = new BarcodeDetector({formats: ['pdf417']});
167-
const detectBarcode = async video => {
168-
try {
169-
// Detect barcodes in the current video frame
170-
const barcodes = await barcodeDetector.detect(video);
171-
if(barcodes.length > 0) {
172-
barcodes.forEach(barcode => {
173-
const {format, rawValue} = barcode;
174-
if(format && rawValue) {
175-
console.log('Barcode Detection API:', barcode);
176-
emit('result', {type: format, text: rawValue});
177-
}
178-
});
179-
}
180-
} catch(error) {
181-
console.error('Barcode detection failed:', error);
182-
}
183-
// Schedule the next frame check
184-
video.requestVideoFrameCallback(() => detectBarcode(video));
185-
};
186167
// Start the detection loop
187168
videoElement.requestVideoFrameCallback(() =>
188-
detectBarcode(videoElement)
169+
detectBarcode(barcodeDetector, videoElement, emit)
189170
);
190171
}
191172
}
@@ -271,6 +252,28 @@ export default {
271252
}
272253
};
273254
255+
async function detectBarcode(barcodeDetector, video, emit) {
256+
try {
257+
// Detect barcodes in the current video frame
258+
const barcodes = await barcodeDetector.detect(video);
259+
if(barcodes.length > 0) {
260+
barcodes.forEach(barcode => {
261+
const {format, rawValue} = barcode;
262+
if(format && rawValue) {
263+
console.log('Barcode Detection API:', barcode);
264+
emit('result', {type: format, text: rawValue});
265+
}
266+
});
267+
}
268+
} catch(error) {
269+
console.error('Barcode detection failed:', error);
270+
}
271+
// Schedule the next frame check
272+
video.requestVideoFrameCallback(() =>
273+
detectBarcode(barcodeDetector, video, emit)
274+
);
275+
}
276+
274277
// see: `BarcodeFormat`
275278
// https://wicg.github.io/shape-detection-api/#enumdef-barcodeformat
276279
const FORMAT_MAP = new Map([

0 commit comments

Comments
 (0)