-
Notifications
You must be signed in to change notification settings - Fork 0
/
TSDocumentScannerImageView.swift
194 lines (153 loc) · 6.26 KB
/
TSDocumentScannerImageView.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
//
// TSDocumentScanner.swift
// Finja
//
// Created by Tallha Sarwar on 25/03/2020.
// Copyright © 2020 Finja Pvt Limited. All rights reserved.
//
import UIKit
import Foundation
import MobileCoreServices
import AVFoundation
import Vision
import VisionKit
protocol TSDocumentChangeProtocol : AnyObject {
func updatedImageStatus (imageView : TSDocumentScannerImageView)
}
@IBDesignable class TSDocumentScannerImageView: UIImageView, VNDocumentCameraViewControllerDelegate {
var parentController: UIViewController?
var imageChanged = false
var statusUpdateDelegate : TSDocumentChangeProtocol?
@IBInspectable var placeholderImage: UIImage? {
didSet {
self.image = placeholderImage
}
}
@IBInspectable var cornerRadius: CGFloat = 10 {
didSet {
refreshCorners(value: cornerRadius)
layer.cornerRadius = cornerRadius
}
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.isUserInteractionEnabled = true
let singleTap = UITapGestureRecognizer(target: self, action: #selector(self.imageViewTapped(sender:)))
self.addGestureRecognizer(singleTap)
self.refreshCorners(value: cornerRadius)
}
override init(frame: CGRect) {
super.init(frame: frame)
self.refreshCorners(value: cornerRadius)
}
func refreshCorners(value: CGFloat) {
layer.cornerRadius = value
}
@objc func imageViewTapped(sender: UITapGestureRecognizer){
checkCameraAccess()
}
func checkCameraAccess() {
switch AVCaptureDevice.authorizationStatus(for: .video) {
case .denied:
print("Denied, request permission from settings")
presentCameraSettings()
case .restricted:
print("Restricted, device owner must approve")
case .authorized:
print("Authorized, proceed")
DispatchQueue.main.async {
self.scanDocument()
}
case .notDetermined:
AVCaptureDevice.requestAccess(for: .video) { success in
if success {
print("Permission granted, proceed")
DispatchQueue.main.async {
self.scanDocument()
}
} else {
print("Permission denied")
}
}
@unknown default:
fatalError()
}
}
func presentCameraSettings() {
self.showAlert(withTitle: "Permission required", message: "Camera access is denied please allow to use this functionality", okButton: {
if let url = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(url, options: [:], completionHandler: { _ in
// Handle
self.reloadInputViews()
})
}
}) {
}
}
func showAlert(withTitle title: String, message : String , okButton success:@escaping () -> () , cancelButton failure:@escaping () -> ()) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default) { action in
success()
}
let cancel = UIAlertAction(title: "Cancel", style: .cancel) { action in
failure()
}
alertController.addAction(OKAction)
alertController.addAction(cancel)
self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
}
func scanDocument() {
if #available(iOS 13.0, *) {
let scannerViewController = VNDocumentCameraViewController()
scannerViewController.delegate = self
self.parentController?.present(scannerViewController, animated: true)
} else {
// Fallback on earlier versions
}
}
// MARK: - Scan Handling
private func processImage(_ image: UIImage) {
self.image = image
self.imageChanged = true
self.statusUpdateDelegate?.updatedImageStatus(imageView: self)
}
// MARK: - VNDocumentCameraViewControllerDelegate
@available(iOS 13.0, *)
func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) {
// Make sure the user scanned at least one page
guard scan.pageCount >= 1 else {
// You are responsible for dismissing the VNDocumentCameraViewController.
controller.dismiss(animated: true)
return
}
// This is a workaround for the VisionKit bug which breaks the `UIImage` returned from `VisionKit`
// See the `Image Loading Hack` section below for more information.
let originalImage = scan.imageOfPage(at: 0)
let fixedImage = reloadedImage(originalImage)
// You are responsible for dismissing the VNDocumentCameraViewController.
controller.dismiss(animated: true)
// Process the image
processImage(fixedImage)
}
@available(iOS 13.0, *)
func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFailWithError error: Error) {
// The VNDocumentCameraViewController failed with an error.
// For now, we'll print it, but you should handle it appropriately in your app.
print(error)
// You are responsible for dismissing the VNDocumentCameraViewController.
controller.dismiss(animated: true)
}
@available(iOS 13.0, *)
func documentCameraViewControllerDidCancel(_ controller: VNDocumentCameraViewController) {
// You are responsible for dismissing the VNDocumentCameraViewController.
controller.dismiss(animated: true)
}
// MARK: - Image Loading Hack
func reloadedImage(_ originalImage: UIImage) -> UIImage {
guard let imageData = originalImage.jpegData(compressionQuality: 1),
let reloadedImage = UIImage(data: imageData) else {
return originalImage
}
return reloadedImage
}
}