-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocumentScannerView.swift
193 lines (162 loc) · 7.24 KB
/
DocumentScannerView.swift
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
//
// DocumentScannerView.swift
// HackUNT
//
// Created by Hussain Alkatheri on 11/11/23.
//
import SwiftUI
import VisionKit
@MainActor
struct DocumentScannerView: UIViewControllerRepresentable {
static let startScanLabel = "Start Scan"
static let stopScanLabel = "Stop Scan"
static let textDataType: DataScannerViewController.RecognizedDataType = .text(
languages: [
"en-US"
]
)
var scannerViewController: DataScannerViewController = DataScannerViewController(
recognizedDataTypes: [DocumentScannerView.textDataType],
qualityLevel: .accurate,
recognizesMultipleItems: false,
isHighFrameRateTrackingEnabled: false,
isHighlightingEnabled: true
)
func makeUIViewController(context: Context) -> DataScannerViewController {
scannerViewController.delegate = context.coordinator
// Add a button to start scanning
let scanButton = UIButton(type: .roundedRect)
scanButton.backgroundColor = UIColor.systemBlue
scanButton.setTitle(DocumentScannerView.startScanLabel, for: .normal)
scanButton.setTitleColor(UIColor.white, for: .normal)
var config = UIButton.Configuration.filled()
config.contentInsets = NSDirectionalEdgeInsets(top: 15, leading: 15, bottom: 15, trailing: 15)
scanButton.configuration = config
scanButton.addTarget(context.coordinator, action: #selector(Coordinator.startScanning(_:)), for: .touchUpInside)
scanButton.layer.cornerRadius = 5.0
scannerViewController.view.addSubview(scanButton)
// Set up button constraints
scanButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
scanButton.centerXAnchor.constraint(equalTo: scannerViewController.view.centerXAnchor),
scanButton.bottomAnchor.constraint(equalTo: scannerViewController.view.safeAreaLayoutGuide.bottomAnchor, constant: -20)
])
return scannerViewController
}
func updateUIViewController(_ uiViewController: DataScannerViewController, context: Context) {
// Update any view controller settings here
}
func makeCoordinator() -> Coordinator {
return Coordinator(self)
}
class Coordinator: NSObject, DataScannerViewControllerDelegate {
var parent: DocumentScannerView
var roundBoxMappings: [UUID: UIView] = [:]
init(_ parent: DocumentScannerView) {
self.parent = parent
}
func dataScanner(_ dataScanner: DataScannerViewController, didAdd addedItems: [RecognizedItem], allItems: [RecognizedItem]) {
processAddedItems(items: addedItems)
}
func dataScanner(_ dataScanner: DataScannerViewController, didRemove removedItems: [RecognizedItem], allItems: [RecognizedItem]) {
processRemovedItems(items: removedItems)
}
func dataScanner(_ dataScanner: DataScannerViewController, didUpdate updatedItems: [RecognizedItem], allItems: [RecognizedItem]) {
processUpdatedItems(items: updatedItems)
}
func dataScanner(_ dataScanner: DataScannerViewController, didTapOn item: RecognizedItem) {
processItem(item: item)
}
func processAddedItems(items: [RecognizedItem]) {
for item in items {
processItem(item: item)
}
}
func processRemovedItems(items: [RecognizedItem]) {
for item in items {
removeRoundBoxFromItem(item: item)
}
}
func processUpdatedItems(items: [RecognizedItem]) {
for item in items {
updateRoundBoxToItem(item: item)
}
}
func processItem(item: RecognizedItem) {
switch item {
case .text(let text):
print("Text Observation - \(text.observation)")
print("Text transcript - \(text.transcript)")
if (text.transcript.contains("Christopher Conly")){
Task(priority: .high) {
_ = await placeOrder(ID: 1, Category: "led1")
}
}
else if(text.transcript.contains("Ameer Hantouli")){
Task(priority: .high) {
_ = await placeOrder(ID: 1, Category: "led2")
}
}
else if(text.transcript.contains("Jason Losh")){
Task(priority: .high) {
_ = await placeOrder(ID: 1, Category: "led3")
}
}
else if(text.transcript.contains("Chris Mcmurrough")){
Task(priority: .high) {
_ = await placeOrder(ID: 1, Category: "led4")
}
}
// let frame = getRoundBoxFrame(item: item)
// Adding the round box overlay to detected text
// addRoundBoxToItem(frame: frame, text: text.transcript, item: item)
case .barcode:
break
@unknown default:
print("Should not happen")
}
}
func addRoundBoxToItem(frame: CGRect, text: String, item: RecognizedItem) {
//let roundedRectView = RoundRectView(frame: frame)
let roundedRectView = RoundedRectLabel(frame: frame)
roundedRectView.setText(text: text)
parent.scannerViewController.overlayContainerView.addSubview(roundedRectView)
roundBoxMappings[item.id] = roundedRectView
}
func removeRoundBoxFromItem(item: RecognizedItem) {
if let roundBoxView = roundBoxMappings[item.id] {
if roundBoxView.superview != nil {
roundBoxView.removeFromSuperview()
roundBoxMappings.removeValue(forKey: item.id)
}
}
}
func updateRoundBoxToItem(item: RecognizedItem) {
if let roundBoxView = roundBoxMappings[item.id] {
if roundBoxView.superview != nil {
let frame = getRoundBoxFrame(item: item)
roundBoxView.frame = frame
}
}
}
func getRoundBoxFrame(item: RecognizedItem) -> CGRect {
let frame = CGRect(
x: item.bounds.topLeft.x,
y: item.bounds.topLeft.y,
width: abs(item.bounds.topRight.x - item.bounds.topLeft.x) + 15,
height: abs(item.bounds.topLeft.y - item.bounds.bottomLeft.y) + 15
)
return frame
}
// Add this method to start scanning
@objc func startScanning(_ sender: UIButton) {
if sender.title(for: .normal) == startScanLabel {
try? parent.scannerViewController.startScanning()
sender.setTitle(stopScanLabel, for: .normal)
} else {
parent.scannerViewController.stopScanning()
sender.setTitle(startScanLabel, for: .normal)
}
}
}
}