Screen capture during video preview fails

耗尽温柔 提交于 2019-12-06 15:37:07

Don't capture the screen. Instead, capture a frame from the buffer and use that.

Implement the AVCaptureVideoDataOutputSampleBufferDelegate.

On the VideoDataOuput, set the setSampleBufferDelegate

Implement the captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) method.

When you store the image to the device, play the shutter sound yourself.

In the end, your code looks more like this:

var videoDataOutput:AVCaptureVideoDataOutput?;
var videoDataOutputQueue:dispatch_queue_t = dispatch_queue_create("VideoDataOutputQueue", DISPATCH_QUEUE_SERIAL);
var stillImageOutput:AVCaptureStillImageOutput?
var previewLayer:AVCaptureVideoPreviewLayer?
var captureDevice:AVCaptureDevice?
let session = AVCaptureSession()

func beginSession() {
    var err : NSError? = nil
    var deviceInput:AVCaptureDeviceInput = AVCaptureDeviceInput(device: captureDevice!, error: &err);

    if err != nil {
        println("error: \(err?.localizedDescription)");
    }
    if session.canAddInput(deviceInput){
        session.addInput(deviceInput);
    }

    stillImageOutput = AVCaptureStillImageOutput()
    videoDataOutput = AVCaptureVideoDataOutput()

    if let videoDataOutput = videoDataOutput, stillImageOutput = stillImageOutput {
        videoDataOutput.alwaysDiscardsLateVideoFrames=true;
        videoDataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey:Int(kCVPixelFormatType_32BGRA)]
        videoDataOutput.setSampleBufferDelegate(self, queue:self.videoDataOutputQueue)
        if session.canAddOutput(videoDataOutput){
            session.addOutput(videoDataOutput)
        }

        stillImageOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
        if session.canAddOutput(stillImageOutput) {
            session.addOutput(stillImageOutput)
        }

        videoDataOutput.connectionWithMediaType(AVMediaTypeVideo).enabled = true

        if let previewLayer = AVCaptureVideoPreviewLayer(session: self.session) {
            self.previewLayer = previewLayer
            previewLayer.videoGravity = AVLayerVideoGravityResizeAspect
            previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight

            var rootLayer :CALayer = self.previewView.layer;
            rootLayer.masksToBounds=true;
            previewLayer.frame = rootLayer.bounds;
            rootLayer.addSublayer(self.previewLayer);
            session.startRunning();
        }
    }
}

// this gets called periodically with an image
func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
    if let image = CheckResponse.imageFromSampleBuffer(sampleBuffer) {
        if keepImage(image) {
            AudioServicesPlaySystemSound(1108)
            session.stopRunning()
        }
    }
}

// This is in the Objective-C CheckResponse class to get an image from the buffer:
+ (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer {
    CVPixelBufferRef pb = CMSampleBufferGetImageBuffer(sampleBuffer);
    CIImage *ciimg = [CIImage imageWithCVPixelBuffer:pb];

    // show result
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef ref = [context createCGImage:ciimg fromRect:ciimg.extent];
    UIImage *image = [UIImage imageWithCGImage:ref scale:1.0 orientation:(UIImageOrientationUp)];

    CFRelease(ref);

    return image;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!