Getting started
Installing the SDK
In your Podfile
top part add the sources
source 'https://gitlab.com/telosid/plugins/specs.git'
source 'https://cdn.cocoapods.org'
pod 'YesidOCRCamera'
pod 'YesidFaceEnrollment', '0.2.3'
pod 'YesidFaceAuthentication'
pod "OnyxCamera"
Run pod install --repo-update
OCR CAMERA
- Create a new swift file
CardModule
copy the following contents in it
import Foundation
import AVFoundation
import YesidOCRFramework
import SwiftUI
struct CardScannerView: View {
@Environment(\.presentationMode) var presentationMode
@State private var cameraLensFacing: AVCaptureDevice.Position = .back
@State private var configuration: OCRConfigurationBuilder = OCRConfigurationBuilder().setUserLicense(userLicense: "YOUR LICENSE")
@State var cardTextResults: [String:String] = [:]
@State var cardImageResults:[String:String] = [:]
var onScanComplete: ((String) -> Void)? // Callback to handle scan results
var body: some View {
OCRCameraUI(configurationBuilder: configuration, cameraLensFacing: self.$cameraLensFacing, onResults: { response in
self.cardTextResults = response.documentText ?? [:]
self.cardImageResults = response.documentImages ?? [:]
// Construct a JSON object manually
let jsonObject: [String: Any] = [
"documentImage": self.cardImageResults["Portrait"],
"documentCardImage": self.cardImageResults["Document front side"],
"documentAllTextData": self.cardTextResults
]
let jsonString: String
do {
let jsonData = try JSONSerialization.data(withJSONObject: jsonObject)
jsonString = String(data: jsonData, encoding: .utf8) ?? ""
} catch {
print("Error converting JSON object to JSON string: \(error.localizedDescription)")
jsonString = error.localizedDescription
}
onScanComplete?(jsonString)
presentationMode.wrappedValue.dismiss()
})
}
}
@objc(CardModule)
class CardModule: RCTViewManager {
@objc
func callCardDetection(_ callback: @escaping RCTResponseSenderBlock) {
DispatchQueue.main.async {
let cardScannerView = CardScannerView(onScanComplete: { result in
let resultArray = [result]
callback(resultArray)
})
let viewController = UIApplication.shared.keyWindow?.rootViewController
viewController?.present(UIHostingController(rootView: cardScannerView), animated: true, completion: nil)
}
}
@objc
override static func requiresMainQueueSetup() -> Bool {
return true
}
}
- Create a `CardModule.m`` file
#import "React/RCTBridgeModule.h"
#import "React/RCTViewManager.h"
@interface RCT_EXTERN_MODULE(CardModule,RCTViewManager);
RCT_EXTERN_METHOD(callCardDetection:(RCTResponseSenderBlock)callback);
@end
FACE ENROLLMENT CAMERA
- Create a new swift file
FaceEnrollmentModule
copy the following contents in it
import Foundation
import AVFoundation
import FaceEnrollmentFramework
import SwiftUI
struct FaceEnrollmentView: View {
@Environment(\.presentationMode) var presentationMode
@State private var configuration = FaceEnrollmentConfigurationBuilder()
.setUserLicense(userLicense: "YOUR LICENSE")
var onScanComplete: ((String) -> Void)?
var body: some View {
FaceEnrollmentCameraUI(
configurationBuilder: self.configuration,
onResults: {FaceEnrollmentResults in
if(FaceEnrollmentResults.enrolledFaces != nil){
DispatchQueue.main.async {
let faces: [String] = FaceEnrollmentResults.enrolledFaces?.values.map { convertImageToBase64String(img: $0) } ?? [""]
let jsonObject: [String: Any] = [
"faceImages": faces,
]
let jsonString: String
do {
let jsonData = try JSONSerialization.data(withJSONObject: jsonObject)
jsonString = String(data: jsonData, encoding: .utf8) ?? ""
} catch {
print("Error converting JSON object to JSON string: \(error.localizedDescription)")
jsonString = error.localizedDescription
}
onScanComplete?(jsonString)
presentationMode.wrappedValue.dismiss()
}
}
}
)
}
public func convertImageToBase64String(img: UIImage) -> String {
return img.jpegData(compressionQuality: 0.2)?.base64EncodedString() ?? ""
}
}
@objc(FaceEnrollmentModule)
class FaceEnrollmentModule: RCTViewManager {
@objc
func callFaceEnrollment(_ callback: @escaping RCTResponseSenderBlock) {
DispatchQueue.main.async {
let cardScannerView = FaceEnrollmentView(onScanComplete: { result in
let resultArray = [result]
callback(resultArray)
})
let viewController = UIApplication.shared.keyWindow?.rootViewController
viewController?.present(UIHostingController(rootView: cardScannerView), animated: true, completion: nil)
}
}
@objc
override static func requiresMainQueueSetup() -> Bool {
return true
}
}
- Create a `FaceEnrollmentModule.m`` file
#import "React/RCTBridgeModule.h"
#import "React/RCTViewManager.h"
@interface RCT_EXTERN_MODULE(FaceEnrollmentModule,RCTViewManager);
RCT_EXTERN_METHOD(callFaceEnrollment:(RCTResponseSenderBlock)callback);
@end
FACE ENROLLMENT CAMERA
- Create a new swift file
FaceAuthModule
copy the following contents in it
import Foundation
import AVFoundation
import FaceAuthFramework
import SwiftUI
struct FaceAuthView: View {
@Environment(\.presentationMode) var presentationMode
@State private var configuration = FaceAuthConfigurationBuilder()
.setUserLicense(userLicense: "YOUR LICENSE")
@State var authenticatedFace: UIImage? = UIImage()
@State private var matchResults: [String: Double] = [:]
@State private var count: Int = 0
var faceOneImageData: String
var faceTwoImageData: String
var onScanComplete: ((String) -> Void)?
var body: some View {
FaceAuthCameraUI(
configurationBuilder: configuration,
firstImage: convertBase64StringToImage(imageBase64String: faceOneImageData),
secondImage: convertBase64StringToImage(imageBase64String: faceOneImageData),
onMatchResultsFirstImage: {
FaceMatchResults in
let similarity = FaceMatchResults.similarity ?? 0.0
count += 1
matchResults["similarity1"] = similarity
}, onMatchResultsSecondImage: {
FaceMatchResults in
let similarity = FaceMatchResults.similarity ?? 0.0
count += 1
matchResults["similarity2"] = similarity
},
authenticatedFace: self.$authenticatedFace
)
.onChange(of: count, perform: { _ in
if(count == 2){
if let similarity1 = matchResults["similarity1"], let similarity2 = matchResults["similarity2"] {
let jsonObject: [String: Any] = [
"similarity1": similarity1,
"similarity2": similarity2,
]
do {
let jsonData = try JSONSerialization.data(withJSONObject: jsonObject)
let jsonString = String(data: jsonData, encoding: .utf8) ?? ""
// Trigger the callback with the JSON string
print(jsonString);
onScanComplete?(jsonString)
presentationMode.wrappedValue.dismiss()
} catch {
print("Error converting JSON object to JSON string: \(error.localizedDescription)")
}
}
}
})
}
public func convertImageToBase64String(img: UIImage) -> String {
return img.jpegData(compressionQuality: 0.2)?.base64EncodedString() ?? ""
}
public func convertBase64StringToImage (imageBase64String:String) -> UIImage {
let imageData = Data(base64Encoded: imageBase64String)
let image = UIImage(data: imageData!)
return image ?? UIImage()
}
}
@objc(FaceAuthModule)
class FaceAuthModule: RCTViewManager {
@objc
func callFaceAuth(_ faceOneImageData: String, faceTwoImageData: String, callback: @escaping RCTResponseSenderBlock) {
DispatchQueue.main.async {
let cardScannerView = FaceAuthView(
faceOneImageData: faceOneImageData,
faceTwoImageData: faceTwoImageData,
onScanComplete: { result in
let resultArray = [result]
callback(resultArray)
}
)
let viewController = UIApplication.shared.keyWindow?.rootViewController
viewController?.present(UIHostingController(rootView: cardScannerView), animated: true, completion: nil)
}
}
@objc
override static func requiresMainQueueSetup() -> Bool {
return true
}
}
- Create a
FaceAuthModule.m
file
#import "React/RCTBridgeModule.h"
#import "React/RCTViewManager.h"
@interface RCT_EXTERN_MODULE(FaceAuthModule, RCTViewManager)
RCT_EXTERN_METHOD(callFaceAuth:(NSString *)faceOneImageData faceTwoImageData:(NSString *) faceTwoImageData callback:(RCTResponseSenderBlock)callback)
@end