-
-
Save ShawnPreval97/b3b46a0a6303d80dc70ac6e12ebc76a9 to your computer and use it in GitHub Desktop.
Scanner QR Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // ScanerVC.swift | |
| // DokumenAsli | |
| // | |
| // Created by asharijuang on 3/18/18. | |
| // Copyright © 2018 kodejs. All rights reserved. | |
| // | |
| import UIKit | |
| import AVFoundation | |
| class ScanerVC: UIViewController, AVCaptureMetadataOutputObjectsDelegate { | |
| var captureSession: AVCaptureSession! | |
| var previewLayer: AVCaptureVideoPreviewLayer! | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| view.backgroundColor = UIColor.black | |
| captureSession = AVCaptureSession() | |
| guard let videoCaptureDevice = AVCaptureDevice.default(for: .video) else { return } | |
| let videoInput: AVCaptureDeviceInput | |
| do { | |
| videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice) | |
| } catch { | |
| return | |
| } | |
| if (captureSession.canAddInput(videoInput)) { | |
| captureSession.addInput(videoInput) | |
| } else { | |
| failed() | |
| return | |
| } | |
| let metadataOutput = AVCaptureMetadataOutput() | |
| if (captureSession.canAddOutput(metadataOutput)) { | |
| captureSession.addOutput(metadataOutput) | |
| metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main) | |
| metadataOutput.metadataObjectTypes = [.qr] | |
| } else { | |
| failed() | |
| return | |
| } | |
| previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) | |
| previewLayer.frame = view.layer.bounds | |
| previewLayer.videoGravity = .resizeAspectFill | |
| view.layer.addSublayer(previewLayer) | |
| captureSession.startRunning() | |
| } | |
| func failed() { | |
| let ac = UIAlertController(title: "Scanning not supported", message: "Your device does not support scanning a code from an item. Please use a device with a camera.", preferredStyle: .alert) | |
| ac.addAction(UIAlertAction(title: "OK", style: .default)) | |
| present(ac, animated: true) | |
| captureSession = nil | |
| } | |
| override func viewWillAppear(_ animated: Bool) { | |
| super.viewWillAppear(animated) | |
| if (captureSession?.isRunning == false) { | |
| captureSession.startRunning() | |
| } | |
| } | |
| override func viewWillDisappear(_ animated: Bool) { | |
| super.viewWillDisappear(animated) | |
| if (captureSession?.isRunning == true) { | |
| captureSession.stopRunning() | |
| } | |
| } | |
| func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) { | |
| captureSession.stopRunning() | |
| if let metadataObject = metadataObjects.first { | |
| guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return } | |
| guard let stringValue = readableObject.stringValue else { return } | |
| AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate)) | |
| found(code: stringValue) | |
| } | |
| dismiss(animated: true) | |
| } | |
| func found(code: String) { | |
| print(code) | |
| let target = ResultVC() | |
| target.dataString = code | |
| self.navigationController?.pushViewController(target, animated: true) | |
| } | |
| override var prefersStatusBarHidden: Bool { | |
| return true | |
| } | |
| override var supportedInterfaceOrientations: UIInterfaceOrientationMask { | |
| return .portrait | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment