@capgo/camera-preview 8.3.8 → 8.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/plugin.js CHANGED
@@ -55,6 +55,9 @@ var capacitorCapacitorCameraView = (function (exports, core) {
55
55
  this.mediaRecorder = null;
56
56
  this.recordedChunks = [];
57
57
  this.currentAspectRatio = '4:3';
58
+ this.barcodeDetector = null;
59
+ this.barcodeScannerTimer = null;
60
+ this.barcodeScannerBusy = false;
58
61
  }
59
62
  async checkPermissions(options) {
60
63
  const result = {
@@ -539,12 +542,23 @@ var capacitorCapacitorCameraView = (function (exports, core) {
539
542
  height: computedStyle.height,
540
543
  },
541
544
  });
542
- return {
545
+ const result = {
543
546
  width: Math.round(rect.width),
544
547
  height: Math.round(rect.height),
545
548
  x: Math.round(rect.x),
546
549
  y: Math.round(rect.y),
547
550
  };
551
+ const barcodeScannerOptions = this.getStartBarcodeScannerOptions(options);
552
+ if (barcodeScannerOptions) {
553
+ try {
554
+ await this.startBarcodeScanner(barcodeScannerOptions);
555
+ }
556
+ catch (error) {
557
+ await this.stop({ force: true });
558
+ throw error;
559
+ }
560
+ }
561
+ return result;
548
562
  }
549
563
  stopStream(stream) {
550
564
  if (stream) {
@@ -554,6 +568,7 @@ var capacitorCapacitorCameraView = (function (exports, core) {
554
568
  }
555
569
  }
556
570
  async stop(_options) {
571
+ await this.stopBarcodeScanner();
557
572
  const video = document.getElementById(DEFAULT_VIDEO_ID);
558
573
  if (video) {
559
574
  video.pause();
@@ -641,6 +656,116 @@ var capacitorCapacitorCameraView = (function (exports, core) {
641
656
  async captureSample(_options) {
642
657
  return this.capture(_options);
643
658
  }
659
+ async startBarcodeScanner(options) {
660
+ var _a, _b;
661
+ if (!this.isStarted || !((_a = this.videoElement) === null || _a === void 0 ? void 0 : _a.srcObject)) {
662
+ throw new Error('camera is not running');
663
+ }
664
+ const Detector = window.BarcodeDetector;
665
+ if (!Detector) {
666
+ throw new Error('BarcodeDetector API is not available in this browser');
667
+ }
668
+ await this.stopBarcodeScanner();
669
+ const formats = ((options === null || options === void 0 ? void 0 : options.formats) || [])
670
+ .map((format) => this.toWebBarcodeFormat(format))
671
+ .filter((format) => !!format);
672
+ this.barcodeDetector = new Detector(formats.length > 0 ? { formats } : undefined);
673
+ const detectionInterval = Math.max(100, (_b = options === null || options === void 0 ? void 0 : options.detectionInterval) !== null && _b !== void 0 ? _b : 500);
674
+ const detect = async () => {
675
+ var _a;
676
+ if (this.barcodeScannerBusy || !this.barcodeDetector || !((_a = this.videoElement) === null || _a === void 0 ? void 0 : _a.srcObject)) {
677
+ return;
678
+ }
679
+ this.barcodeScannerBusy = true;
680
+ try {
681
+ const results = (await this.barcodeDetector.detect(this.videoElement));
682
+ const barcodes = results
683
+ .map((result) => this.toBarcodeScanResult(result))
684
+ .filter((barcode) => !!barcode);
685
+ if (barcodes.length > 0) {
686
+ this.notifyListeners('barcodeScanned', { barcodes });
687
+ }
688
+ }
689
+ catch (error) {
690
+ const message = error instanceof Error ? error.message : String(error);
691
+ console.error('Barcode detection failed:', error);
692
+ this.notifyListeners('barcodeScanError', { message });
693
+ }
694
+ finally {
695
+ this.barcodeScannerBusy = false;
696
+ }
697
+ };
698
+ this.barcodeScannerTimer = window.setInterval(() => {
699
+ void detect();
700
+ }, detectionInterval);
701
+ void detect();
702
+ }
703
+ async stopBarcodeScanner() {
704
+ if (this.barcodeScannerTimer !== null) {
705
+ window.clearInterval(this.barcodeScannerTimer);
706
+ this.barcodeScannerTimer = null;
707
+ }
708
+ this.barcodeDetector = null;
709
+ this.barcodeScannerBusy = false;
710
+ }
711
+ getStartBarcodeScannerOptions(options) {
712
+ if (options.barcodeScanner === true) {
713
+ return {};
714
+ }
715
+ if (options.barcodeScanner && typeof options.barcodeScanner === 'object') {
716
+ return options.barcodeScanner;
717
+ }
718
+ return null;
719
+ }
720
+ toWebBarcodeFormat(format) {
721
+ switch (format) {
722
+ case 'aztec':
723
+ case 'codabar':
724
+ case 'code_39':
725
+ case 'code_93':
726
+ case 'code_128':
727
+ case 'data_matrix':
728
+ case 'ean_8':
729
+ case 'ean_13':
730
+ case 'itf':
731
+ case 'pdf417':
732
+ case 'qr_code':
733
+ case 'upc_a':
734
+ case 'upc_e':
735
+ return format;
736
+ default:
737
+ return null;
738
+ }
739
+ }
740
+ toBarcodeScanResult(result) {
741
+ if (!result.rawValue) {
742
+ return null;
743
+ }
744
+ return {
745
+ value: result.rawValue,
746
+ format: this.fromWebBarcodeFormat(result.format),
747
+ };
748
+ }
749
+ fromWebBarcodeFormat(format) {
750
+ switch (format) {
751
+ case 'aztec':
752
+ case 'codabar':
753
+ case 'code_39':
754
+ case 'code_93':
755
+ case 'code_128':
756
+ case 'data_matrix':
757
+ case 'ean_8':
758
+ case 'ean_13':
759
+ case 'itf':
760
+ case 'pdf417':
761
+ case 'qr_code':
762
+ case 'upc_a':
763
+ case 'upc_e':
764
+ return format;
765
+ default:
766
+ return 'unknown';
767
+ }
768
+ }
644
769
  async stopRecordVideo() {
645
770
  if (!this.mediaRecorder) {
646
771
  throw new Error('video recording is not running');