【问题标题】:AVFoundation: Take High Quality Still Image in Video-SessionAVFoundation:在视频会话中拍摄高质量的静止图像
【发布时间】:2017-08-07 10:39:51
【问题描述】:

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

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

}

【问题讨论】:

    标签: objective-c swift uiimage avfoundation


    【解决方案1】:

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

    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

    【讨论】:

    • 感谢您的回答!满足条件时我调用了您的方法,并在您的方法中将 AVCapture-Session-Preset 设置为 Photo(因为我需要高分辨率的照片,但低分辨率的预览)。但是,图像非常暗。你知道为什么我可以让它获得足够的光线吗?
    • @pascal 如果图像很暗,那么您在更改其参数后没有让会话有足够的时间使其自动曝光正确。尽早更改预设,或手动调整其他参数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 1970-01-01
    • 2021-05-09
    • 2021-05-15
    相关资源
    最近更新 更多