-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathQualityAnalyzerDemoViewController.swift
More file actions
142 lines (124 loc) · 4.99 KB
/
Copy pathQualityAnalyzerDemoViewController.swift
File metadata and controls
142 lines (124 loc) · 4.99 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
//
// QualityAnalyzerDemoViewController.swift
// ClassicComponentsExample
//
// Created by Danil Voitenko on 24.11.21.
// Copyright © 2021 doo GmbH. All rights reserved.
//
import UIKit
import ScanbotSDK
final class QualityAnalyzerDemoViewController: UIViewController {
@IBOutlet private var containerView: UIView!
private var scannerViewController: SBSDKDocumentScannerViewController?
private var analyzer: SBSDKDocumentQualityAnalyzer?
override func viewDidLoad() {
super.viewDidLoad()
scannerViewController = SBSDKDocumentScannerViewController(parentViewController: self,
parentView: containerView,
delegate: self)
analyzer = try? SBSDKDocumentQualityAnalyzer()
scannerViewController?.delegate = self
scannerViewController?.suppressDetectionStatusLabel = true
scannerViewController?.suppressPolygonLayer = true
}
@IBAction private func selectImageButtonDidPress(_ sender: Any) {
darkenScreen()
let picker = UIImagePickerController()
picker.delegate = self
picker.modalPresentationStyle = .fullScreen
present(picker, animated: true, completion: nil)
}
private func estimateAndShowResults(from image: SBSDKImageRef) {
do {
if let result = try analyzer?.run(image: image) {
DispatchQueue.main.async { [weak self] in
self?.show(result: result)
}
}
} catch {
sbsdk_showError(error) { [weak self] _ in
guard let self else { return }
self.sbsdk_forceClose(animated: true, completion: nil)
}
}
}
private func darkenScreen() {
UIView.animate(withDuration: 0.2) { [weak self] in
self?.scannerViewController?.view.backgroundColor = UIColor.black.withAlphaComponent(0.8)
}
}
private func lightenScreen() {
UIView.animate(withDuration: 0.2) { [weak self] in
self?.scannerViewController?.view.backgroundColor = UIColor.clear
}
}
private func show(result: SBSDKDocumentQualityAnalyzerResult) {
let quality: String
switch result.quality {
case .acceptable:
quality = "Acceptable"
case .unacceptable:
quality = "Unacceptable"
case .uncertain:
quality = "Uncertain"
default:
quality = "No document"
}
let resultString = "Quality = \(quality)"
let alert = UIAlertController(title: "Quality Analysis",
message: resultString,
preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK",
style: .default) { _ in
alert.presentedViewController?.dismiss(animated: true)
}
alert.addAction(okAction)
present(alert, animated: true, completion: nil)
}
}
extension QualityAnalyzerDemoViewController: SBSDKDocumentScannerViewControllerDelegate {
func documentScannerViewController(_ controller: SBSDKDocumentScannerViewController,
didSnapDocumentImage documentImage: SBSDKImageRef,
on originalImage: SBSDKImageRef,
with result: SBSDKDocumentDetectionResult?,
autoSnapped: Bool) {
estimateAndShowResults(from: originalImage)
}
func documentScannerViewController(_ controller: SBSDKDocumentScannerViewController, didFailScanning error: any Error) {
sbsdk_showError(error) { [weak self] _ in
guard let self else { return }
self.sbsdk_forceClose(animated: true, completion: nil)
}
}
}
extension QualityAnalyzerDemoViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
dismiss(animated: true) { [weak self] in
let imageRef = SBSDKImageRef.fromUIImage(image: image)
self?.estimateAndShowResults(from: imageRef)
}
} else {
dismiss(animated: true, completion: nil)
}
}
}
extension SBSDKDocumentQuality {
var stringValue: String {
switch self {
case .veryPoor:
return "Very Poor"
case .poor:
return "Poor"
case .reasonable:
return "Reasonable"
case .good:
return "Good"
case .excellent:
return "Excellent"
default:
return ""
}
}
}