-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEkycIDModule.swift
More file actions
250 lines (215 loc) · 10.3 KB
/
EkycIDModule.swift
File metadata and controls
250 lines (215 loc) · 10.3 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
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
//
// EkycIDModule.swift
// ekyc-id-react-native
//
// Created by Socret Lee on 8/17/22.
//
import AVFoundation
import EkycID
import Foundation
import UIKit
@objc(EkycIDModule)
class EkycIDModule: NSObject, DocumentScannerViewControllerDelegate, LivenessDetectionViewControllerDelegate, EkycIDExpressViewControllerDelegate {
private var promiseResolver: RCTPromiseResolveBlock?
private var promiseRejecter: RCTPromiseRejectBlock?
@objc static func requiresMainQueueSetup() -> Bool {
return true
}
@objc func startDocumentScanner(
_ scannerOptions: NSDictionary?,
overlayOptions: NSDictionary?,
resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping RCTPromiseRejectBlock
) {
self.prepareScanning(resolve, reject)
DispatchQueue.main.async {
let documentScannerView = DocumentScannerViewController()
documentScannerView.delegate = self
documentScannerView.scannerOptions = self.buildDocumentScannerScannerOptions(options: scannerOptions)
documentScannerView.overlayOptions = self.buildDocumentScannerOverlayOptions(options: overlayOptions)
print("important", documentScannerView.overlayOptions)
let rootViewController = UIApplication.shared.keyWindow?.rootViewController
rootViewController?.present(documentScannerView, animated: true)
}
}
@objc func startLivenessDetection(
_ scannerOptions: NSDictionary?,
overlayOptions: NSDictionary?,
resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping RCTPromiseRejectBlock
) {
self.prepareScanning(resolve, reject)
DispatchQueue.main.async {
let livenessDetectionView = LivenessDetectionViewController()
livenessDetectionView.delegate = self
livenessDetectionView.scannerOptions = self.buildLivenessDetectionScannerOptions(options: scannerOptions)
livenessDetectionView.overlayOptions = self.buildLivenessDetectionOverlayOptions(options: overlayOptions)
let rootViewController = UIApplication.shared.keyWindow?.rootViewController
rootViewController?.present(livenessDetectionView, animated: true)
}
}
@objc func startEkycIDExpress(
_ documentScannerScannerOptions: NSDictionary?,
documentScannerOverlayOptions: NSDictionary?,
livenessDetectionScannerOptions: NSDictionary?,
livenessDetectionOverlayOptions: NSDictionary?,
resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping RCTPromiseRejectBlock
) {
self.prepareScanning(resolve, reject)
DispatchQueue.main.async {
let ekycIDExpressView = EkycIDExpressViewController()
ekycIDExpressView.delegate = self
ekycIDExpressView.documentScannerScannerOptions = self.buildDocumentScannerScannerOptions(options: documentScannerScannerOptions)
ekycIDExpressView.documentScannerOverlayOptions = self.buildDocumentScannerOverlayOptions(options: documentScannerOverlayOptions)
ekycIDExpressView.livenessDetectionScannerOptions = self.buildLivenessDetectionScannerOptions(options: livenessDetectionScannerOptions)
ekycIDExpressView.livenessDetectionOverlayOptions = self.buildLivenessDetectionOverlayOptions(options: livenessDetectionOverlayOptions)
let rootViewController = UIApplication.shared.keyWindow?.rootViewController
rootViewController?.present(ekycIDExpressView, animated: true)
}
}
func onDocumentScannerViewControllerResult(mainSide: DocumentScannerResult, secondarySide: DocumentScannerResult?) {
var response: NSArray = []
response = response.adding(mainSide.toDictionary()) as NSArray
if secondarySide != nil {
response = response.adding(secondarySide!.toDictionary()) as NSArray
}
self.promiseResolver!(response)
}
func onLivenessDetectionViewControllerResult(_ result: LivenessDetectionResult) {
var response: NSArray = []
response = response.adding(result.toDictionary()) as NSArray
self.promiseResolver!(response)
}
func onEkycIDExpressViewControllerResult(mainSide: DocumentScannerResult, secondarySide: DocumentScannerResult?, liveness: LivenessDetectionResult) {
var response: NSArray = []
response = response.adding(mainSide.toDictionary()) as NSArray
if secondarySide != nil {
response = response.adding(secondarySide!.toDictionary()) as NSArray
}
response = response.adding(liveness.toDictionary()) as NSArray
self.promiseResolver!(response)
}
private func prepareScanning(_ resolve: @escaping RCTPromiseResolveBlock, _ reject: @escaping RCTPromiseRejectBlock) {
self.promiseResolver = resolve
self.promiseRejecter = reject
}
private func buildDocumentScannerScannerOptions(options: NSDictionary?) -> DocumentScannerOptions {
if options == nil {
return DocumentScannerOptions()
}
var cameraOptions = DocumentScannerCameraOptions()
if options!.value(forKey: "cameraOptions") != nil {
let map = options!.value(forKey: "cameraOptions") as! NSDictionary
cameraOptions = DocumentScannerCameraOptions(
preparingDuration: map.value(forKey: "preparingDuration") != nil ? map.value(forKey: "preparingDuration") as! Int : 3
)
}
var scannableDocuments: [ScannableDocument] = []
if options!.value(forKey: "scannableDocuments") != nil {
let arr: [NSDictionary] = options?.value(forKey: "scannableDocuments") as! [NSDictionary]
scannableDocuments = arr.map {
ScannableDocument(
mainSide: ObjectDetectionObjectType(rawValue: $0.value(forKey: "mainSide") as! String)!,
secondarySide: $0.value(forKey: "secondarySide") != nil ? ObjectDetectionObjectType(rawValue: $0.value(forKey: "secondarySide") as! String) : nil
)
}
}
return DocumentScannerOptions(
cameraOptions: cameraOptions,
scannableDocuments: scannableDocuments
)
}
private func buildDocumentScannerOverlayOptions(options: NSDictionary?) -> DocumentScannerOverlayOptions {
if options == nil {
return DocumentScannerOverlayOptions()
}
return DocumentScannerOverlayOptions(
language: options!.value(forKey: "language") != nil ? EkycIDLanguage(rawValue: options!.value(forKey: "language") as! String)! : .KH
)
}
private func buildLivenessDetectionScannerOptions(options: NSDictionary?) -> LivenessDetectionOptions {
if options == nil {
return LivenessDetectionOptions()
}
print("hello from here")
var cameraOptions = LivenessDetectionCameraOptions()
if options!.value(forKey: "cameraOptions") != nil {
let map = options!.value(forKey: "cameraOptions") as! NSDictionary
cameraOptions = LivenessDetectionCameraOptions(
prompts: map.value(forKey: "prompts") != nil ? (map.value(forKey: "prompts") as! [String]).map {
LivenessPromptType(rawValue: $0)!
} : [.LOOK_LEFT, .LOOK_RIGHT, .BLINKING],
promptTimerCountDownSec: map.value(forKey: "promptTimerCountDownSec") != nil ? map.value(forKey: "promptTimerCountDownSec") as! Int : 5
)
}
return LivenessDetectionOptions(cameraOptions: cameraOptions)
}
private func buildLivenessDetectionOverlayOptions(options: NSDictionary?) -> LivenessDetectionOverlayOptions {
if options == nil {
return LivenessDetectionOverlayOptions()
}
return LivenessDetectionOverlayOptions(
language: options!.value(forKey: "language") != nil ? EkycIDLanguage(rawValue: options!.value(forKey: "language") as! String)! : .KH
)
}
}
extension DocumentScannerResult {
func toDictionary() -> NSDictionary {
return [
"documentType": self.documentType.rawValue,
"documentGroup": self.documentGroup.rawValue,
"fullImage": self.fullImage.saveJPGToTemp("doc_full"),
"documentImage": self.fullImage.saveJPGToTemp("doc_warped"),
"faceImage": self.fullImage.saveJPGToTemp("face_card"),
]
}
}
extension LivenessPrompt {
func toDictionary() -> NSDictionary {
return [
"prompt": self.prompt.rawValue,
"success": self.success,
]
}
}
extension LivenessFace {
func toDictionary(_ filename: String) -> NSDictionary {
return [
"image": self.image!.saveJPGToTemp(filename),
"leftEyeOpenProbability": self.leftEyeOpenProbability,
"rightEyeOpenProbability": self.rightEyeOpenProbability,
"headEulerAngleX": self.headEulerAngleX,
"headEulerAngleY": self.headEulerAngleY,
"headEulerAngleZ": self.headEulerAngleZ,
"headDirection": self.headDirection != nil ? self.headDirection!.rawValue : nil,
"eyesStatus": self.eyesStatus != nil ? self.eyesStatus!.rawValue : nil,
]
}
}
extension LivenessDetectionResult {
func toDictionary() -> NSDictionary {
return [
"prompts": self.prompts.map { $0.toDictionary() },
"frontFace": self.frontFace != nil ? self.frontFace!.toDictionary("face_front") : nil,
"leftFace": self.frontFace != nil ? self.frontFace!.toDictionary("face_left") : nil,
"rightFace": self.frontFace != nil ? self.frontFace!.toDictionary("face_right") : nil,
]
}
}
extension UIImage {
func saveJPGToTemp(_ name: String) -> String {
let url = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
.appendingPathComponent(name, isDirectory: false)
.appendingPathExtension("jpg")
// Then write to disk
if let data = self.jpegData(compressionQuality: 0.8) {
do {
try data.write(to: url)
} catch {
print("Handle the error, i.e. disk can be full")
}
}
return url.absoluteString
}
}