Advertisement
Guest User

Untitled

a guest
Jul 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.77 KB | None | 0 0
  1. //
  2. //  GraphicCodesReader.swift
  3. //  pmd
  4. //
  5. //  Created by Tomasz Kiszka on 24/05/2017.
  6. //  Copyright © 2017 EARP. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import AVFoundation
  11.  
  12. protocol GraphicCodesReaderDelegate: class {
  13.    
  14.     func didCaptureCodes(_ codes: [GraphicCodeType]) -> Bool
  15.     func didFailInitializingCaptureSession(error: Error)
  16. }
  17.  
  18. struct GraphicCodeType {
  19.    
  20.     let code: String
  21.     let type: String
  22. }
  23.  
  24. class GraphicCodesReader: NSObject {
  25.    
  26.     weak var delegate: GraphicCodesReaderDelegate?
  27.    
  28.     fileprivate var cameraFrameView: UIView?
  29.     fileprivate(set) var captureSession: AVCaptureSession?
  30.     fileprivate var videoPreviewLayer: AVCaptureVideoPreviewLayer?
  31.     fileprivate var metadataObjectTypes: [String]
  32.    
  33.     init(metadataObjectTypes: [String]? = nil) {
  34.         if let objectTypes = metadataObjectTypes {
  35.             self.metadataObjectTypes = objectTypes
  36.         }
  37.         else {
  38.             // If none provided, let's make it all possible objects
  39.             self.metadataObjectTypes = [AVMetadataObjectTypeQRCode,
  40.                                         AVMetadataObjectTypeUPCECode,
  41.                                         AVMetadataObjectTypeCode39Code,
  42.                                         AVMetadataObjectTypeCode39Mod43Code,
  43.                                         AVMetadataObjectTypeEAN13Code,
  44.                                         AVMetadataObjectTypeEAN8Code,
  45.                                         AVMetadataObjectTypeCode93Code,
  46.                                         AVMetadataObjectTypeCode128Code,
  47.                                         AVMetadataObjectTypePDF417Code,
  48.                                         AVMetadataObjectTypeAztecCode]
  49.         }
  50.     }
  51.    
  52.     func prepareCaptureSession(cameraFrameView: UIView) {
  53.         self.cameraFrameView = cameraFrameView
  54.         let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
  55.         do {
  56.             let input = try AVCaptureDeviceInput(device: captureDevice)
  57.             captureSession = AVCaptureSession()
  58.             captureSession?.addInput(input)
  59.         }
  60.         catch {
  61.             delegate?.didFailInitializingCaptureSession(error: error)
  62.             return
  63.         }
  64.         let captureMetadataOutput = AVCaptureMetadataOutput()
  65.         captureSession?.addOutput(captureMetadataOutput)
  66.         captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
  67.         captureMetadataOutput.metadataObjectTypes = metadataObjectTypes
  68.        
  69.         videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
  70.         videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
  71.         videoPreviewLayer?.frame = cameraFrameView.layer.bounds
  72.         cameraFrameView.layer.addSublayer(videoPreviewLayer!)
  73.        
  74.         captureSession?.startRunning()
  75.     }
  76.    
  77.     func resumeCaptureSession() {
  78.         captureSession?.startRunning()
  79.     }
  80.    
  81.     func pauseCaptureSession() {
  82.         captureSession?.stopRunning()
  83.     }
  84. }
  85.  
  86. extension GraphicCodesReader: AVCaptureMetadataOutputObjectsDelegate {
  87.    
  88.     func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
  89.         let codes = metadataObjects
  90.             .map { (object) -> GraphicCodeType? in
  91.                 guard let readableCodeObject = object as? AVMetadataMachineReadableCodeObject else { return nil }
  92.                 return GraphicCodeType(code: readableCodeObject.stringValue, type: readableCodeObject.type)
  93.             }
  94.             .flatMap { $0 }
  95.         let shouldStopRunning = delegate?.didCaptureCodes(codes) ?? false
  96.         if  shouldStopRunning {
  97.             captureSession?.stopRunning()
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement