我正在运行下面的代码,但它没有捕获任何照片,但给我一个错误消息,说“无法记录”。在这方面的文档有点少,所以如果有任何帮助,我们将不胜感激。
import Foundation
import AVFoundation
class CapturePhoto: NSObject, AVCapturePhotoCaptureDelegate{
var capture = AVCapturePhotoOutput()
var availableCameraSettings: Array<AVCapturePhotoSettings> = []
var session = AVCaptureSession()
let prompt = Prompt()
override init(){
super.init()
createSettings()
configOutput()
configSession()
startSesssion()
// do any other config here
}
func configOutput(){
capture.isHighResolutionCaptureEnabled = true
capture.setPreparedPhotoSettingsArray( availableCameraSettings, completionHandler: nil )
}
func configSession(){
session.stopRunning()
session.beginConfiguration()
session.sessionPreset = .photo
let input = try? AVCaptureDeviceInput( device: getDevice() )
if( session.canAddOutput( capture ) ){
session.addOutput( capture )
}
if ( session.canAddInput( input! ) ){
session.addInput( input! )
}
session.commitConfiguration()
}
func startSesssion(){
session.startRunning()
}
func stopSession(){
session.stopRunning()
}
func getDevice() -> AVCaptureDevice{
if let device = AVCaptureDevice.default(.builtInWideAngleCamera,
for: .video, position: .back) {
return device
} else {
fatalError("Missing expected back camera device.")
}
}
func getBestSettings() -> AVCapturePhotoSettings{
return AVCapturePhotoSettings( /* rawPixelFormatType: 0 */ )
}
func createSettings(){
let settings = getBestSettings()
settings.flashMode = .on
if #available(iOS 14.1, *) {
settings.isAutoContentAwareDistortionCorrectionEnabled = false
}
settings.isAutoRedEyeReductionEnabled = false
settings.isCameraCalibrationDataDeliveryEnabled = false
settings.isHighResolutionPhotoEnabled = false
settings.isPortraitEffectsMatteDeliveryEnabled = false
availableCameraSettings.append( settings )
let ditto = AVCapturePhotoSettings( from: settings )
ditto.flashMode = .off
availableCameraSettings.append( ditto )
}
func captureImage()
{
for setting in availableCameraSettings{
let settings = AVCapturePhotoSettings( from: setting )
capture.capturePhoto(with: settings, delegate: self )
}
}
func photoOutput(_ output: AVCapturePhotoOutput,
didFinishProcessingPhoto photo: AVCapturePhoto,
error: Error?)
{
if error != nil {
print( error!.localizedDescription )
prompt.prompt ( Info( error!.localizedDescription, 0 ) )
} else{
print( photo.timestamp )
print ( ( photo.isRawPhoto ? " Raw" : " Not raw" ) + "\n" )
DispatchQueue.global( qos: .background ).async {
AudioServicesPlaySystemSound( 1108 )
}
}
}
}发布于 2020-12-12 00:34:45
据我所知,您不能启动捕获会话,因为ARSession会抓住摄像头,因此您无法准确捕获您想要的图像。
但是,要获得不包含所有3d对象的场景,可以像这样获取ARSession当前使用的图像:self.arView.session.currentFrame?.capturedImage,其中self.arView是当前的ARView。
capturedImage是一种CVPixelBuffer,有很多方法可以将其转换为UIImage或您可能需要的任何其他格式,如下所示:
if let img = self.arView.session.currentFrame?.capturedImage {
let ciimg = CIImage(cvImageBuffer: img)
let finImage = UIImage(ciImage: ciimg)
}您可能还必须针对设备方向调整图像。
原始答案:
如果要使用3d objects view.snapshot(:)显示场景
https://developer.apple.com/documentation/realitykit/arview/3255319-snapshot
https://stackoverflow.com/questions/65215443
复制相似问题