gpt4 book ai didi

ios - 设备相机数据(不是图像而是实际的相机属性)

转载 作者:行者123 更新时间:2023-11-28 15:14:51 25 4
gpt4 key购买 nike

我需要访问我的设备相机数据(不是图像数据!已经有了)。例如,“针孔”fx 和 fy 以及我可能得到的任何其他东西。

目前,我正在使用带有自定义 UI 的 AVFoundation 的“AVCaptureSession”。但之前我使用了“UIImagePickerController”,它有一个名为

的委托(delegate)方法
imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])

我能够从“信息”字典中检索到拍摄的照片。它还为我提供了有关相机及其功能的非常详细的信息。就目前而言,我不知道如何在“AVCaptureSession”摄影期间检索同样的详细信息。请帮忙

最佳答案

您需要使用 AVCapturePhotoOutput 而不是 AVCaptureStillImageOutput。

首先,下面的代码需要在您的类中添加以下变量

private let photoOutput = AVCapturePhotoOutput()
private var inProgressPhotoCaptureDelegates = [Int64 : AVPhotoCaptureDelegate]()
private let sessionQueue = DispatchQueue(label: "session queue", attributes: [], target: nil) // Communicate with the session
private var videoDeviceOrientation : AVCaptureVideoOrientation = .portrait // this needs updated as the device orientation changes

在 AVSession 设置中添加照片输出

// Add photo output.
if session.canAddOutput(photoOutput) {
session.addOutput(photoOutput)

self.photoOutput.isHighResolutionCaptureEnabled = true
}

你的 capturePhoto() 函数应该设置如下

func capturePhoto(aspectRatio : Float, metaData : NSDictionary?) {
sessionQueue.async {
// Update the photo output's connection to match the video orientation of the video preview layer.
if let photoOutputConnection = self.photoOutput.connection(with: AVMediaType.video) {
photoOutputConnection.videoOrientation = self.videoDeviceOrientation
}

// Capture a JPEG photo with flash set to off and high resolution photo enabled.
let photoSettings = AVCapturePhotoSettings()
photoSettings.flashMode = .off
photoSettings.isHighResolutionPhotoEnabled = true
if photoSettings.availablePreviewPhotoPixelFormatTypes.count > 0 {
photoSettings.previewPhotoFormat = [kCVPixelBufferPixelFormatTypeKey as String : photoSettings.availablePreviewPhotoPixelFormatTypes.first!]
}

// Use a separate object for the photo capture delegate to isolate each capture life cycle.
let photoCaptureDelegate = MyAVPhotoCaptureDelegate(completed: { [unowned self] photoCaptureDelegate in
// When the capture is complete, remove a reference to the photo capture delegate so it can be deallocated.
self.sessionQueue.async { [unowned self] in
self.inProgressPhotoCaptureDelegates[photoCaptureDelegate.requestedPhotoSettings.uniqueID] = nil
}
)

/*
The Photo Output keeps a weak reference to the photo capture delegate so
we store it in an array to maintain a strong reference to this object
until the capture is completed.
*/
self.inProgressPhotoCaptureDelegates[photoCaptureDelegate.requestedPhotoSettings.uniqueID] = photoCaptureDelegate
self.photoOutput.capturePhoto(with: photoSettings, delegate: photoCaptureDelegate)
}
}

上面的 capturePhoto() 函数中引用的 MyAVPhotoCaptureDelegate 类需要按如下方式设置

class MyAVPhotoCaptureDelegate: NSObject, AVCapturePhotoCaptureDelegate {

init(completed: @escaping (AVPhotoCaptureDelegate) -> ()) {
self.completed = completed
}

private func didFinish() {
completed(self)
}

func photoOutput(_ captureOutput: AVCapturePhotoOutput, willCapturePhotoFor resolvedSettings: AVCaptureResolvedPhotoSettings) {
}

func photoOutput(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?, previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {
if let photoSampleBuffer = photoSampleBuffer {
let propertiesDictionary = NSMutableDictionary()

if let exif = CMGetAttachment(photoSampleBuffer, kCGImagePropertyExifDictionary as NSString, nil) {
if let exifDictionary = exif as? NSMutableDictionary {
// view exif data
}
}

photoData = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer)
}
else {
print("Error capturing photo: \(String(describing:error))")
return
}
}

func photoOutput(_ captureOutput: AVCapturePhotoOutput, didFinishCaptureFor resolvedSettings: AVCaptureResolvedPhotoSettings, error: Error?) {

// Use PHPhotoLibrary to save photoData to photo library
...
}

private let completed : (MyAVPhotoCaptureDelegate) -> ()
}

大部分代码来 self 的 AVCam 版本,删除了我的实现细节。我省略了保存到照片库的代码,但您可以从示例代码中提取它。你可以在我评论“查看exif数据”的地方查看exif数据

关于ios - 设备相机数据(不是图像而是实际的相机属性),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47020259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com