BarcodeViewController.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. }
  14. extension BarcodeViewController{
  15. override func viewDidLoad() {
  16. super.viewDidLoad()
  17. self.scanAction()
  18. // Do any additional setup after loading the view.
  19. }
  20. override func didReceiveMemoryWarning() {
  21. super.didReceiveMemoryWarning()
  22. // Dispose of any resources that can be recreated.
  23. }
  24. func scanAction(){
  25. // ADD: present a barcode reader that scans from the camera feed
  26. reader = ZBarReaderViewController()
  27. reader.readerDelegate = self
  28. reader.supportedOrientationsMask = (1 << UIInterfaceOrientation.portraitUpsideDown.rawValue)
  29. reader.showsZBarControls = false
  30. let cameraOverlayView = UIView(frame: UIScreen.main.bounds)
  31. cameraOverlayView.backgroundColor = UIColor.clear
  32. let instructionLabel = UILabel(frame: UIScreen.main.bounds)
  33. instructionLabel.textColor = UIColor.gray
  34. instructionLabel.backgroundColor = UIColor.clear
  35. instructionLabel.font = UIFont(name: "Trebuchet MS", size: 24.0)
  36. instructionLabel.text = "취소"
  37. cameraOverlayView.addSubview(instructionLabel)
  38. instructionLabel.addTapGestureRecognizer(action: self.tapAction)
  39. reader.cameraOverlayView = cameraOverlayView
  40. let scanner = reader.scanner
  41. // TODO: (optional) additional reader configuration here
  42. // EXAMPLE: disable rarely used I2/5 to improve performance
  43. scanner?.setSymbology(ZBAR_I25, config: ZBAR_CFG_ENABLE, to: 0)
  44. // present and release the controller
  45. self.present(reader, animated: true, completion: nil)
  46. }
  47. }
  48. extension BarcodeViewController: ZBarReaderDelegate{
  49. func tapAction(){
  50. guard let reader = self.reader else {
  51. return
  52. }
  53. self.navigationController?.popViewController(animated: false)
  54. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak reader] in
  55. reader?.dismiss(animated: true, completion: nil)
  56. }
  57. }
  58. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
  59. // ADD: get the decode results
  60. let key = UIImagePickerController.InfoKey.init(rawValue: ZBarReaderControllerResults)
  61. guard let results = info[key] as? NSFastEnumeration else{ return }
  62. var iterator = NSFastEnumerationIterator(results)
  63. var symbol:ZBarSymbol? = nil
  64. while let _symbol = iterator.next(){
  65. symbol = _symbol as? ZBarSymbol
  66. }
  67. if let data = symbol?.data{
  68. callBack?(data)
  69. }
  70. self.navigationController?.popViewController(animated: false)
  71. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak reader] in
  72. reader?.dismiss(animated: true, completion: nil)
  73. }
  74. }
  75. }