|
| 1 | +import UIKit |
| 2 | +import AVFoundation |
| 3 | + |
| 4 | +class QRScannerController: UIViewController, AVCaptureMetadataOutputObjectsDelegate { |
| 5 | + |
| 6 | + // MARK: Properties |
| 7 | + |
| 8 | + @IBOutlet weak var allowCameraButton: UIButton! |
| 9 | + |
| 10 | + var videoPreviewLayer: AVCaptureVideoPreviewLayer? |
| 11 | + var codeIsRead = false |
| 12 | + |
| 13 | + // MARK: View life cycle |
| 14 | + |
| 15 | + override func viewDidLoad() { |
| 16 | + super.viewDidLoad() |
| 17 | + |
| 18 | + allowCameraButton.setTitle("Allow camera access".localized, for: .normal) |
| 19 | + |
| 20 | + if !self.codeIsRead { |
| 21 | + |
| 22 | + let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) |
| 23 | + let input: AVCaptureInput? |
| 24 | + |
| 25 | + do { input = try AVCaptureDeviceInput(device: captureDevice) } |
| 26 | + catch { return } |
| 27 | + |
| 28 | + let captureSession = AVCaptureSession() |
| 29 | + captureSession.addInput(input) |
| 30 | + |
| 31 | + let captureMetadataOutput = AVCaptureMetadataOutput() |
| 32 | + captureSession.addOutput(captureMetadataOutput) |
| 33 | + |
| 34 | + captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main) |
| 35 | + captureMetadataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode] |
| 36 | + |
| 37 | + self.videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession) |
| 38 | + self.videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill |
| 39 | + self.loadPreview() |
| 40 | + |
| 41 | + captureSession.startRunning() |
| 42 | + |
| 43 | + NotificationCenter.default.addObserver(self, selector: #selector(self.loadPreview), |
| 44 | + name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + override func viewWillAppear(_ animated: Bool) { |
| 49 | + loadTheme() |
| 50 | + } |
| 51 | + |
| 52 | + // MARK: AVCaptureMetadataOutputObjectsDelegate |
| 53 | + |
| 54 | + func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) { |
| 55 | + |
| 56 | + if !codeIsRead { |
| 57 | + |
| 58 | + let metadataObj = metadataObjects.first as! AVMetadataMachineReadableCodeObject |
| 59 | + |
| 60 | + if metadataObj.type == AVMetadataObjectTypeQRCode { |
| 61 | + |
| 62 | + // READ JSON format |
| 63 | + |
| 64 | + if let data = metadataObj.stringValue.data(using: .utf8) { |
| 65 | + let content = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [[String: Any]] |
| 66 | + performSegue(withIdentifier: "unwindToQuestions", sender: content) |
| 67 | + codeIsRead = true |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // MARK: UIStoryboardSegue Handling |
| 74 | + |
| 75 | + override func prepare(for segue: UIStoryboardSegue, sender: Any?) { |
| 76 | + |
| 77 | + if let content = sender as? [[String: Any]], segue.identifier == "unwindToQuestions" { |
| 78 | + let controller = segue.destination as! QuestionsViewController |
| 79 | + controller.isSetFromJSON = true |
| 80 | + controller.set = content as NSArray |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @IBAction func unwindToQRScanner(_ segue: UIStoryboardSegue) { } |
| 85 | + |
| 86 | + @IBAction func allowCameraAction() { |
| 87 | + let alertViewController = UIAlertController(title: "Attention".localized, |
| 88 | + message: "Camera access required for QR Scanning".localized, |
| 89 | + preferredStyle: .alert) |
| 90 | + |
| 91 | + alertViewController.addAction(title: "Cancel".localized, style: .cancel, handler: nil) |
| 92 | + alertViewController.addAction(title: "Allow Camera".localized, style: .default) { action in |
| 93 | + if let settingsURL = URL(string: UIApplicationOpenSettingsURLString) { |
| 94 | + UIApplication.shared.openURL(settingsURL) |
| 95 | + } |
| 96 | + } |
| 97 | + self.present(alertViewController, animated: true, completion: nil) |
| 98 | + } |
| 99 | + |
| 100 | + // MARK: Convenience |
| 101 | + |
| 102 | + func loadPreview() { |
| 103 | + |
| 104 | + switch UIDevice.current.orientation { |
| 105 | + case .portrait, .faceUp, .faceDown, .portraitUpsideDown, .unknown: |
| 106 | + videoPreviewLayer?.connection.videoOrientation = .portrait |
| 107 | + case .landscapeRight: |
| 108 | + videoPreviewLayer?.connection.videoOrientation = .landscapeLeft |
| 109 | + case .landscapeLeft: |
| 110 | + videoPreviewLayer?.connection.videoOrientation = .landscapeRight |
| 111 | + } |
| 112 | + videoPreviewLayer?.frame = view.layer.bounds |
| 113 | + |
| 114 | + if let newLayer = videoPreviewLayer { |
| 115 | + view.layer.addSublayer(newLayer) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + func loadTheme() { |
| 120 | + let darkThemeEnabled = Settings.sharedInstance.darkThemeEnabled |
| 121 | + self.navigationController?.navigationBar.barStyle = darkThemeEnabled ? .black : .default |
| 122 | + self.navigationController?.navigationBar.tintColor = darkThemeEnabled ? .orange : .defaultTintColor |
| 123 | + view.backgroundColor = darkThemeEnabled ? .gray : .white |
| 124 | + allowCameraButton.setTitleColor(darkThemeEnabled ? .warmYellow : .coolBlue, for: .normal) |
| 125 | + } |
| 126 | +} |
0 commit comments