3

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

我使用 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];
}

这工作正常,我可以看到过滤后的实时图像。

应用了实时图像过滤器

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

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

我使用 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 );
        }
    }];
} );    
}

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

快照图像

与原版相比,它非常明亮。

我在这里错过了什么?

4

0 回答 0