Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import UIKit

final class ImportAction: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

private var completionHandler: (UIImage?)->()
private var completionHandler: (UIImage?, URL?)->()

init(completionHandler: @escaping (UIImage?)->()) {
init(completionHandler: @escaping (UIImage?, URL?)->()) {
self.completionHandler = completionHandler
}

Expand All @@ -27,12 +27,14 @@ final class ImportAction: NSObject, UIImagePickerControllerDelegate, UINavigatio
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
let path = info[UIImagePickerController.InfoKey.imageURL] as? URL // UIImagePickerControllerImageURL
picker.presentingViewController?.dismiss(animated: true, completion: nil)
completionHandler(image)

completionHandler(image, path)
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.presentingViewController?.dismiss(animated: true, completion: nil)
completionHandler(nil)
completionHandler(nil, nil)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class ReviewDocumentsViewController: UIViewController {
}

@IBAction private func importButtonTapped(_ item: UIBarButtonItem) {
importAction = ImportAction { [weak self] image in
importAction = ImportAction { [weak self] (image, path) in
defer { self?.importAction = nil }
guard let image = image else {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ final class StaticImageDetectionViewController: UIViewController {
}
}

private var imagePath: URL?

private var importAction: ImportAction?
private var detectorsManager: DetectorsManager?
private var alertsManager: AlertsManager?
Expand All @@ -35,8 +37,9 @@ final class StaticImageDetectionViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
importAction = ImportAction(completionHandler: { [weak self] image in
importAction = ImportAction(completionHandler: { [weak self] (image, path) in
self?.image = image
self?.imagePath = path
})
detectorsManager = DetectorsManager(delegate: self)
alertsManager = AlertsManager(presenter: self)
Expand Down Expand Up @@ -73,7 +76,7 @@ final class StaticImageDetectionViewController: UIViewController {
for detector in detectorsManager.allDetectors {
let action = UIAlertAction(title: detector.detectorName, style: .default) { [weak self] _ in
guard let image = self?.image else { return }
detectorsManager.detectInfo(on: image, using: detector)
detectorsManager.detectInfo(on: image, orFilePath: self?.imagePath ?? URL(fileURLWithPath: ""), using: detector)
}
alert.addAction(action)
}
Expand Down Expand Up @@ -116,6 +119,10 @@ extension StaticImageDetectionViewController: ScannerCameraViewControllerDelegat
}

extension StaticImageDetectionViewController: DetectorsManagerDelegate {
func scanner(_ generator: ScanbotSDK.SBSDKPDFGenerator, didFindPolygon isSuccess: Bool?) {
alertsManager?.showSuccessAlert(with: "Success")
}


func scanner(_ scanner: SBSDKBarcodeScanner,
didFindBarcodes result: SBSDKBarcodeScannerResult?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@ protocol DetectorsManagerDelegate: AnyObject {
func scanner(_ scanner: SBSDKCheckScanner,
didScanCheck result: SBSDKCheckScanningResult?)

func scanner(_ scanner: SBSDKCreditCardScanner,
func scanner(_ scanner: SBSDKCreditCardScanner,
didScanCreditCard result: SBSDKCreditCardScanningResult?)

func scanner(_ scanner: SBSDKMRZScanner,
didScanMRZ result: SBSDKMRZScannerResult?)

func scanner(_ scanner: SBSDKDocumentScanner, didFindPolygon result : SBSDKDocumentDetectionResult?)


func scanner(_ generator: SBSDKPDFGenerator, didFindPolygon isSuccess : Bool?)

}

final class DetectorsManager {
enum Detector: CaseIterable {
case barcode, ehic, genericDocument, mrz, medicalCertificate, documentScanner, check, creditCard
case barcode, ehic, genericDocument, mrz, medicalCertificate, documentScanner, check, creditCard, pdfGenerator

var detectorName: String {
switch self {
Expand All @@ -57,6 +60,8 @@ final class DetectorsManager {
return "Check Scanner"
case.creditCard:
return "Credit Card Scanner"
case.pdfGenerator:
return "PDF Generator"
}
}
}
Expand All @@ -68,7 +73,7 @@ final class DetectorsManager {
self.delegate = delegate
}

func detectInfo(on image: UIImage, using detector: Detector) {
func detectInfo(on image: UIImage, orFilePath url: URL, using detector: Detector) {
switch detector {
case .barcode:
let scanner = SBSDKBarcodeScanner()
Expand Down Expand Up @@ -107,9 +112,38 @@ final class DetectorsManager {
let scanner = SBSDKCreditCardScanner(configuration: configuration)
let result = scanner.scan(from: image)
delegate?.scanner(scanner, didScanCreditCard: result)
case .pdfGenerator:

let storage = DetectorsManager.getStorage(imagePath: url, encrypter: ScanbotUI.defaultImageStoreEncrypter)

let pdfGenerator = SBSDKPDFGenerator(configuration: SBSDKPDFConfiguration(), ocrConfiguration: nil)
let outputUrl = SBSDKStorageLocation.applicationDocumentsFolderURL.appendingPathComponent("Agoda123.pdf")

pdfGenerator.generate(from: storage, output: outputUrl) { isSuccess, error in
print("")
self.delegate?.scanner(pdfGenerator, didFindPolygon: isSuccess)
}
}
}

internal static func getStorage(
imagePath: URL,
encrypter: SBSDKStorageCrypting?
) -> SBSDKIndexedImageStorage {

let url = SBSDKStorageLocation.applicationSupportFolderURL
var pathString = url.scheme == "file" ? url.relativePath : url.absoluteString;
pathString = "\(pathString)/\(UUID().uuidString)"

let location = SBSDKStorageLocation(baseURL: URL(fileURLWithPath: pathString))
let storage = SBSDKIndexedImageStorage(storageLocation: location, fileFormat: .JPEG, encrypter: encrypter, encryptedImagesURLs: [imagePath])
if storage != nil {
return storage!
}
return SBSDKIndexedImageStorage.temporary!
}


private func makeFinderRect(for image: UIImage) -> CGRect {
let width = image.size.width
let height = image.size.height
Expand Down