Guest User

Untitled

a guest
Oct 20th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. import UIKit
  2. import AVFoundation
  3. class ScanViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
  4.  
  5. @IBOutlet var messageLabel:UILabel!
  6. @IBOutlet var topbar: UIView!
  7.  
  8. var captureSession: AVCaptureSession?
  9. var videoPreviewLayer: AVCaptureVideoPreviewLayer?
  10. var qrCodeFrameView: UIView?
  11.  
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14.  
  15. // Get an instance of the AVCaptureDevice class to initialize a device object and provide the video as the media type parameter.
  16. let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
  17.  
  18. do {
  19. // Get an instance of the AVCaptureDeviceInput class using the previous device object.
  20. let input = try AVCaptureDeviceInput(device: captureDevice)
  21.  
  22. // Initialize the captureSession object.
  23. captureSession = AVCaptureSession()
  24.  
  25. // Set the input device on the capture session.
  26. captureSession?.addInput(input)
  27.  
  28. // Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session.
  29. let captureMetadataOutput = AVCaptureMetadataOutput()
  30. captureSession?.addOutput(captureMetadataOutput)
  31.  
  32. // Set delegate and use the default dispatch queue to execute the call back
  33. captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
  34. captureMetadataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode]
  35.  
  36. // Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
  37. videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
  38. videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
  39. videoPreviewLayer?.frame = view.layer.bounds
  40. view.layer.addSublayer(videoPreviewLayer!)
  41.  
  42. // Move the message label and top bar to the front
  43. view.bringSubview(toFront: messageLabel)
  44. view.bringSubview(toFront: topbar)
  45.  
  46. // Start video capture.
  47. captureSession?.startRunning()
  48.  
  49. // Initialize QR Code Frame to highlight the QR code
  50. qrCodeFrameView = UIView()
  51.  
  52. if let qrCodeFrameView = qrCodeFrameView {
  53. qrCodeFrameView.layer.borderColor = UIColor.randomColor().cgColor
  54. qrCodeFrameView.layer.borderWidth = 4
  55. view.addSubview(qrCodeFrameView)
  56. view.bringSubview(toFront: qrCodeFrameView)
  57. }
  58.  
  59. } catch {
  60. // If any error occurs, simply print it out and don't continue any more.
  61. print(error)
  62. return
  63. }
  64. }
  65.  
  66. func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
  67.  
  68. // Check if the metadataObjects array is not nil and it contains at least one object.
  69. if metadataObjects == nil || metadataObjects.count == 0 {
  70. qrCodeFrameView?.frame = CGRect.zero
  71. messageLabel.text = "No QR code is detected"
  72. return
  73. }
  74.  
  75. // Get the metadata object.
  76. let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
  77.  
  78. if metadataObj.type == AVMetadataObjectTypeQRCode {
  79. // If the found metadata is equal to the QR code metadata then update the status label's text and set the bounds
  80. let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
  81. qrCodeFrameView?.frame = barCodeObject!.bounds
  82.  
  83. if metadataObj.stringValue != nil { //Output
  84. messageLabel.text = metadataObj.stringValue
  85.  
  86. performSegue(withIdentifier: "QRFound", sender: self)
  87. }
  88. }
  89. }
  90.  
  91. @IBAction func cancel(_ sender: UIButton) {
  92. if let owningNavController = navigationController {
  93. owningNavController.popViewController(animated: true)
  94. }
  95. dismiss(animated: true, completion: nil)
  96. }
  97. }
Add Comment
Please, Sign In to add comment