gpt4 book ai didi

objective-c - AVFoundation:在视频 session 中拍摄高质量静态图像

转载 作者:行者123 更新时间:2023-11-30 12:36:18 25 4
gpt4 key购买 nike

我正在分析 AVMediaTypeVideo 类型的捕获 session 中的实时图像。如何在某些事件中捕获高质量静态图像(而不是低分辨率样本缓冲区)=

var videoCaptureDevice: AVCaptureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
var device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
var previewLayer: AVCaptureVideoPreviewLayer?
var captureSession = AVCaptureSession()
let cameraOutput = AVCapturePhotoOutput()


//called in view did load
private func setupCamera() {

let input = try? AVCaptureDeviceInput(device: videoCaptureDevice)
captureSession.sessionPreset = AVCaptureSessionPreset640x480
if self.captureSession.canAddInput(input) {
self.captureSession.addInput(input)
}
self.previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)


let videoDataOutput = AVCaptureVideoDataOutput()
if self.captureSession.canAddOutput(videoDataOutput){
self.captureSession.addOutput(videoDataOutput)
videoDataOutput.setSampleBufferDelegate(self, queue: DispatchQueue.main)
}

}

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {

// sampleBuffer is analysed
// if result is positive, I would like to take a high quality picture

}

最佳答案

这是我在应用程序中使用的一个小片段。我希望这会有所帮助

private func takePhoto() -> Void
{
if let videoConnection = stillImageOutput!.connection(withMediaType: AVMediaTypeVideo)
{
videoConnection.videoOrientation = AVCaptureVideoOrientation.portrait

stillImageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (sampleBuffer, error) in
guard let buffer = sampleBuffer else
{
return
}

let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer)
let dataProvider = CGDataProvider(data: imageData as! CFData)
let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!,
decode: nil,
shouldInterpolate: true,
intent: CGColorRenderingIntent.defaultIntent)

// The image taked
let image: UIImage = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.right)

// Detenemos la captura de imagenes
self.captureSession!.stopRunning()
})
}
}

如果您没有设置captureSession变量的sessionPreset属性,默认情况下他的值为AVCapture​Session​Preset​High。唯一高于此的预设是 AVCaptureSessionPresetPhoto

关于objective-c - AVFoundation:在视频 session 中拍摄高质量静态图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42830977/

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