MobileScannerUtilities.swift
3.75 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
import AVFoundation
import Foundation
import MLKitBarcodeScanning
extension CVBuffer {
var image: UIImage {
let ciImage = CIImage(cvPixelBuffer: self)
let cgImage = CIContext().createCGImage(ciImage, from: ciImage.extent)
return UIImage(cgImage: cgImage!)
}
}
extension UIDeviceOrientation {
func imageOrientation(position: AVCaptureDevice.Position) -> UIImage.Orientation {
switch self {
case .portrait:
return position == .front ? .leftMirrored : .right
case .landscapeLeft:
return position == .front ? .downMirrored : .up
case .portraitUpsideDown:
return position == .front ? .rightMirrored : .left
case .landscapeRight:
return position == .front ? .upMirrored : .down
default:
return .up
}
}
}
extension Barcode {
var data: [String: Any?] {
let corners = cornerPoints?.map({$0.cgPointValue.data})
return ["corners": corners, "format": format.rawValue, "rawBytes": rawData, "rawValue": rawValue, "type": valueType.rawValue, "calendarEvent": calendarEvent?.data, "contactInfo": contactInfo?.data, "driverLicense": driverLicense?.data, "email": email?.data, "geoPoint": geoPoint?.data, "phone": phone?.data, "sms": sms?.data, "url": url?.data, "wifi": wifi?.data, "displayValue": displayValue]
}
}
extension CGPoint {
var data: [String: Any?] {
let x1 = NSNumber(value: x.native)
let y1 = NSNumber(value: y.native)
return ["x": x1, "y": y1]
}
}
extension BarcodeCalendarEvent {
var data: [String: Any?] {
return ["description": eventDescription, "end": end?.rawValue, "location": location, "organizer": organizer, "start": start?.rawValue, "status": status, "summary": summary]
}
}
extension Date {
var rawValue: String {
return ISO8601DateFormatter().string(from: self)
}
}
extension BarcodeContactInfo {
var data: [String: Any?] {
return ["addresses": addresses?.map({$0.data}), "emails": emails?.map({$0.data}), "name": name?.data, "organization": organization, "phones": phones?.map({$0.data}), "title": jobTitle, "urls": urls]
}
}
extension BarcodeAddress {
var data: [String: Any?] {
return ["addressLines": addressLines, "type": type.rawValue]
}
}
extension BarcodePersonName {
var data: [String: Any?] {
return ["first": first, "formattedName": formattedName, "last": last, "middle": middle, "prefix": prefix, "pronunciation": pronunciation, "suffix": suffix]
}
}
extension BarcodeDriverLicense {
var data: [String: Any?] {
return ["addressCity": addressCity, "addressState": addressState, "addressStreet": addressStreet, "addressZip": addressZip, "birthDate": birthDate, "documentType": documentType, "expiryDate": expiryDate, "firstName": firstName, "gender": gender, "issueDate": issuingDate, "issuingCountry": issuingCountry, "lastName": lastName, "licenseNumber": licenseNumber, "middleName": middleName]
}
}
extension BarcodeEmail {
var data: [String: Any?] {
return ["address": address, "body": body, "subject": subject, "type": type.rawValue]
}
}
extension BarcodeGeoPoint {
var data: [String: Any?] {
return ["latitude": latitude, "longitude": longitude]
}
}
extension BarcodePhone {
var data: [String: Any?] {
return ["number": number, "type": type.rawValue]
}
}
extension BarcodeSMS {
var data: [String: Any?] {
return ["message": message, "phoneNumber": phoneNumber]
}
}
extension BarcodeURLBookmark {
var data: [String: Any?] {
return ["title": title, "url": url]
}
}
extension BarcodeWifi {
var data: [String: Any?] {
return ["encryptionType": type.rawValue, "password": password, "ssid": ssid]
}
}