【发布时间】:2017-06-12 19:03:30
【问题描述】:
我正在尝试使用 AVDepthData 从 iOS 11 中的相机获取深度数据,但是当我使用 AVCapturePhotoCaptureDelegate 设置 photoOutput 时,photo.depthData 为 nil。
所以我尝试使用 AVCaptureDepthDataOutput 设置 AVCaptureDepthDataOutputDelegate,但我不知道如何捕获深度照片?
有人从 AVDepthData 得到图像吗?
编辑:
这是我尝试过的代码:
// delegates: AVCapturePhotoCaptureDelegate & AVCaptureDepthDataOutputDelegate
@IBOutlet var image_view: UIImageView!
@IBOutlet var capture_button: UIButton!
var captureSession: AVCaptureSession?
var sessionOutput: AVCapturePhotoOutput?
var depthOutput: AVCaptureDepthDataOutput?
var previewLayer: AVCaptureVideoPreviewLayer?
@IBAction func capture(_ sender: Any) {
self.sessionOutput?.capturePhoto(with: AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.jpeg]), delegate: self)
}
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
self.previewLayer?.removeFromSuperlayer()
self.image_view.image = UIImage(data: photo.fileDataRepresentation()!)
let depth_map = photo.depthData?.depthDataMap
print("depth_map:", depth_map) // is nil
}
func depthDataOutput(_ output: AVCaptureDepthDataOutput, didOutput depthData: AVDepthData, timestamp: CMTime, connection: AVCaptureConnection) {
print("depth data") // never called
}
override func viewDidLoad() {
super.viewDidLoad()
self.captureSession = AVCaptureSession()
self.captureSession?.sessionPreset = .photo
self.sessionOutput = AVCapturePhotoOutput()
self.depthOutput = AVCaptureDepthDataOutput()
self.depthOutput?.setDelegate(self, callbackQueue: DispatchQueue(label: "depth queue"))
do {
let device = AVCaptureDevice.default(for: .video)
let input = try AVCaptureDeviceInput(device: device!)
if(self.captureSession?.canAddInput(input))!{
self.captureSession?.addInput(input)
if(self.captureSession?.canAddOutput(self.sessionOutput!))!{
self.captureSession?.addOutput(self.sessionOutput!)
if(self.captureSession?.canAddOutput(self.depthOutput!))!{
self.captureSession?.addOutput(self.depthOutput!)
self.previewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession!)
self.previewLayer?.frame = self.image_view.bounds
self.previewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
self.previewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
self.image_view.layer.addSublayer(self.previewLayer!)
}
}
}
} catch {}
self.captureSession?.startRunning()
}
我正在尝试两件事,一件是深度数据为零,另一件是我试图调用深度委托方法。
有人知道我错过了什么吗?
【问题讨论】:
-
能否提供您尝试过的代码?
-
当然。我在尝试两件事合二为一,我会尝试用代码更多地解释它。
-
您使用的是 iPhone 7 吗?我认为您需要双摄像头才能获得深度
-
是的,iPhone 7 配备了双摄像头,但 AVDepthData 仍然为零。
-
有人让它工作吗? @Coder256 你用的是什么硬件?
标签: swift camera ios11 color-depth