前提・実現したいこと
iOSでバーコードリーダを作成しようと思い各所を参考にし、実装したのですが、
メタデータが取得できない状況です。
発生している問題・エラーメッセージ
画面は正常にカメラが開かれて、赤枠も表示されます。
しかし、バーコードからメタデータが読み取れていないようです。
ビルドは成功しています。
該当のソースコード
swift
1import UIKit 2import AVFoundation 3 4class ViewController: UIViewController, AVCapturePhotoCaptureDelegate { 5 6 // Connect as outlet 7 @IBOutlet weak var previewView: UIView! 8 9 // Make instance 10 var captureSession = AVCaptureSession() 11 12 // Make notification 13 let notification = NotificationCenter.default 14 15 // Status for privacy and in-output 16 var authStatus:AuthorizedStatus = .authorized 17 var inOutStatus:InputOutputStatus = .ready 18 19 // Authorization Status 20 enum AuthorizedStatus { 21 case authorized 22 case notAuthorized 23 case failed 24 } 25 // InOut Status 26 enum InputOutputStatus { 27 case ready 28 case notReady 29 case failed 30 } 31 32 let x: CGFloat = 0.1 33 let y: CGFloat = 0.4 34 let width: CGFloat = 0.8 35 let height: CGFloat = 0.2 36 37 override func viewDidLoad(){ 38 super.viewDidLoad() 39 // interrupt if processing session 40 guard !captureSession.isRunning else { 41 return 42 } 43 // privacy authorization for camera 44 cameraAuth() 45 46 // Set InputOutput 47 setupInputOutput() 48 49 // Check camera is ready 50 if (authStatus == .authorized)&&(inOutStatus == .ready) { 51 // Set previewLayer 52 setPreviewLayer() 53 // Start session 54 captureSession.startRunning() 55 } else { 56 // alert 57 showAlert(appName: "Camera") 58 } 59 60 } 61 62 override func viewWillAppear(_ animated: Bool) { 63 super.viewWillAppear(animated) 64 65 if (captureSession.isRunning == false) { 66 captureSession.startRunning() 67 } 68 } 69 70 override func viewWillDisappear(_ animated: Bool) { 71 super.viewWillDisappear(animated) 72 73 if (captureSession.isRunning == true) { 74 captureSession.stopRunning() 75 } 76 } 77 78 func cameraAuth(){ 79 let status = AVCaptureDevice.authorizationStatus(for: AVMediaType.video) 80 switch status { 81 case .notDetermined: 82 AVCaptureDevice.requestAccess(for: AVMediaType.video, 83 completionHandler: { [unowned self] authorized in print("First Time", authorized.description) 84 if authorized { 85 self.authStatus = .authorized 86 } else { 87 self.authStatus = .notAuthorized 88 }}) 89 case .restricted, .denied: 90 authStatus = .notAuthorized 91 case .authorized: 92 authStatus = .authorized 93 } 94 } 95 96 func setupInputOutput(){ 97 // Set resolution 98 // captureSession.sessionPreset = AVCaptureSession.Preset. 99 100 // Set input 101 do { 102 // Obtain devise 103 let devise = AVCaptureDevice.default( 104 AVCaptureDevice.DeviceType.builtInWideAngleCamera, 105 for: AVMediaType.video, 106 position: AVCaptureDevice.Position.back 107 ) 108 109 // where to input 110 let input = try AVCaptureDeviceInput(device: devise!) 111 if captureSession.canAddInput(input){ 112 captureSession.addInput(input) 113 } else { 114 print("Failed to add input to session") 115 return 116 } 117 } catch let err as NSError { 118 print("Camera doesn't exist (err)") 119 return 120 } 121 122 // Set output 123 let metadataOutput = AVCaptureMetadataOutput() 124 if captureSession.canAddOutput(metadataOutput) { 125 captureSession.addOutput(metadataOutput) 126 metadataOutput.setMetadataObjectsDelegate((self as? AVCaptureMetadataOutputObjectsDelegate), queue: DispatchQueue.main) 127 metadataOutput.metadataObjectTypes = [.ean13] 128 metadataOutput.rectOfInterest = CGRect(x: y, y: 1 - x - width, width: height, height: width) 129 } else { 130 print("Failed to add output to session") 131 return 132 } 133 } 134 135 func setPreviewLayer(){ 136 // Make previewLayer 137 let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 138 previewLayer.frame = view.layer.bounds 139 previewLayer.videoGravity = AVLayerVideoGravity.resizeAspect 140 141 // Add preview 142 previewView.layer.addSublayer(previewLayer) 143 144 // Add red-frame 145// let detectionArea = UIView() 146// detectionArea.frame = CGRect(x: view.frame.size.width * x, y: view.frame.size.height * y, width: view.frame.size.width * width, height: view.frame.size.height * height) 147// detectionArea.layer.borderColor = UIColor.red.cgColor 148// detectionArea.layer.borderWidth = 3 149// view.addSubview(detectionArea) 150 } 151 152 func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection){ 153 for metadata in metadataObjects as! [AVMetadataMachineReadableCodeObject] { 154 // check whether it's barcode 155 if metadata.type == AVMetadataObject.ObjectType.ean13 { 156 if metadata.stringValue != nil { 157 print(metadata.stringValue!) 158 } 159 } 160 print("metadataOutput called") 161 162 // metadata 163 print(metadata.type) 164 print(metadata.stringValue!) 165 } 166 } 167 168 func showAlert(appName: String){ 169 let aTitle = "privacy authorization for " + appName 170 let aMessage = "Please authorize by Setting>Privacy> " + appName 171 let alert = UIAlertController(title: aTitle, message: aMessage, preferredStyle: .alert) 172 173 // OK(nothing) 174 alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) 175 176 // Open Setting 177 alert.addAction(UIAlertAction(title: "Open Setting", style: .default, handler: { action in UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)})) 178 179 // Display alert 180 self.present(alert, animated: false, completion: nil) 181 } 182} 183 184
試したこと
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。