AVFoundation:在视频会话中拍摄高质量的静态图像

时间:2017-03-16 10:11:29

标签: objective-c swift uiimage avfoundation

我在AVMediaTypeVideo类型的Capture-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

}

1 个答案:

答案 0 :(得分:0)

这是我在我的应用中使用的一小段代码。我希望这会有所帮助

recfile = open('/home/pi/Desktop/Recipt.txt', "a")
barfile = open('/home/pi/Desktop/Barcodes.txt').read()

if barcode in barfile:
    print("Your product has been found. ")
    while barcode in barfile:
        amount = input("How many do you want? ")
        print(barfile)

else:
    print("Your product was not found on our systems. ")

如果您未设置 captureSession 变量的sessionPreset属性,则默认情况下其值为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() }) } } 。唯一高于此值的预设为AVCapture​Session​Preset​High

相关问题