Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get_DepthData
import UIKit
import AVFoundation
class ViewController: UIViewController, AVCapturePhotoCaptureDelegate {
private var videoPreviewLayer: AVCaptureVideoPreviewLayer?
private var stillImageOutput = AVCapturePhotoOutput()
private var captureSession = AVCaptureSession()
private var capturepDevice: AVCaptureDevice?
private var session: AVCaptureSession?
private var previewLayer: CALayer?
private var preview = UIView()
private var bt = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
bt.frame = CGRect(x: 0,
y: view.frame.height - 200,
width: self.view.frame.width,
height: 100)
bt.addTarget(self, action: #selector(takePhoto), for: .touchUpInside)
bt.setTitle("Button", for: .normal)
bt.setTitleColor(.black, for: .normal)
preview.frame = self.view.bounds
view.addSubview(preview)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
prepareCamera()
beginSession()
preview.addSubview(bt)
}
private func prepareCamera() {
if let availableDevice = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInTrueDepthCamera,
.builtInDualCamera,
.builtInWideAngleCamera],
mediaType: .video,
position: .back).devices.first {
capturepDevice = availableDevice
}
}
private func beginSession() {
guard let capturepDevice = capturepDevice else { return }
captureSession.beginConfiguration()
captureSession.sessionPreset = .photo
do {
let captureDeviceInput = try AVCaptureDeviceInput(device: capturepDevice)
captureSession.addInput(captureDeviceInput)
} catch {
print(error.localizedDescription)
}
let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
self.previewLayer = previewLayer
self.previewLayer?.frame = preview.frame
preview.layer.addSublayer(previewLayer)
let dataOutput = AVCaptureVideoDataOutput()
dataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String:kCVPixelFormatType_32BGRA]
if captureSession.canAddOutput(dataOutput) {
captureSession.addOutput(dataOutput)
captureSession.addOutput(stillImageOutput)
captureSession.commitConfiguration()
}
if stillImageOutput.isDepthDataDeliverySupported {
stillImageOutput.isHighResolutionCaptureEnabled = true
stillImageOutput.isDepthDataDeliveryEnabled = stillImageOutput.isDepthDataDeliverySupported
}
if captureSession.isRunning { return }
captureSession.startRunning()
}
@objc private func takePhoto() {
let photoSettings = AVCapturePhotoSettings()
photoSettings.flashMode = .auto
photoSettings.isHighResolutionPhotoEnabled = true
if photoSettings.availablePreviewPhotoPixelFormatTypes.count > 0 {
photoSettings.previewPhotoFormat = [kCVPixelBufferPixelFormatTypeKey as String : photoSettings.availablePreviewPhotoPixelFormatTypes.first!]
}
if stillImageOutput.isDepthDataDeliveryEnabled {
photoSettings.isDepthDataDeliveryEnabled = true
}
stillImageOutput.capturePhoto(with: photoSettings, delegate: self)
}
// Delegate: AVCapturePhotoCaptureDelegate
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
print(photo.depthData, "photo")
}
}
@daisukenagata

This comment has been minimized.

Copy link
Owner Author

@daisukenagata daisukenagata commented Mar 28, 2021

It can be used on iPhones that have this function.

.builtInTrueDepthCamera,
.builtInDualCamera,
.builtInWideAngleCamera

スクリーンショット 2021-03-28 21 34 41

@daisukenagata

This comment has been minimized.

Copy link
Owner Author

@daisukenagata daisukenagata commented Mar 28, 2021

Don't forget to set info plist
スクリーンショット 2021-03-28 21 38 41

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment