BarcodeViewController.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // TestViewControllerSwift.swift
  3. // MCPlus
  4. //
  5. // Created by seo ha on 15/02/2019.
  6. // Copyright © 2019 KangSH. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. class BarcodeViewController: UIViewController {
  11. var reader:ZBarReaderViewController!
  12. var callBack:((String)->Void)?
  13. @IBOutlet weak var cameraOverlayView: UIView!
  14. @IBOutlet weak var closeButton: UIImageView!{
  15. didSet{
  16. closeButton.addTapGestureRecognizer(action: self.tapAction)
  17. }
  18. }
  19. }
  20. extension BarcodeViewController{
  21. override func viewDidLoad() {
  22. super.viewDidLoad()
  23. self.scanAction()
  24. // Do any additional setup after loading the view.
  25. }
  26. override func didReceiveMemoryWarning() {
  27. super.didReceiveMemoryWarning()
  28. // Dispose of any resources that can be recreated.
  29. }
  30. func scanAction(){
  31. // ADD: present a barcode reader that scans from the camera feed
  32. reader = ZBarReaderViewController()
  33. reader.readerDelegate = self
  34. reader.supportedOrientationsMask = (1 << UIInterfaceOrientation.portraitUpsideDown.rawValue)
  35. reader.showsZBarControls = false
  36. // let cameraOverlayView = UIView(frame: UIScreen.main.bounds)
  37. // cameraOverlayView.backgroundColor = UIColor.clear
  38. //
  39. // let instructionLabel = UILabel(frame: UIScreen.main.bounds)
  40. // instructionLabel.textColor = UIColor.gray
  41. // instructionLabel.backgroundColor = UIColor.clear
  42. // instructionLabel.font = UIFont(name: "Trebuchet MS", size: 24.0)
  43. // instructionLabel.text = "취소"
  44. //
  45. // cameraOverlayView.addSubview(instructionLabel)
  46. //
  47. // instructionLabel.addTapGestureRecognizer(action: self.tapAction)
  48. let scanner = reader.scanner
  49. // TODO: (optional) additional reader configuration here
  50. // EXAMPLE: disable rarely used I2/5 to improve performance
  51. scanner?.setSymbology(ZBAR_I25, config: ZBAR_CFG_ENABLE, to: 0)
  52. // present and release the controller
  53. // self.present(reader, animated: true, completion: nil)
  54. reader.view.frame = self.cameraOverlayView.bounds
  55. self.cameraOverlayView.addSubview(reader.view)
  56. self.addChild(self.reader)
  57. }
  58. }
  59. extension BarcodeViewController: ZBarReaderDelegate{
  60. func tapAction(){
  61. guard let reader = self.reader else {
  62. return
  63. }
  64. self.navigationController?.popViewController(animated: false)
  65. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak reader] in
  66. reader?.dismiss(animated: true, completion: nil)
  67. }
  68. }
  69. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
  70. // ADD: get the decode results
  71. let key = UIImagePickerController.InfoKey.init(rawValue: ZBarReaderControllerResults)
  72. guard let results = info[key] as? NSFastEnumeration else{ return }
  73. var iterator = NSFastEnumerationIterator(results)
  74. var symbol:ZBarSymbol? = nil
  75. while let _symbol = iterator.next(){
  76. symbol = _symbol as? ZBarSymbol
  77. }
  78. if let data = symbol?.data{
  79. callBack?(data)
  80. }
  81. self.navigationController?.popViewController(animated: false)
  82. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak reader] in
  83. reader?.dismiss(animated: true, completion: nil)
  84. }
  85. }
  86. }