Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // GraphicCodesReader.swift
- // pmd
- //
- // Created by Tomasz Kiszka on 24/05/2017.
- // Copyright © 2017 EARP. All rights reserved.
- //
- import UIKit
- import AVFoundation
- protocol GraphicCodesReaderDelegate: class {
- func didCaptureCodes(_ codes: [GraphicCodeType]) -> Bool
- func didFailInitializingCaptureSession(error: Error)
- }
- struct GraphicCodeType {
- let code: String
- let type: String
- }
- class GraphicCodesReader: NSObject {
- weak var delegate: GraphicCodesReaderDelegate?
- fileprivate var cameraFrameView: UIView?
- fileprivate(set) var captureSession: AVCaptureSession?
- fileprivate var videoPreviewLayer: AVCaptureVideoPreviewLayer?
- fileprivate var metadataObjectTypes: [String]
- init(metadataObjectTypes: [String]? = nil) {
- if let objectTypes = metadataObjectTypes {
- self.metadataObjectTypes = objectTypes
- }
- else {
- // If none provided, let's make it all possible objects
- self.metadataObjectTypes = [AVMetadataObjectTypeQRCode,
- AVMetadataObjectTypeUPCECode,
- AVMetadataObjectTypeCode39Code,
- AVMetadataObjectTypeCode39Mod43Code,
- AVMetadataObjectTypeEAN13Code,
- AVMetadataObjectTypeEAN8Code,
- AVMetadataObjectTypeCode93Code,
- AVMetadataObjectTypeCode128Code,
- AVMetadataObjectTypePDF417Code,
- AVMetadataObjectTypeAztecCode]
- }
- }
- func prepareCaptureSession(cameraFrameView: UIView) {
- self.cameraFrameView = cameraFrameView
- let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
- do {
- let input = try AVCaptureDeviceInput(device: captureDevice)
- captureSession = AVCaptureSession()
- captureSession?.addInput(input)
- }
- catch {
- delegate?.didFailInitializingCaptureSession(error: error)
- return
- }
- let captureMetadataOutput = AVCaptureMetadataOutput()
- captureSession?.addOutput(captureMetadataOutput)
- captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
- captureMetadataOutput.metadataObjectTypes = metadataObjectTypes
- videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
- videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
- videoPreviewLayer?.frame = cameraFrameView.layer.bounds
- cameraFrameView.layer.addSublayer(videoPreviewLayer!)
- captureSession?.startRunning()
- }
- func resumeCaptureSession() {
- captureSession?.startRunning()
- }
- func pauseCaptureSession() {
- captureSession?.stopRunning()
- }
- }
- extension GraphicCodesReader: AVCaptureMetadataOutputObjectsDelegate {
- func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
- let codes = metadataObjects
- .map { (object) -> GraphicCodeType? in
- guard let readableCodeObject = object as? AVMetadataMachineReadableCodeObject else { return nil }
- return GraphicCodeType(code: readableCodeObject.stringValue, type: readableCodeObject.type)
- }
- .flatMap { $0 }
- let shouldStopRunning = delegate?.didCaptureCodes(codes) ?? false
- if shouldStopRunning {
- captureSession?.stopRunning()
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement