Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import AVFoundation
- class ViewController : UIViewController , ,AVCaptureMetadataOutputObjectsDelegate{
- //MARK: - Local Variables
- var captureSession = AVCaptureSession()
- var videoPreviewLayer = AVCaptureVideoPreviewLayer()
- //MARK: - View Life Cycle
- Override func ViewDidLoad(){
- loadQRCapture()
- }
- func loadQRCapture(){
- // Get an instance of the AVCaptureDevice class to initialize a device object and provide the video
- // as the media type parameter.
- let captureDevice = AVCaptureDevice.default(for: AVMediaType.video)
- // Initialize the captureSession object.
- captureSession = AVCaptureSession()
- // Get an instance of the AVCaptureDeviceInput class using the previous device object.
- do{
- let input = try AVCaptureDeviceInput(device: captureDevice!)
- // Set the input device on the capture session.
- captureSession.addInput(input as AVCaptureInput)
- }
- catch{
- if (error != nil) {
- // If any error occurs, simply log the description of it and don't continue any more.
- print("\(error.localizedDescription)")
- return
- }
- }
- // 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: DispatchQueue.main)
- captureMetadataOutput.metadataObjectTypes = [AVMetadataObject.ObjectType.qr]
- // Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
- videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
- videoPreviewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
- videoPreviewLayer.frame = view.layer.bounds
- view.layer.addSublayer(videoPreviewLayer)
- // To show Square Box inside camara for scanning.
- //Add ImageView as squareImgView and insert a scaning box image to imageView
- self.view.bringSubviewToFront(squareImgView)
- // Start video capture.
- captureSession.startRunning()
- }
- }
Add Comment
Please, Sign In to add comment