@capgo/capacitor-document-scanner 8.3.24 → 8.3.25

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.
@@ -846,34 +846,22 @@ class DocScanner: NSObject {
846
846
  return className.contains("alert") || className.contains("sheet") || className.contains("prompt")
847
847
  }
848
848
 
849
+ private func finishScan(with scan: VNDocumentCameraScan) {
850
+ processImages(pageCount: limitedPageCount(total: scan.pageCount)) { index in
851
+ scan.imageOfPage(at: index)
852
+ }
853
+ }
854
+
849
855
  private func finishScan(with images: [UIImage]) {
856
+ processImages(pageCount: limitedPageCount(total: images.count)) { index in
857
+ images[index]
858
+ }
859
+ }
860
+
861
+ private func processImages(pageCount: Int, imageProvider: @escaping (Int) -> UIImage) {
850
862
  DispatchQueue.global(qos: .userInitiated).async {
851
863
  do {
852
- let limitedImages = self.limit(images: images)
853
- let results = try limitedImages.enumerated().map { index, image in
854
- let adjustedImage = self.applyBrightnessContrastIfNeeded(to: image)
855
- guard
856
- let imageData = adjustedImage.jpegData(
857
- compressionQuality: CGFloat(self.croppedImageQuality) / 100.0
858
- )
859
- else {
860
- throw RuntimeError.message("Unable to get scanned document in jpeg format.")
861
- }
862
-
863
- switch self.responseType {
864
- case ResponseType.base64:
865
- return imageData.base64EncodedString()
866
- case ResponseType.imageFilePath:
867
- let imagePath = FileUtil().createImageFile(index)
868
- try imageData.write(to: imagePath)
869
- return imagePath.absoluteString
870
- default:
871
- throw RuntimeError.message(
872
- "responseType must be \(ResponseType.base64) or \(ResponseType.imageFilePath)"
873
- )
874
- }
875
- }
876
-
864
+ let results = try self.makeResults(pageCount: pageCount, imageProvider: imageProvider)
877
865
  DispatchQueue.main.async {
878
866
  self.successHandler(results)
879
867
  }
@@ -889,11 +877,50 @@ class DocScanner: NSObject {
889
877
  }
890
878
  }
891
879
 
892
- private func limit(images: [UIImage]) -> [UIImage] {
880
+ private func makeResults(pageCount: Int, imageProvider: @escaping (Int) -> UIImage) throws -> [String] {
881
+ var results: [String] = []
882
+ results.reserveCapacity(pageCount)
883
+
884
+ for index in 0 ..< pageCount {
885
+ let result = try autoreleasepool {
886
+ try self.process(image: imageProvider(index), at: index)
887
+ }
888
+ results.append(result)
889
+ }
890
+
891
+ return results
892
+ }
893
+
894
+ private func process(image: UIImage, at index: Int) throws -> String {
895
+ let adjustedImage = applyBrightnessContrastIfNeeded(to: image)
896
+ guard
897
+ let imageData = adjustedImage.jpegData(
898
+ compressionQuality: CGFloat(croppedImageQuality) / 100.0
899
+ )
900
+ else {
901
+ throw RuntimeError.message("Unable to get scanned document in jpeg format.")
902
+ }
903
+
904
+ switch responseType {
905
+ case ResponseType.base64:
906
+ return imageData.base64EncodedString()
907
+ case ResponseType.imageFilePath:
908
+ let imagePath = FileUtil().createImageFile(index)
909
+ try imageData.write(to: imagePath)
910
+ return imagePath.absoluteString
911
+ default:
912
+ throw RuntimeError.message(
913
+ "responseType must be \(ResponseType.base64) or \(ResponseType.imageFilePath)"
914
+ )
915
+ }
916
+ }
917
+
918
+ private func limitedPageCount(total: Int) -> Int {
893
919
  guard let maxNumDocuments else {
894
- return images
920
+ return total
895
921
  }
896
- return Array(images.prefix(maxNumDocuments))
922
+
923
+ return max(0, min(total, maxNumDocuments))
897
924
  }
898
925
 
899
926
  private func applyBrightnessContrastIfNeeded(to image: UIImage) -> UIImage {
@@ -983,10 +1010,9 @@ extension DocScanner: VNDocumentCameraViewControllerDelegate {
983
1010
  _ controller: VNDocumentCameraViewController,
984
1011
  didFinishWith scan: VNDocumentCameraScan
985
1012
  ) {
986
- let images = (0 ..< scan.pageCount).map(scan.imageOfPage(at:))
987
1013
  DocScanner.resetVisionKitHackState()
988
1014
  dismiss(controller) {
989
- self.finishScan(with: images)
1015
+ self.finishScan(with: scan)
990
1016
  }
991
1017
  }
992
1018
 
@@ -3,7 +3,7 @@ import Foundation
3
3
 
4
4
  @objc(DocumentScannerPlugin)
5
5
  public class DocumentScannerPlugin: CAPPlugin, CAPBridgedPlugin {
6
- private let pluginVersion: String = "8.3.24"
6
+ private let pluginVersion: String = "8.3.25"
7
7
  public let identifier = "DocumentScannerPlugin"
8
8
  public let jsName = "DocumentScanner"
9
9
  public let pluginMethods: [CAPPluginMethod] = [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/capacitor-document-scanner",
3
- "version": "8.3.24",
3
+ "version": "8.3.25",
4
4
  "description": "Capacitor plugin to scan document iOS and Android",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",