前提・実現したいこと
このサイトを参考にして、画像を取得するコードを書いています。
https://dev.classmethod.jp/smartphone/ios-avfoundation-avcapturevideodataoutput/
外カメではなく内カメで作りたかったので該当の箇所に手を加えました
.backだと正常に起動するのですがfrontにするとエラーを吐きます。
発生している問題・エラーメッセージ
Thread 1: signal SIGABRT
該当のソースコード
swift
1import UIKit 2import AVFoundation 3 4protocol AVCaptureDelegate { 5 func capture(image: UIImage) 6} 7 8class AVCapture:NSObject, AVCaptureVideoDataOutputSampleBufferDelegate { 9 10 var captureSession: AVCaptureSession! 11 var delegate: AVCaptureDelegate? 12 var videoDevice: AVCaptureDevice! 13 14 15 var counter = 0 16 let DISMISS_COUNT = 5 17 18 override init(){ 19 super.init() 20 21 captureSession = AVCaptureSession() 22 captureSession.sessionPreset = AVCaptureSession.Preset.hd1920x1080 23 24 25 26 // 該当の箇所 27 videoDevice = AVCaptureDevice.default( 28 AVCaptureDevice.DeviceType.builtInWideAngleCamera, 29 for: AVMediaType.video, 30 position: .front) // position: .front 31 32 33 videoDevice?.activeVideoMinFrameDuration = CMTimeMake(1, 30)// 1/30秒 (1秒間に30フレーム) 34 35 36 let videoInput = try! AVCaptureDeviceInput.init(device: videoDevice!) 37 captureSession.addInput(videoInput) ////////breakpointの箇所 38 39 40 let videoDataOutput = AVCaptureVideoDataOutput() 41 videoDataOutput.setSampleBufferDelegate(self, queue: DispatchQueue.main) 42 videoDataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as AnyHashable as! String : Int(kCVPixelFormatType_32BGRA)] 43 videoDataOutput.alwaysDiscardsLateVideoFrames = true 44 45 captureSession.addOutput(videoDataOutput) 46 47 48 DispatchQueue.global(qos: .userInitiated).async { 49 self.captureSession.startRunning() 50 } 51 52 } 53 54 55 func captureOutput(_ captureOutput: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { 56 57 if (counter % 60 == 0) { 58 59 let image = imageFromSampleBuffer(sampleBuffer: sampleBuffer) 60 delegate?.capture(image: image) 61 62 if let camData = CMGetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix, nil) as? Data { 63 64 let matrix: matrix_float3x3 = camData.withUnsafeBytes { $0.pointee } 65 print(matrix) 66 } 67 } 68 counter += 1 69 } 70 71 72 func imageFromSampleBuffer(sampleBuffer :CMSampleBuffer) -> UIImage { 73 74 let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)! 75 76 CVPixelBufferLockBaseAddress(imageBuffer, CVPixelBufferLockFlags(rawValue: 0)) 77 78 let base = CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0)! 79 let bytesPerRow = UInt(CVPixelBufferGetBytesPerRow(imageBuffer)) 80 let width = UInt(CVPixelBufferGetWidth(imageBuffer)) 81 let height = UInt(CVPixelBufferGetHeight(imageBuffer)) 82 83 let colorSpace = CGColorSpaceCreateDeviceRGB() 84 let bitsPerCompornent = 8 85 let bitmapInfo = CGBitmapInfo(rawValue: (CGBitmapInfo.byteOrder32Little.rawValue | CGImageAlphaInfo.premultipliedFirst.rawValue) as UInt32) 86 let newContext = CGContext(data: base, width: Int(width), height: Int(height), bitsPerComponent: Int(bitsPerCompornent), bytesPerRow: Int(bytesPerRow), space: colorSpace, bitmapInfo: bitmapInfo.rawValue)! as CGContext 87 88 CVPixelBufferUnlockBaseAddress(imageBuffer, CVPixelBufferLockFlags(rawValue: 0)) 89 90 let imageRef = newContext.makeImage()! 91 let image = UIImage(cgImage: imageRef, scale: 1.0, orientation: UIImageOrientation.right) 92 93 return image 94 } 95} 96
試したこと
breakpointを見たところ、上記の箇所を指しています。
補足情報(FW/ツールのバージョンなど)
よろしくお願いします。
カメラのことよく知りませんが、フロントカメラは .builtInWideAngleCamera に対応しているのでしょうか?
この記事にそのような記載がありましたが、私もよくわかっていません.. https://i-app-tec.com/ios/avcapturestillimageoutput.html
あなたの回答
tips
プレビュー