Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a protocol for scanning extensibility #56

Merged
merged 2 commits into from
Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CardScan/Classes/Ocr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class Ocr {
public var expiry: Expiry?

public var errorCorrectionDuration = 1.0
var scanEventsDelegate: ScanEvents?

public init() {}

Expand Down Expand Up @@ -91,6 +92,15 @@ public class Ocr {
}

if let number = number {
// Note: the onScanComplete method is called in ScanBaseViewController
let xmin = findFour.predictedBoxes.map { $0.minX }.min() ?? 0.0
let xmax = findFour.predictedBoxes.map { $0.maxX }.max() ?? 0.0
let ymin = findFour.predictedBoxes.map { $0.minY }.min() ?? 0.0
let ymax = findFour.predictedBoxes.map { $0.maxY }.max() ?? 0.0
let numberBoundingBox = CGRect(x: xmin, y: ymin, width: (xmax - xmin), height: (ymax - ymin))
self.scanEventsDelegate?.onNumberRecognized(number: number, expiry:
findFour.expiry, cardImage: rawImage, numberBoundingBox: numberBoundingBox, expiryBoundingBox: findFour.expiryBoxes.first)

self.scanStats.algorithm = findFour.algorithm
self.updateStats(model: findFour.modelString, boxes: findFour.predictedBoxes, image: rawImage, number: number, cvvBoxes: findFour.cvvBoxes)
return number
Expand Down
11 changes: 9 additions & 2 deletions CardScan/Classes/ScanBaseViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import Vision
@objc public var includeCardImage = false
@objc public var showDebugImageView = false

public var scanEventsDelegate: ScanEvents?

static public let machineLearningQueue = DispatchQueue(label: "CardScanMlQueue")
// Only access this variable from the machineLearningQueue
static var hasRegisteredAppNotifications = false
Expand All @@ -41,7 +43,7 @@ import Vision
@objc open func onScannedCard(number: String, expiryYear: String?, expiryMonth: String?, scannedImage: UIImage?) { }
@objc open func showCardNumber(_ number: String, expiry: String?) { }

func toggleTorch() {
public func toggleTorch() {
self.ocr.scanStats.torchOn = !self.ocr.scanStats.torchOn
self.videoFeed.toggleTorch()
}
Expand Down Expand Up @@ -178,6 +180,10 @@ import Vision
self.videoFeed.setup(captureDelegate: self, completion: { success in })

self.ocr.errorCorrectionDuration = self.errorCorrectionDuration

// we split the implementation between OCR, which calls `onNumberRecognized`
// and ScanBaseViewController, which calls `onScanComplete`
self.ocr.scanEventsDelegate = self.scanEventsDelegate
self.previewView?.videoPreviewLayer.session = self.videoFeed.session

self.videoFeed.pauseSession()
Expand Down Expand Up @@ -354,8 +360,9 @@ import Vision
if self.calledOnScannedCard {
return
}

self.calledOnScannedCard = true
// Note: the onNumberRecognized method is called on Ocr
self.scanEventsDelegate?.onScanComplete()

let expiryMonth = expiry.map { String($0.month) }
let expiryYear = expiry.map { String($0.year) }
Expand Down
13 changes: 13 additions & 0 deletions CardScan/Classes/ScanEventsProtocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// ScanEventsProtocol.swift
// CardScan
//
// Created by Sam King on 8/9/19.
//

import Foundation

public protocol ScanEvents {
func onNumberRecognized(number: String, expiry: Expiry?, cardImage: CGImage, numberBoundingBox: CGRect, expiryBoundingBox: CGRect?)
func onScanComplete()
}
14 changes: 12 additions & 2 deletions Example/CardScan/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import UIKit
import CardScan

class ViewController: UIViewController, ScanDelegate, ScanStringsDataSource, TestingImageDataSource {
class ViewController: UIViewController, ScanEvents, ScanDelegate, ScanStringsDataSource, TestingImageDataSource {

let testImages = [UIImage(imageLiteralResourceName: "frame0"),
UIImage(imageLiteralResourceName: "frame19"),
UIImage(imageLiteralResourceName: "frame38"),
Expand Down Expand Up @@ -162,12 +162,22 @@ class ViewController: UIViewController, ScanDelegate, ScanStringsDataSource, Tes
self.present(vc, animated: true)
}

func onNumberRecognized(number: String, expiry: Expiry?, cardImage: CGImage, numberBoundingBox: CGRect, expiryBoundingBox: CGRect?) {
print("number recognized")
}

func onScanComplete() {
print("scan complete")
}

@IBAction func scanWithStatsPress() {
ScanViewController.configure(apiKey: "0xdeadbeef")
guard let vc = ScanViewController.createViewController(withDelegate: self) else {
print("scan view controller not supported on this hardware")
return
}
vc.scanEventsDelegate = self

self.present(vc, animated: true)
}
}
Expand Down
Loading