LBXScanViewController.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //
  2. // LBXScanViewController.swift
  3. // swiftScan
  4. //
  5. // Created by lbxia on 15/12/8.
  6. // Copyright © 2015年 xialibing. All rights reserved.
  7. //
  8. import UIKit
  9. import Foundation
  10. import AVFoundation
  11. public protocol LBXScanViewControllerDelegate: class {
  12. func scanFinished(scanResult: LBXScanResult, error: String?)
  13. }
  14. public protocol QRRectDelegate {
  15. func drawwed()
  16. }
  17. open class LBXScanViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
  18. //返回扫码结果,也可以通过继承本控制器,改写该handleCodeResult方法即可
  19. open weak var scanResultDelegate: LBXScanViewControllerDelegate?
  20. open var delegate: QRRectDelegate?
  21. open var scanObj: LBXScanWrapper?
  22. open var scanStyle: LBXScanViewStyle? = LBXScanViewStyle()
  23. open var qRScanView: LBXScanView?
  24. //启动区域识别功能
  25. open var isOpenInterestRect = false
  26. //识别码的类型
  27. public var arrayCodeType:[AVMetadataObject.ObjectType]?
  28. //是否需要识别后的当前图像
  29. public var isNeedCodeImage = false
  30. //相机启动提示文字
  31. public var readyString:String! = "loading"
  32. override open func viewDidLoad() {
  33. super.viewDidLoad()
  34. // Do any additional setup after loading the view.
  35. // [self.view addSubview:_qRScanView];
  36. self.view.backgroundColor = UIColor.black
  37. self.edgesForExtendedLayout = UIRectEdge(rawValue: 0)
  38. drawScanView()
  39. perform(#selector(LBXScanViewController.startScan), with: nil, afterDelay: 0.3)
  40. }
  41. open func setNeedCodeImage(needCodeImg:Bool)
  42. {
  43. isNeedCodeImage = needCodeImg;
  44. }
  45. //设置框内识别
  46. open func setOpenInterestRect(isOpen:Bool){
  47. isOpenInterestRect = isOpen
  48. }
  49. override open func viewWillAppear(_ animated: Bool) {
  50. super.viewWillAppear(animated)
  51. }
  52. override open func viewDidAppear(_ animated: Bool) {
  53. super.viewDidAppear(animated)
  54. }
  55. @objc open func startScan()
  56. {
  57. if (scanObj == nil)
  58. {
  59. var cropRect = CGRect.zero
  60. if isOpenInterestRect
  61. {
  62. cropRect = LBXScanView.getScanRectWithPreView(preView: self.view, style:scanStyle! )
  63. }
  64. //指定识别几种码
  65. if arrayCodeType == nil
  66. {
  67. arrayCodeType = [AVMetadataObject.ObjectType.qr as NSString] as [AVMetadataObject.ObjectType]
  68. }
  69. scanObj = LBXScanWrapper(videoPreView: self.view,objType:arrayCodeType!, isCaptureImg: isNeedCodeImage,cropRect:cropRect, success: { [weak self] (arrayResult) -> Void in
  70. if let strongSelf = self
  71. {
  72. //停止扫描动画
  73. strongSelf.qRScanView?.stopScanAnimation()
  74. strongSelf.handleCodeResult(arrayResult: arrayResult)
  75. }
  76. })
  77. }
  78. //结束相机等待提示
  79. qRScanView?.deviceStopReadying()
  80. //开始扫描动画
  81. qRScanView?.startScanAnimation()
  82. //相机运行
  83. scanObj?.start()
  84. }
  85. open func drawScanView()
  86. {
  87. if qRScanView == nil
  88. {
  89. qRScanView = LBXScanView(frame: self.view.frame,vstyle:scanStyle! )
  90. self.view.addSubview(qRScanView!)
  91. delegate?.drawwed()
  92. }
  93. qRScanView?.deviceStartReadying(readyStr: readyString)
  94. }
  95. /**
  96. 处理扫码结果,如果是继承本控制器的,可以重写该方法,作出相应地处理,或者设置delegate作出相应处理
  97. */
  98. open func handleCodeResult(arrayResult:[LBXScanResult])
  99. {
  100. if let delegate = scanResultDelegate {
  101. self.navigationController? .popViewController(animated: true)
  102. let result:LBXScanResult = arrayResult[0]
  103. delegate.scanFinished(scanResult: result, error: nil)
  104. }else{
  105. for result:LBXScanResult in arrayResult
  106. {
  107. print("%@",result.strScanned ?? "")
  108. }
  109. let result:LBXScanResult = arrayResult[0]
  110. showMsg(title: result.strBarCodeType, message: result.strScanned)
  111. }
  112. }
  113. override open func viewWillDisappear(_ animated: Bool) {
  114. NSObject.cancelPreviousPerformRequests(withTarget: self)
  115. qRScanView?.stopScanAnimation()
  116. scanObj?.stop()
  117. }
  118. open func openPhotoAlbum()
  119. {
  120. LBXPermissions.authorizePhotoWith { [weak self] (granted) in
  121. let picker = UIImagePickerController()
  122. picker.sourceType = UIImagePickerController.SourceType.photoLibrary
  123. picker.delegate = self;
  124. picker.allowsEditing = true
  125. self?.present(picker, animated: true, completion: nil)
  126. }
  127. }
  128. //MARK: -----相册选择图片识别二维码 (条形码没有找到系统方法)
  129. public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
  130. picker.dismiss(animated: true, completion: nil)
  131. var image:UIImage? = info[UIImagePickerController.InfoKey.editedImage] as? UIImage
  132. if (image == nil )
  133. {
  134. image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
  135. }
  136. if(image != nil)
  137. {
  138. let arrayResult = LBXScanWrapper.recognizeQRImage(image: image!)
  139. if arrayResult.count > 0
  140. {
  141. handleCodeResult(arrayResult: arrayResult)
  142. return
  143. }
  144. }
  145. showMsg(title: nil, message: NSLocalizedString("Identify failed", comment: "Identify failed"))
  146. }
  147. func showMsg(title:String?,message:String?)
  148. {
  149. let alertController = UIAlertController(title: nil, message:message, preferredStyle: UIAlertController.Style.alert)
  150. let alertAction = UIAlertAction(title: NSLocalizedString("OK", comment: "OK"), style: UIAlertAction.Style.default) { (alertAction) in
  151. // if let strongSelf = self
  152. // {
  153. // strongSelf.startScan()
  154. // }
  155. }
  156. alertController.addAction(alertAction)
  157. present(alertController, animated: true, completion: nil)
  158. }
  159. deinit
  160. {
  161. // print("LBXScanViewController deinit")
  162. }
  163. }