gpt4 book ai didi

ios - 拍摄照片时未使用 AVCaptureDevice 的设置

转载 作者:行者123 更新时间:2023-11-28 07:35:46 25 4
gpt4 key购买 nike

我正在尝试编写一个基本的 iOS 应用程序,以使用一些固定设置捕获 RAW 照片。但是我在拍照时遇到了问题。我在设备配置(iPhone Xs 的 Telecamera)中选择的设置显示在预览中(固定焦点,改变照明,取决于我在代码中选择的 iso),当我按下捕获按钮时不应用并且运行 capturePhoto 方法。然后我可以看到焦点调整和手电筒点火。起初我怀疑 session 预设会在我开始 session 时覆盖我的设置,但锁定设备直到 session 运行没有帮助。提前致谢!

我的代码:

import UIKit
import AVFoundation
import Photos

class ViewController: UIViewController {

//instance variables:

//dispatch-queue for session setup:
var setupQueue = DispatchQueue(label: "sessionSetupQueue")

//session-related:
var captureSession: AVCaptureSession!
var camera: AVCaptureDevice!
var captureInput: AVCaptureInput!
var photoOutput: AVCapturePhotoOutput!
var photoSettings: AVCapturePhotoSettings!

//view-model:
var previewLayer: AVCaptureVideoPreviewLayer!

@IBOutlet weak var capturePreviewView: UIView!
@IBOutlet weak var photoCaptureButton: UIButton!

}

//extension with methods and functions:

extension ViewController {

override func viewDidLoad() {
super.viewDidLoad()


do {
try self.configureSession()

//Create a preview view:
self.previewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession)
self.previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill

//connect preview view to UIView object


self.capturePreviewView.layer.insertSublayer(self.previewLayer, at: 0)
self.previewLayer.frame = self.view.frame


} catch {
print("could not set up a CaptureSession!")
return
}
}

//configuration of the capture session
func configureSession() throws {

// create a capture session and set it up for configuration
self.captureSession = AVCaptureSession()
captureSession.sessionPreset = .photo
captureSession.beginConfiguration()

//find tele-camera and set it as input to the session
let camera = AVCaptureDevice.default(.builtInTelephotoCamera, for: .video, position: .back)
self.camera = camera

//configure the device before adding it as an input to the session
do {
try configureDevice()
}
guard let captureInput = try? AVCaptureDeviceInput(device: self.camera!), captureSession.canAddInput(captureInput) else { return }
captureSession.addInput(captureInput)

//Set an output for the session:
self.photoOutput = AVCapturePhotoOutput()
photoOutput.isHighResolutionCaptureEnabled = true
guard captureSession.canAddOutput(photoOutput) else { return }
captureSession.addOutput(photoOutput)

//commit the configuration:
captureSession.commitConfiguration()

//start the session:
captureSession.startRunning()

}

//configure the device

func configureDevice() throws {

//lock the device for setup

do {
try camera.lockForConfiguration()
} catch {
print("could not lock device!")
return
}

//set camera parameters
camera.focusMode = .locked
if camera.isLockingFocusWithCustomLensPositionSupported {
camera.setFocusModeLocked(lensPosition: 1.0, completionHandler: nil)
}
if camera.isExposureModeSupported(.custom) {
camera.setExposureModeCustom(duration: AVCaptureDevice.currentExposureDuration, iso: 60, completionHandler: nil)
}
camera.torchMode = .off

//end setup by unlocking for configuration
camera.unlockForConfiguration()

}

//capture fuction

@IBAction func capturePhoto (_ sender: UIButton) {

//settings configuration

//setup settings for RAW capture
self.photoSettings = AVCapturePhotoSettings(rawPixelFormatType: self.photoOutput.availableRawPhotoPixelFormatTypes.first!)

//disable red eye correction, image stabilization and flash
photoSettings.isAutoRedEyeReductionEnabled = false
photoSettings.isAutoStillImageStabilizationEnabled = false
photoSettings.flashMode = .on

//capture the photo
self.photoOutput.capturePhoto(with: self.photoSettings, delegate: self)

//visual effects:

self.capturePreviewView.layer.opacity = 0
UIView.animate(withDuration: 0.25) {
self.capturePreviewView.layer.opacity = 1
}

}

}

//photoOutput delegate functions:

extension ViewController: AVCapturePhotoCaptureDelegate {

func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
if error != nil { print("Error capturing photo!"); return }

PHPhotoLibrary.requestAuthorization { status in
guard status == .authorized else { return }
PHPhotoLibrary.shared().performChanges({
//add captured photos data to asset
let creationRequest = PHAssetCreationRequest.forAsset()
creationRequest.addResource(with: .photo, data: photo.fileDataRepresentation()!, options: nil)
})
}

}

}

最佳答案

我找到了解决方案:

问题出在 photoSettings 中的 flashMode。使用 flashMode = .on 会覆盖我事先选择的大部分设备设置。

为了评估场景的闪光强度,该设备使用了一些自动对焦和手电筒。

没有闪光灯,自定义曝光设置正确并且焦点保持固定。

关于ios - 拍摄照片时未使用 AVCaptureDevice 的设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53203169/

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