Guest User

Untitled

a guest
Jan 17th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. import AVFoundation
  2.  
  3. class ViewController : UIViewController , ,AVCaptureMetadataOutputObjectsDelegate{
  4.  
  5. //MARK: - Local Variables
  6. var captureSession = AVCaptureSession()
  7. var videoPreviewLayer = AVCaptureVideoPreviewLayer()
  8.  
  9. //MARK: - View Life Cycle
  10. Override func ViewDidLoad(){
  11. loadQRCapture()
  12. }
  13.  
  14. func loadQRCapture(){
  15. // Get an instance of the AVCaptureDevice class to initialize a device object and provide the video
  16. // as the media type parameter.
  17. let captureDevice = AVCaptureDevice.default(for: AVMediaType.video)
  18.  
  19. // Initialize the captureSession object.
  20. captureSession = AVCaptureSession()
  21.  
  22. // Get an instance of the AVCaptureDeviceInput class using the previous device object.
  23. do{
  24. let input = try AVCaptureDeviceInput(device: captureDevice!)
  25.  
  26. // Set the input device on the capture session.
  27. captureSession.addInput(input as AVCaptureInput)
  28. }
  29. catch{
  30. if (error != nil) {
  31. // If any error occurs, simply log the description of it and don't continue any more.
  32. print("\(error.localizedDescription)")
  33. return
  34. }
  35. }
  36.  
  37. // Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session.
  38. let captureMetadataOutput = AVCaptureMetadataOutput()
  39. captureSession.addOutput(captureMetadataOutput)
  40.  
  41. // Set delegate and use the default dispatch queue to execute the call back
  42. captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
  43. captureMetadataOutput.metadataObjectTypes = [AVMetadataObject.ObjectType.qr]
  44.  
  45. // Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
  46. videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
  47. videoPreviewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
  48. videoPreviewLayer.frame = view.layer.bounds
  49. view.layer.addSublayer(videoPreviewLayer)
  50. // To show Square Box inside camara for scanning.
  51. //Add ImageView as squareImgView and insert a scaning box image to imageView
  52. self.view.bringSubviewToFront(squareImgView)
  53. // Start video capture.
  54. captureSession.startRunning()
  55. }
  56. }
Add Comment
Please, Sign In to add comment