Skip to content
Open
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
8 changes: 0 additions & 8 deletions QRCode/QRCodeTests/QRCodeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,4 @@ class QRCodeTests: XCTestCase {
// This is an example of a functional test case.
XCTAssert(true, "Pass")
}

func testPerformanceExample() {
// This is an example of a performance test case.
self.measure() {
// Put the code you want to measure the time of here.
}
}

}
32 changes: 20 additions & 12 deletions QRCode/Source/QRCode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@
import UIKit
import AVFoundation


open class QRCode: NSObject, AVCaptureMetadataOutputObjectsDelegate {

@objc public enum Error : Int, Swift.Error{
public typealias RawValue = Int

case sessionAlreadyRunning
case sessionAlreadyStopped
case cannotOpenCamera
case cannotSaveData
}

/// corner line width
var lineWidth: CGFloat
/// corner stroke color
Expand Down Expand Up @@ -135,33 +145,31 @@ open class QRCode: NSObject, AVCaptureMetadataOutputObjectsDelegate {
///
/// - parameter view: the scan view, the preview layer and the drawing layer will be insert into this view
/// - parameter completion: the completion call back
open func prepareScan(_ view: UIView, completion:@escaping (_ stringValue: String)->()) {
open func prepareScan(_ view: UIView, completion:@escaping (_ stringValue: String)->()) throws {

scanFrame = view.bounds

completedCallBack = completion
currentDetectedCount = 0

setupSession()
try setupSession()
setupLayers(view)
}

/// start scan
open func startScan() {
open func startScan() throws {
if session.isRunning {
print("the capture session is running")

return
throw QRCode.Error.sessionAlreadyRunning
}
session.startRunning()
}

/// stop scan
open func stopScan() {
open func stopScan() throws {
if !session.isRunning {
print("the capture session is not running")

return
throw QRCode.Error.sessionAlreadyStopped
}
session.stopRunning()
}
Expand All @@ -173,20 +181,20 @@ open class QRCode: NSObject, AVCaptureMetadataOutputObjectsDelegate {
view.layer.insertSublayer(previewLayer, at: 0)
}

func setupSession() {
func setupSession() throws{
if session.isRunning {
print("the capture session is running")
return
throw QRCode.Error.sessionAlreadyRunning
}

if !session.canAddInput(videoInput) {
print("can not add input device")
return
throw QRCode.Error.cannotOpenCamera
}

if !session.canAddOutput(dataOutput) {
print("can not add output device")
return
throw QRCode.Error.cannotSaveData
}

session.addInput(videoInput)
Expand Down
14 changes: 11 additions & 3 deletions QRCodeDemo/QRCodeDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

scanner.prepareScan(view) { (stringValue) -> () in
print(stringValue)
do{
try scanner.prepareScan(view) { (stringValue) -> () in
print(stringValue)
}
}catch(_){
print("Error during preparing scan")
}
// test scan frame
scanner.scanFrame = view.bounds
Expand All @@ -29,7 +33,11 @@ class ViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

scanner.startScan()
do{
try scanner.startScan()
}catch(_){
print("Error during starting scan")
}
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
Expand Down
12 changes: 10 additions & 2 deletions QRcodeDemoObjc/QRcodeDemoObjc/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,25 @@ - (void)viewDidLoad {
[super viewDidLoad];

// alloc and prepare for scan
NSError *error;
self.scanner = [[QRCode alloc] init];
[self.scanner prepareScan:self.view completion:^(NSString * __nonnull stringValue) {
[self.scanner prepareScan:self.view error:&error completion:^(NSString * _Nonnull stringValue) {
NSLog(@"%@", stringValue);
}];
if (error) {
NSLog(@"Error during preparing scan");
}
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

// start scan
[self.scanner startScan];
NSError *error;
[self.scanner startScanAndReturnError:&error];
if (error) {
NSLog(@"Error during starting scan");
}
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
Expand Down