AVCaptureStillImageOutput和AVCaptureVideoDataOutput输出不同的图像

时间:2017-09-27 18:28:03

标签: ios objective-c camera cifilter glkview

我正在创建一个实时相机过滤器应用程序。

我使用AVCaptureVideoDataOutputSampleBufferDelegate捕获输出视频,然后应用过滤器。

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CIImage *sourceImage = [CIImage imageWithCVPixelBuffer:(CVPixelBufferRef)imageBuffer options:nil];
CGRect sourceExtent = sourceImage.extent;

CIImage *filteredImage = [self customFilterImage:sourceImage];

CGFloat sourceAspect = sourceExtent.size.width / sourceExtent.size.height;
CGFloat previewAspect = _videoPreviewViewBounds.size.width  / _videoPreviewViewBounds.size.height;

CGRect drawRect = sourceExtent;
if (sourceAspect > previewAspect)
{
    drawRect.origin.x += (drawRect.size.width - drawRect.size.height * previewAspect) / 2.0;
    drawRect.size.width = drawRect.size.height * previewAspect;
}
else
{
    drawRect.origin.y += (drawRect.size.height - drawRect.size.width / previewAspect) / 2.0;
    drawRect.size.height = drawRect.size.width / previewAspect;
}

[_videoPreviewView bindDrawable];

if (_eaglContext != [EAGLContext currentContext])
    [EAGLContext setCurrentContext:_eaglContext];

if (filteredImage){
    [_ciContext drawImage:filteredImage inRect:_videoPreviewViewBounds fromRect:drawRect];
}

[_videoPreviewView display];
}

这很好用,我可以看到过滤后的实时图像。

Live image filter applied

当按下按钮时,我想拍摄我在相机中看到的内容的快照,并将其保存为手机中的图像。图像质量应该很好。

我也在这里使用完全相同的过滤器。

我使用AVCaptureStillImageOutput拍摄快照。

- (IBAction)clickPhotoBtn:(id)sender {

dispatch_async( _captureSessionQueue, ^{
    AVCaptureConnection *connection = [_captureImageOutput connectionWithMediaType:AVMediaTypeVideo];

    [_captureImageOutput captureStillImageAsynchronouslyFromConnection:connection completionHandler:^( CMSampleBufferRef imageDataSampleBuffer, NSError *error ) {
        if ( imageDataSampleBuffer ) {
            CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(imageDataSampleBuffer);
            CIImage *sourceImage = [CIImage imageWithCVPixelBuffer:(CVPixelBufferRef)imageBuffer options:nil];

            CGImageRef imageRef;
            CIImage *filteredImage = [self customFilterImage:sourceImage];

            CIContext *context = [CIContext contextWithOptions:nil];
            imageRef = [context createCGImage:filteredImage fromRect:filteredImage.extent];
            UIImage *lastImage = [UIImage imageWithCGImage:imageRef scale:1.0 orientation:UIImageOrientationRight];

            UIImageWriteToSavedPhotosAlbum(lastImage, nil, nil, nil);

        }
        else {
            NSLog( @"Could not capture still image: %@", error );
        }
    }];
} );    
}

这里的问题是这里拍摄的照片与我在手机屏幕上实时看到的不一样。

snapshot image

与原版相比非常明亮。

我在这里错过了什么?

0 个答案:

没有答案