Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // QRReader
- //
- // Created by Burak Akkas on 12/09/15.
- //
- import UIKit
- import AVFoundation
- class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
- @IBOutlet weak var messageLabel: UILabel!
- var captureSession : AVCaptureSession?
- var videoPreviewLayer : AVCaptureVideoPreviewLayer?
- var qrCodeFrameView : UIView?
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view, typically from a nib.
- // Get an instance of the AVCaptureDevice class to initialize a device object and provide the video
- // as the media type parameter.
- let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
- // Get an instance of the AVCaptureDeviceInput class using the previous device object.
- //error:NSError?
- var input : AnyObject!
- do {
- input = try AVCaptureDeviceInput(device: captureDevice)
- } catch _ {
- print("hata")
- }
- // Initialize the captureSession object.
- captureSession = AVCaptureSession()
- // Set the input device on the capture session.
- captureSession?.addInput(input as! AVCaptureInput)
- // Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session.
- let captureMetadataOutput = AVCaptureMetadataOutput()
- captureSession?.addOutput(captureMetadataOutput)
- // Set delegate and use the default dispatch queue to execute the call back
- captureMetadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
- captureMetadataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode]
- // Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
- videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
- videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
- videoPreviewLayer?.frame = view.layer.bounds
- view.layer.addSublayer(videoPreviewLayer!)
- captureSession?.startRunning()
- view.bringSubviewToFront(messageLabel)
- // Initialize QR Code Frame to highlight the QR code
- qrCodeFrameView = UIView()
- qrCodeFrameView?.layer.borderColor = UIColor.greenColor().CGColor
- qrCodeFrameView?.layer.borderWidth = 2
- view.addSubview(qrCodeFrameView!)
- view.bringSubviewToFront(qrCodeFrameView!)
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
- // Check if the metadataObjects array is not nil and it contains at least one object.
- if metadataObjects == nil || metadataObjects.count == 0 {
- qrCodeFrameView?.frame = CGRectZero
- messageLabel.text = "No QR code is detected"
- return
- }
- // Get the metadata object.
- let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
- if metadataObj.type == AVMetadataObjectTypeQRCode {
- // If the found metadata is equal to the QR code metadata then update the status label's text and set the bounds
- let barCodeObject = videoPreviewLayer?.transformedMetadataObjectForMetadataObject(metadataObj as AVMetadataMachineReadableCodeObject) as! AVMetadataMachineReadableCodeObject
- qrCodeFrameView?.frame = barCodeObject.bounds;
- if metadataObj.stringValue != nil {
- messageLabel.text = metadataObj.stringValue
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement