Last active
March 28, 2021 14:41
-
-
Save DaisukeNagata/f6b80479dc22f96673ae53698df63458 to your computer and use it in GitHub Desktop.
Get_DepthData
This file contains 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 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") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It can be used on iPhones that have this function.