-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathStaticImageDetectionViewController.swift
More file actions
205 lines (180 loc) · 8 KB
/
Copy pathStaticImageDetectionViewController.swift
File metadata and controls
205 lines (180 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//
// StaticImageDetectionViewController.swift
// ClassicComponentsExample
//
// Created by Danil Voitenko on 20.01.22.
// Copyright © 2022 doo GmbH. All rights reserved.
//
import UIKit
import ScanbotSDK
final class StaticImageDetectionViewController: UIViewController {
private enum Segue: String {
case presentScannerCamera
case showBarcodeResults
case showGenericDocumentResult
}
@IBOutlet private var imageView: UIImageView!
@IBOutlet private var importPhotoButton: UIButton!
@IBOutlet private var selectDetectorButton: UIButton!
private var image: UIImage? {
didSet {
updateUI()
}
}
private var imagePath: URL?
private var importAction: ImportAction?
private var detectorsManager: DetectorsManager?
private var alertsManager: AlertsManager?
private var barcodeResults: [SBSDKBarcodeItem]?
private var documentDataExtractorResult: SBSDKDocumentDataExtractionResult?
override func viewDidLoad() {
super.viewDidLoad()
importAction = ImportAction(completionHandler: { [weak self] (image, path) in
self?.image = image
self?.imagePath = path
})
detectorsManager = DetectorsManager(delegate: self)
alertsManager = AlertsManager(presenter: self)
updateUI()
}
@IBAction private func importPhotoButtonPressed(_ sender: Any?) {
let alert = UIAlertController(title: nil,
message: nil,
preferredStyle: .actionSheet)
let fromCameraAction = UIAlertAction(title: "From Camera", style: .default) { [weak self] _ in
self?.performSegue(withIdentifier: Segue.presentScannerCamera.rawValue, sender: self)
}
let fromLibraryAction = UIAlertAction(title: "From Photo Library", style: .default) { [weak self] _ in
guard let self = self else { return }
self.importAction?.showImagePicker(on: self)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { _ in
alert.presentingViewController?.dismiss(animated: true, completion: nil)
}
alert.addAction(fromCameraAction)
alert.addAction(fromLibraryAction)
alert.addAction(cancelAction)
alert.popoverPresentationController?.sourceView = importPhotoButton
present(alert, animated: true, completion: nil)
}
@IBAction private func selectDetectorButtonPressed(_ sender: Any?) {
let alert = UIAlertController(title: nil,
message: nil,
preferredStyle: .actionSheet)
guard let detectorsManager = detectorsManager else { return }
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, orFilePath: self?.imagePath ?? URL(fileURLWithPath: ""), using: detector)
}
alert.addAction(action)
}
let cancel = UIAlertAction(title: "Cancel", style: .cancel) { _ in
alert.presentingViewController?.dismiss(animated: true, completion: nil)
}
alert.addAction(cancel)
alert.popoverPresentationController?.sourceView = selectDetectorButton
present(alert, animated: true, completion: nil)
}
func updateUI() {
imageView.image = image
selectDetectorButton.isEnabled = image != nil
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == Segue.presentScannerCamera.rawValue {
if let controller = segue.destination as? ScannerCameraViewController {
controller.delegate = self
}
} else if segue.identifier == Segue.showBarcodeResults.rawValue {
if let controller = segue.destination as? BarcodeScannerResultsViewController {
controller.results = barcodeResults
}
} else if segue.identifier == Segue.showGenericDocumentResult.rawValue {
if let controller = segue.destination as? DocumentDataExtractorResultViewController {
controller.document = documentDataExtractorResult?.document
controller.sourceImage = image
}
}
}
}
extension StaticImageDetectionViewController: ScannerCameraViewControllerDelegate {
func cameraViewController(_ viewController: ScannerCameraViewController, didCapture image: UIImage) {
self.image = image
}
}
extension StaticImageDetectionViewController: DetectorsManagerDelegate {
func scanner(_ generator: ScanbotSDK.SBSDKPDFGenerator, didFindPolygon isSuccess: Bool?) {
alertsManager?.showSuccessAlert(with: "Success")
}
func scanner(_ scanner: SBSDKBarcodeScanner,
didFindBarcodes result: SBSDKBarcodeScannerResult?) {
guard let result = result, !result.barcodes.isEmpty else {
alertsManager?.showFailureAlert()
return
}
barcodeResults = result.barcodes
performSegue(withIdentifier: Segue.showBarcodeResults.rawValue, sender: self)
}
func recognizer(_ recognizer: SBSDKHealthInsuranceCardRecognizer,
didFindEHIC result: SBSDKEuropeanHealthInsuranceCardRecognitionResult?) {
guard let result = result,
result.status == SBSDKEuropeanHealthInsuranceCardRecognitionResultRecognitionStatus.success else {
alertsManager?.showFailureAlert()
return
}
alertsManager?.showSuccessAlert(with: result.toJson())
}
func extractor(_ extractor: SBSDKDocumentDataExtractor,
didExtractDocument result: SBSDKDocumentDataExtractionResult?) {
guard let result = result else {
alertsManager?.showFailureAlert()
return
}
documentDataExtractorResult = result
performSegue(withIdentifier: Segue.showGenericDocumentResult.rawValue, sender: self)
}
func scanner(_ scanner: SBSDKMRZScanner,
didScanMRZ result: SBSDKMRZScannerResult?) {
guard let result = result else {
alertsManager?.showFailureAlert()
return
}
alertsManager?.showSuccessAlert(with: result.toJson())
}
func scanner(_ scanner: SBSDKMedicalCertificateScanner,
didScanMedicalCertificate result: SBSDKMedicalCertificateScanningResult?) {
guard let result = result, result.scanningSuccessful else {
alertsManager?.showFailureAlert()
return
}
alertsManager?.showSuccessAlert(with: result.toJson())
}
func scanner(_ recognizer: SBSDKCheckScanner,
didScanCheck result: SBSDKCheckScanningResult?) {
guard let result = result,
result.status == .success else {
alertsManager?.showFailureAlert()
return
}
alertsManager?.showSuccessAlert(with: result.toJson())
}
func scanner(_ recognizer: SBSDKCreditCardScanner,
didScanCreditCard result: SBSDKCreditCardScanningResult?) {
guard let result = result, result.creditCard != nil else {
alertsManager?.showFailureAlert()
return
}
alertsManager?.showSuccessAlert(with: result.stringRepresentation)
}
func scanner(_ scanner: SBSDKDocumentScanner, didFindPolygon result: SBSDKDocumentDetectionResult?) {
guard let result = result, result.isScanningStatusOK, result.polygon != nil, let image = image else {
alertsManager?.showFailureAlert()
return
}
let processor = SBSDKImageProcessor(uiImage: image)
if let polygon = result.polygon {
processor.applyCrop(polygon: polygon)
}
imageView.image = processor.processedImage
}
}