Last active
April 9, 2020 13:09
-
-
Save shinjism/95a432ef535a06a15fd438c241526cf1 to your computer and use it in GitHub Desktop.
QRCode scanner in Swift 4.0
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
| import Foundation | |
| import AVFoundation | |
| import UIKit | |
| class CameraViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate { | |
| // カメラやマイクの入出力を管理するオブジェクトを生成 | |
| private let session = AVCaptureSession() | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| // Do any additional setup after loading the view, typically from a nib. | |
| // カメラやマイクのデバイスそのものを管理するオブジェクトを生成(ここではワイドアングルカメラ・ビデオ・背面カメラを指定) | |
| let discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], | |
| mediaType: .video, | |
| position: .back) | |
| // ワイドアングルカメラ・ビデオ・背面カメラに該当するデバイスを取得 | |
| let devices = discoverySession.devices | |
| // 該当するデバイスのうち最初に取得したものを利用する | |
| if let backCamera = devices.first { | |
| do { | |
| // QRコードの読み取りに背面カメラの映像を利用するための設定 | |
| let deviceInput = try AVCaptureDeviceInput(device: backCamera) | |
| if self.session.canAddInput(deviceInput) { | |
| self.session.addInput(deviceInput) | |
| // 背面カメラの映像からQRコードを検出するための設定 | |
| let metadataOutput = AVCaptureMetadataOutput() | |
| if self.session.canAddOutput(metadataOutput) { | |
| self.session.addOutput(metadataOutput) | |
| metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main) | |
| metadataOutput.metadataObjectTypes = [.qr] | |
| // 背面カメラの映像を画面に表示するためのレイヤーを生成 | |
| let previewLayer = AVCaptureVideoPreviewLayer(session: self.session) | |
| previewLayer.frame = self.view.bounds | |
| previewLayer.videoGravity = .resizeAspectFill | |
| self.view.layer.addSublayer(previewLayer) | |
| // 読み取り開始 | |
| self.session.startRunning() | |
| } | |
| } | |
| } catch { | |
| print("Error occured while creating video device input: \(error)") | |
| } | |
| } | |
| } | |
| override func didReceiveMemoryWarning() { | |
| super.didReceiveMemoryWarning() | |
| // Dispose of any resources that can be recreated. | |
| } | |
| func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) { | |
| for metadata in metadataObjects as! [AVMetadataMachineReadableCodeObject] { | |
| // QRコードのデータかどうかの確認 | |
| if metadata.type != .qr { continue } | |
| // QRコードの内容が空かどうかの確認 | |
| if metadata.stringValue == nil { continue } | |
| /* | |
| このあたりで取得したQRコードを使ってゴニョゴニョする | |
| 読み取りの終了・再開のタイミングは用途によって制御が異なるので注意 | |
| 以下はQRコードに紐づくWebサイトをSafariで開く例 | |
| */ | |
| // URLかどうかの確認 | |
| if let url = URL(string: metadata.stringValue!) { | |
| // 読み取り終了 | |
| self.session.stopRunning() | |
| // QRコードに紐付いたURLをSafariで開く | |
| UIApplication.shared.open(url, options: [:], completionHandler: nil) | |
| break | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment