-
Notifications
You must be signed in to change notification settings - Fork 67
/
SimpleScanViewController.swift
434 lines (359 loc) · 18.6 KB
/
SimpleScanViewController.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import UIKit
/*
This class is all programmatic UI with a small bit of logic to handle
the events that ScanBaseViewController expects subclasses to implement.
Our goal is to have a fully featured Card Scan implementation with a
minimal UI that people can customize fully. You can use this directly or
you can subclass and customize it. If you'd like to use an off-the-shelf
design as well, we suggest using the `ScanViewController`, which uses
mature and well tested UI design patterns.
The default UI looks something like this, with most of the constraints
shown:
------------------------------------
| | | |
|-Cancel Torch-|
| |
| |
| |
| |
| |
|------------Scan Card-------------|
| | |
| ------------------------------ |
| | | |
| | | |
| | | |
| |--4242 4242 4242 4242--| |
| || 05/23 | |
| ||-Sam King | |
| | | | |
| ------------------------------ |
| | | | |
| | | | |
| | Enable camera permissions | |
| | | | |
| | | | |
| |---To scan your card you...---| |
| |
| |
| |
------------------------------------
For the UI we separate out the key components into three parts:
- Five `*String` variables that we use to set the copy
- For each component or group of components we have:
- `setup*Ui` functions for setting the visual look and feel
- `setup*Constraints for setting up autolayout
- We have top level `setupUiComponents` and `setupConstraints` functions that do
a small bit of setup and call the appropriate setup functions for each
components
And to customize the UI you can either override any of these functions or you
can access components directly to adjust. Also, you're welcome to copy and paste
this code and customize it to fit your needs -- we're fine with whatever makes
the most sense for your app.
*/
@available(iOS 11.2, *)
@available(*, deprecated, message: "Replaced by stripe card scan. See https://github.com/stripe/stripe-ios/tree/master/StripeCardScan")
@objc public protocol SimpleScanDelegate {
@objc func userDidCancelSimple(_ scanViewController: SimpleScanViewController)
@objc func userDidScanCardSimple(_ scanViewController: SimpleScanViewController, creditCard: CreditCard)
}
@available(iOS 11.2, *)
@available(*, deprecated, message: "Replaced by stripe card scan. See https://github.com/stripe/stripe-ios/tree/master/StripeCardScan")
@objc open class SimpleScanViewController: ScanBaseViewController {
// used by ScanBase
public var previewView: PreviewView = PreviewView()
public var blurView: BlurView = BlurView()
public var roiView: UIView = UIView()
public var cornerView: CornerView?
// our UI components
public var descriptionText = UILabel()
@objc public var closeButton: UIButton = {
var button = UIButton(type: .system)
button.setTitleColor(.white, for: .normal)
button.tintColor = .white
button.setTitle(SimpleScanViewController.closeButtonString, for: .normal)
return button
}()
@objc public var torchButton: UIButton = {
var button = UIButton(type: .system)
button.setTitleColor(.white, for: .normal)
button.tintColor = .white
button.setTitle(SimpleScanViewController.torchButtonString, for: .normal)
return button
}()
private var debugView: UIImageView?
public var enableCameraPermissionsButton = UIButton(type: .system)
public var enableCameraPermissionsText = UILabel()
// Dynamic card details
public var numberText = UILabel()
public var expiryText = UILabel()
public var nameText = UILabel()
public var expiryLayoutView = UIView()
// String
@objc public static var descriptionString = "Scan Card".localize()
@objc public static var enableCameraPermissionString = "Enable camera access".localize()
@objc public static var enableCameraPermissionsDescriptionString = "To scan your card you'll need to update your phone settings".localize()
@objc public static var closeButtonString = "Close".localize()
@objc public static var torchButtonString = "Torch".localize()
@objc public weak var delegate: SimpleScanDelegate?
@objc public var scanPerformancePriority: ScanPerformance = .fast
@objc public var maxErrorCorrectionDuration: Double = 4.0
@objc public static func createViewController() -> SimpleScanViewController {
let vc = SimpleScanViewController()
if UIDevice.current.userInterfaceIdiom == .pad {
// For the iPad you can use the full screen style but you have to select "requires full screen" in
// the Info.plist to lock it in portrait mode. For iPads, we recommend using a formSheet, which
// handles all orientations correctly.
vc.modalPresentationStyle = .formSheet
} else {
vc.modalPresentationStyle = .fullScreen
}
return vc
}
open override func viewDidLoad() {
super.viewDidLoad()
setupUiComponents()
setupConstraints()
setupOnViewDidLoad(regionOfInterestLabel: roiView, blurView: blurView, previewView: previewView, cornerView: cornerView, debugImageView: debugView, torchLevel: 1.0)
if #available(iOS 13.0, *) {
setUpMainLoop(errorCorrectionDuration: maxErrorCorrectionDuration)
}
startCameraPreview()
}
/* TODO:
Removing targets manually since we are allowing custom buttons which retains button reference ->
ARC doesn't automatically decrement its reference count ->
Targets gets added on every setUpUi call.
Figure out a better way of allow custom buttons programmatically instead of whole UI buttons.
*/
open override func viewDidDisappear(_ animated: Bool) {
closeButton.removeTarget(self, action: #selector(cancelButtonPress), for: .touchUpInside)
torchButton.removeTarget(self, action: #selector(torchButtonPress), for: .touchUpInside)
}
@available(iOS 13.0, *)
func setUpMainLoop(errorCorrectionDuration: Double) {
if scanPerformancePriority == .accurate {
let mainLoop = self.mainLoop as? OcrMainLoop
mainLoop?.errorCorrection = ErrorCorrection(stateMachine: OcrAccurateMainLoopStateMachine(maxErrorCorrection: maxErrorCorrectionDuration))
}
}
// MARK: -Visual and UI event setup for UI components
open func setupUiComponents() {
view.backgroundColor = .white
regionOfInterestCornerRadius = 15.0
let children: [UIView] = [previewView, blurView, roiView, descriptionText, closeButton, torchButton, numberText, expiryText, nameText, expiryLayoutView, enableCameraPermissionsButton, enableCameraPermissionsText]
for child in children {
self.view.addSubview(child)
}
setupPreviewViewUi()
setupBlurViewUi()
setupRoiViewUi()
setupCloseButtonUi()
setupTorchButtonUi()
setupDescriptionTextUi()
setupCardDetailsUi()
setupDenyUi()
if showDebugImageView {
setupDebugViewUi()
}
}
open func setupPreviewViewUi() {
// no ui setup
}
open func setupBlurViewUi() {
blurView.backgroundColor = #colorLiteral(red: 0.2411109507, green: 0.271378696, blue: 0.3280351758, alpha: 0.7020547945)
}
open func setupRoiViewUi() {
roiView.layer.borderColor = UIColor.white.cgColor
}
open func setupCloseButtonUi() {
closeButton.addTarget(self, action: #selector(cancelButtonPress), for: .touchUpInside)
}
open func setupTorchButtonUi() {
torchButton.addTarget(self, action: #selector(torchButtonPress), for: .touchUpInside)
}
open func setupDescriptionTextUi() {
descriptionText.text = SimpleScanViewController.descriptionString
descriptionText.textColor = .white
descriptionText.textAlignment = .center
descriptionText.font = descriptionText.font.withSize(30)
}
open func setupCardDetailsUi() {
numberText.isHidden = true
numberText.textColor = .white
numberText.textAlignment = .center
numberText.font = numberText.font.withSize(48)
numberText.adjustsFontSizeToFitWidth = true
numberText.minimumScaleFactor = 0.2
expiryText.isHidden = true
expiryText.textColor = .white
expiryText.textAlignment = .center
expiryText.font = expiryText.font.withSize(20)
nameText.isHidden = true
nameText.textColor = .white
nameText.font = expiryText.font.withSize(20)
}
open func setupDenyUi() {
let text = SimpleScanViewController.enableCameraPermissionString
let attributedString = NSMutableAttributedString(string: text)
attributedString.addAttribute(NSAttributedString.Key.underlineColor, value: UIColor.white, range: NSRange(location: 0, length: text.count))
attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.white, range: NSRange(location: 0, length: text.count))
attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: text.count))
let font = enableCameraPermissionsButton.titleLabel?.font.withSize(20) ?? UIFont.systemFont(ofSize: 20.0)
attributedString.addAttribute(NSAttributedString.Key.font, value: font, range: NSRange(location: 0, length: text.count))
enableCameraPermissionsButton.setAttributedTitle(attributedString, for: .normal)
enableCameraPermissionsButton.isHidden = true
enableCameraPermissionsButton.addTarget(self, action: #selector(enableCameraPermissionsPress), for: .touchUpInside)
enableCameraPermissionsText.text = SimpleScanViewController.enableCameraPermissionsDescriptionString
enableCameraPermissionsText.textColor = .white
enableCameraPermissionsText.textAlignment = .center
enableCameraPermissionsText.font = enableCameraPermissionsText.font.withSize(17)
enableCameraPermissionsText.numberOfLines = 3
enableCameraPermissionsText.isHidden = true
}
open func setupDebugViewUi() {
debugView = UIImageView()
guard let debugView = debugView else { return }
self.view.addSubview(debugView)
}
// MARK: -Autolayout constraints
open func setupConstraints() {
let children: [UIView] = [previewView, blurView, roiView, descriptionText, closeButton, torchButton, numberText, expiryText, nameText, expiryLayoutView, enableCameraPermissionsButton, enableCameraPermissionsText]
for child in children {
child.translatesAutoresizingMaskIntoConstraints = false
}
setupPreviewViewConstraints()
setupBlurViewConstraints()
setupRoiViewConstraints()
setupCloseButtonConstraints()
setupTorchButtonConstraints()
setupDescriptionTextConstraints()
setupCardDetailsConstraints()
setupDenyConstraints()
if showDebugImageView {
setupDebugViewConstraints()
}
}
open func setupPreviewViewConstraints() {
// make it full screen
previewView.setAnchorsEqual(to: self.view)
}
open func setupBlurViewConstraints() {
blurView.setAnchorsEqual(to: self.previewView)
}
open func setupRoiViewConstraints() {
roiView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16).isActive = true
roiView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16).isActive = true
roiView.heightAnchor.constraint(equalTo: roiView.widthAnchor, multiplier: 1.0 / 1.586).isActive = true
roiView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
open func setupCloseButtonConstraints() {
let margins = view.layoutMarginsGuide
closeButton.topAnchor.constraint(equalTo: margins.topAnchor, constant: 16.0).isActive = true
closeButton.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
}
open func setupTorchButtonConstraints() {
let margins = view.layoutMarginsGuide
torchButton.topAnchor.constraint(equalTo: margins.topAnchor, constant: 16.0).isActive = true
torchButton.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
}
open func setupDescriptionTextConstraints() {
descriptionText.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 32).isActive = true
descriptionText.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -32).isActive = true
descriptionText.bottomAnchor.constraint(equalTo: roiView.topAnchor, constant: -16).isActive = true
}
open func setupCardDetailsConstraints() {
numberText.leadingAnchor.constraint(equalTo: roiView.leadingAnchor, constant: 32).isActive = true
numberText.trailingAnchor.constraint(equalTo: roiView.trailingAnchor, constant: -32).isActive = true
numberText.centerYAnchor.constraint(equalTo: roiView.centerYAnchor).isActive = true
nameText.leadingAnchor.constraint(equalTo: numberText.leadingAnchor).isActive = true
nameText.bottomAnchor.constraint(equalTo: roiView.bottomAnchor, constant: -16).isActive = true
expiryLayoutView.topAnchor.constraint(equalTo: numberText.bottomAnchor).isActive = true
expiryLayoutView.bottomAnchor.constraint(equalTo: nameText.topAnchor).isActive = true
expiryLayoutView.leadingAnchor.constraint(equalTo: numberText.leadingAnchor).isActive = true
expiryLayoutView.trailingAnchor.constraint(equalTo: numberText.trailingAnchor).isActive = true
expiryText.leadingAnchor.constraint(equalTo: expiryLayoutView.leadingAnchor).isActive = true
expiryText.trailingAnchor.constraint(equalTo: expiryLayoutView.trailingAnchor).isActive = true
expiryText.centerYAnchor.constraint(equalTo: expiryLayoutView.centerYAnchor).isActive = true
}
open func setupDenyConstraints() {
enableCameraPermissionsButton.topAnchor.constraint(equalTo: roiView.bottomAnchor, constant: 32).isActive = true
enableCameraPermissionsButton.centerXAnchor.constraint(equalTo: roiView.centerXAnchor).isActive = true
enableCameraPermissionsText.topAnchor.constraint(equalTo: enableCameraPermissionsButton.bottomAnchor, constant: 32).isActive = true
enableCameraPermissionsText.leadingAnchor.constraint(equalTo: roiView.leadingAnchor).isActive = true
enableCameraPermissionsText.trailingAnchor.constraint(equalTo: roiView.trailingAnchor).isActive = true
}
open func setupDebugViewConstraints() {
guard let debugView = debugView else { return }
debugView.translatesAutoresizingMaskIntoConstraints = false
debugView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
debugView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
debugView.widthAnchor.constraint(equalToConstant: 240).isActive = true
debugView.heightAnchor.constraint(equalTo: debugView.widthAnchor, multiplier: 1.0).isActive = true
}
// MARK: -Override some ScanBase functions
override open func onScannedCard(number: String, expiryYear: String?, expiryMonth: String?, scannedImage: UIImage?) {
let card = CreditCard(number: number)
card.expiryMonth = expiryMonth
card.expiryYear = expiryYear
card.name = predictedName
card.image = scannedImage
delegate?.userDidScanCardSimple(self, creditCard: card)
}
open func showScannedCardDetails(prediction: CreditCardOcrPrediction) {
guard let number = prediction.number else {
return
}
numberText.text = CreditCardUtils.format(number: number)
if numberText.isHidden {
numberText.fadeIn()
}
if let expiry = prediction.expiryForDisplay {
expiryText.text = expiry
if expiryText.isHidden {
expiryText.fadeIn()
}
}
if let name = prediction.name {
nameText.text = name
if nameText.isHidden {
nameText.fadeIn()
}
}
}
override open func prediction(prediction: CreditCardOcrPrediction, squareCardImage: CGImage, fullCardImage: CGImage, state: MainLoopState) {
super.prediction(prediction: prediction, squareCardImage: squareCardImage, fullCardImage: fullCardImage, state: state)
showScannedCardDetails(prediction: prediction)
}
override open func onCameraPermissionDenied(showedPrompt: Bool) {
descriptionText.isHidden = true
torchButton.isHidden = true
enableCameraPermissionsButton.isHidden = false
enableCameraPermissionsText.isHidden = false
}
// MARK: -UI event handlers
@objc open func cancelButtonPress() {
delegate?.userDidCancelSimple(self)
self.cancelScan()
}
@objc open func torchButtonPress() {
toggleTorch()
}
/// Warning: if the user navigates to settings and updates the setting, it'll suspend your app.
@objc open func enableCameraPermissionsPress() {
guard let settingsUrl = URL(string: UIApplication.openSettingsURLString), UIApplication.shared.canOpenURL(settingsUrl) else {
print("can't open settings")
return
}
UIApplication.shared.open(settingsUrl)
}
}
@available(*, deprecated, message: "Replaced by stripe card scan. See https://github.com/stripe/stripe-ios/tree/master/StripeCardScan")
public extension UIView {
func setAnchorsEqual(to otherView: UIView) {
self.topAnchor.constraint(equalTo: otherView.topAnchor).isActive = true
self.leadingAnchor.constraint(equalTo: otherView.leadingAnchor).isActive = true
self.trailingAnchor.constraint(equalTo: otherView.trailingAnchor).isActive = true
self.bottomAnchor.constraint(equalTo: otherView.bottomAnchor).isActive = true
}
}