Advertisement
Guest User

QRCode Reader Appcoda

a guest
Sep 12th, 2015
1,136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.96 KB | None | 0 0
  1. //
  2. //  ViewController.swift
  3. //  QRReader
  4. //
  5. //  Created by Burak Akkas on 12/09/15.
  6. //
  7.  
  8. import UIKit
  9. import AVFoundation
  10.  
  11. class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
  12.  
  13.     @IBOutlet weak var messageLabel: UILabel!
  14.    
  15.     var captureSession : AVCaptureSession?
  16.     var videoPreviewLayer : AVCaptureVideoPreviewLayer?
  17.     var qrCodeFrameView : UIView?
  18.    
  19.     override func viewDidLoad() {
  20.         super.viewDidLoad()
  21.         // Do any additional setup after loading the view, typically from a nib.
  22.        
  23.         // Get an instance of the AVCaptureDevice class to initialize a device object and provide the video
  24.         // as the media type parameter.
  25.         let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
  26.        
  27.         // Get an instance of the AVCaptureDeviceInput class using the previous device object.
  28.         //error:NSError?
  29.         var input : AnyObject!
  30.        
  31.         do {
  32.             input = try AVCaptureDeviceInput(device: captureDevice)
  33.         } catch _ {
  34.             print("hata")
  35.         }
  36.        
  37.         // Initialize the captureSession object.
  38.         captureSession = AVCaptureSession()
  39.         // Set the input device on the capture session.
  40.         captureSession?.addInput(input as! AVCaptureInput)
  41.        
  42.         // Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session.
  43.         let captureMetadataOutput = AVCaptureMetadataOutput()
  44.         captureSession?.addOutput(captureMetadataOutput)
  45.        
  46.         // Set delegate and use the default dispatch queue to execute the call back
  47.         captureMetadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
  48.         captureMetadataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode]
  49.        
  50.         // Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
  51.         videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
  52.         videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
  53.         videoPreviewLayer?.frame = view.layer.bounds
  54.         view.layer.addSublayer(videoPreviewLayer!)
  55.        
  56.         captureSession?.startRunning()
  57.         view.bringSubviewToFront(messageLabel)
  58.  
  59.         // Initialize QR Code Frame to highlight the QR code
  60.         qrCodeFrameView = UIView()
  61.         qrCodeFrameView?.layer.borderColor = UIColor.greenColor().CGColor
  62.         qrCodeFrameView?.layer.borderWidth = 2
  63.         view.addSubview(qrCodeFrameView!)
  64.         view.bringSubviewToFront(qrCodeFrameView!)
  65.        
  66.     }
  67.  
  68.     override func didReceiveMemoryWarning() {
  69.         super.didReceiveMemoryWarning()
  70.         // Dispose of any resources that can be recreated.
  71.     }
  72.    
  73.     func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
  74.        
  75.         // Check if the metadataObjects array is not nil and it contains at least one object.
  76.         if metadataObjects == nil || metadataObjects.count == 0 {
  77.             qrCodeFrameView?.frame = CGRectZero
  78.             messageLabel.text = "No QR code is detected"
  79.             return
  80.         }
  81.        
  82.         // Get the metadata object.
  83.         let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
  84.        
  85.         if metadataObj.type == AVMetadataObjectTypeQRCode {
  86.             // If the found metadata is equal to the QR code metadata then update the status label's text and set the bounds
  87.             let barCodeObject = videoPreviewLayer?.transformedMetadataObjectForMetadataObject(metadataObj as AVMetadataMachineReadableCodeObject) as! AVMetadataMachineReadableCodeObject
  88.             qrCodeFrameView?.frame = barCodeObject.bounds;
  89.            
  90.             if metadataObj.stringValue != nil {
  91.                 messageLabel.text = metadataObj.stringValue
  92.             }
  93.         }
  94.     }
  95.  
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement