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
26 changes: 19 additions & 7 deletions Source/LBXScanViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ open class LBXScanViewController: UIViewController {
// 停止扫描动画
strongSelf.qRScanView?.stopScanAnimation()
}
strongSelf.handleCodeResult(arrayResult: arrayResult)
if !arrayResult.isEmpty {
strongSelf.handleCodeResult(reslut: .success(arrayResult))
} else {
strongSelf.handleCodeResult(reslut: .failure(.noResult))
}
})
}

Expand Down Expand Up @@ -126,7 +130,7 @@ open class LBXScanViewController: UIViewController {
/**
处理扫码结果,如果是继承本控制器的,可以重写该方法,作出相应地处理,或者设置delegate作出相应处理
*/
open func handleCodeResult(arrayResult: [LBXScanResult]) {
open func handleCodeResult(reslut: Result<[LBXScanResult], LBXError>) {
guard let delegate = scanResultDelegate else {
fatalError("you must set scanResultDelegate or override this method without super keyword")
}
Expand All @@ -136,11 +140,17 @@ open class LBXScanViewController: UIViewController {

}

if let result = arrayResult.first {
delegate.scanFinished(scanResult: result, error: nil)
} else {
switch reslut {
case .success(let array):
if let result = array.first {
delegate.scanFinished(scanResult: result, error: nil)
} else {
let result = LBXScanResult(str: nil, img: nil, barCodeType: nil, corner: nil)
delegate.scanFinished(scanResult: result, error: "no scan result")
}
case .failure(let error):
let result = LBXScanResult(str: nil, img: nil, barCodeType: nil, corner: nil)
delegate.scanFinished(scanResult: result, error: "no scan result")
delegate.scanFinished(scanResult: result, error: error.rawValue)
}
}

Expand Down Expand Up @@ -176,7 +186,9 @@ extension LBXScanViewController: UIImagePickerControllerDelegate, UINavigationCo
}
let arrayResult = LBXScanWrapper.recognizeQRImage(image: image)
if !arrayResult.isEmpty {
handleCodeResult(arrayResult: arrayResult)
handleCodeResult(reslut: .success(arrayResult))
} else {
handleCodeResult(reslut: .failure(.noResult))
}
}

Expand Down
28 changes: 28 additions & 0 deletions Source/LBXScanViewStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,31 @@ public struct LBXScanViewStyle {
public init() { }

}



public enum LBXError: Error, RawRepresentable {
public typealias RawValue = String

public init?(rawValue: String) {
switch rawValue {
case "nr":
self = .noResult
default:
self = .none
}
}

public var rawValue: RawValue {
switch self {
case .noResult:
return "No scan result"
default:
return "Unknown"
}
}

case noResult
case none

}
29 changes: 18 additions & 11 deletions swiftScan/QQScanViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,26 @@ class QQScanViewController: LBXScanViewController {
drawBottomItems()
}

override func handleCodeResult(arrayResult: [LBXScanResult]) {

for result: LBXScanResult in arrayResult {
if let str = result.strScanned {
print(str)

override func handleCodeResult(reslut: Result<[LBXScanResult], LBXError>) {
switch reslut {
case .success(let array):
for result: LBXScanResult in array {
if let str = result.strScanned {
print(str)
}
}
let vc = ScanResultController()
vc.codeResult = array.first!
navigationController?.pushViewController(vc, animated: true)
default:
let alert = UIAlertController(title: "no scan result", message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: { (_) in
}))
if !(self.navigationController?.visibleViewController?.isKind(of: UIAlertController.self))! {
self.present(alert, animated: true, completion: nil)
}
}

let result: LBXScanResult = arrayResult[0]

let vc = ScanResultController()
vc.codeResult = result
navigationController?.pushViewController(vc, animated: true)
}

func drawBottomItems() {
Expand Down