将CVImageBufferRef转换为PNG格式

4

我希望使用QTKit.framework将摄像头输入的所有帧写入磁盘。但是,我得到的所有图像都是半透明的,而且缺少一些颜色,可能是颜色空间问题? ;( 在预览视图中它们看起来不错,但当我将它们写入时,“某些事情”发生了。 我想知道那是什么。

-(void)savePNGImage:(CGImageRef)imageRef path:(NSString *)path {
    NSURL *outURL = [[NSURL alloc] initFileURLWithPath:path]; 
    CGImageDestinationRef dr = CGImageDestinationCreateWithURL ((CFURLRef)outURL, (CFStringRef)@"public.png" , 1, NULL);
    CGImageDestinationAddImage(dr, imageRef, NULL);
    CGImageDestinationFinalize(dr);
    [outURL release];
}
-(void)saveJPEGImage:(CGImageRef)imageRef path:(NSString *)path {
    CFMutableDictionaryRef mSaveMetaAndOpts = CFDictionaryCreateMutable(nil, 0,
                                                                    &kCFTypeDictionaryKeyCallBacks,  &kCFTypeDictionaryValueCallBacks);
    CFDictionarySetValue(mSaveMetaAndOpts, kCGImageDestinationLossyCompressionQuality, 
                         [NSNumber numberWithFloat:1.0]);   // set the compression quality here
    NSURL *outURL = [[NSURL alloc] initFileURLWithPath:path];
    CGImageDestinationRef dr = CGImageDestinationCreateWithURL ((CFURLRef)outURL, (CFStringRef)@"public.jpeg" , 1, NULL);
    CGImageDestinationAddImage(dr, imageRef, mSaveMetaAndOpts);
    CGImageDestinationFinalize(dr);
    [outURL release];
}


- (void)captureOutput:(QTCaptureOutput *)captureOutput 
  didOutputVideoFrame:(CVImageBufferRef)videoFrame 
     withSampleBuffer:(QTSampleBuffer *)sampleBuffer 
       fromConnection:(QTCaptureConnection *)connection{

    CVPixelBufferLockBaseAddress(videoFrame,0); 
    uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(videoFrame); 
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(videoFrame); 
    size_t width = CVPixelBufferGetWidth(videoFrame); 
    size_t height = CVPixelBufferGetHeight(videoFrame); 
    CVPixelBufferUnlockBaseAddress(videoFrame,0);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef newContext = CGBitmapContextCreate(baseAddress, 
                                                        width, height, 8, 
                                                        bytesPerRow, 
                                                        colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); 
        CGImageRef frame = CGBitmapContextCreateImage(newContext); 

    CGContextRelease(newContext); 
    CGColorSpaceRelease(colorSpace);

    [self savePNGImage:frame path:[NSString stringWithFormat:@"/Users/nacho4d/Desktop/framesCam/frame%003d.png", frameNum++]];
[self saveJPEGImage:frame path:[NSString stringWithFormat:@"/Users/nacho4d/Desktop/framesCam/frame%003d.jpeg", frameNum++]];

    CGImageRelease(frame);
}

Capturer的属性只有帧大小和像素格式,例如: [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32ARGB], (id)kCVPixelBufferPixelFormatTypeKey 更新:
我也尝试了使用JPEG,我得到了同样类型的图像,就像图片中缺少一些渠道,而且所有的写入帧都有一个白色的背景。(因为JPEG不允许透明度?)
原始图像和写入(JPEG)的图像如下所示: alt text alt text (我没有展示PNG因为它是透明的,在浏览器中很难看到)
提前感谢。

你正在泄露URL... - jakev
3个回答

3

我不确定你的问题是什么,但这就是我如何将图片保存到磁盘的方式。只需传入从委托方法中获取的CVImageBufferRef,以及您自己的URL,它应该可以正常工作。至少对我来说是这样的。祝你好运!

-(void)saveImage:(CVImageBufferRef)image toURL:(NSURL*)url{

    NSCIImageRep *imageRep = [NSCIImageRep imageRepWithCIImage:[CIImage imageWithCVImageBuffer:image]];

    NSImage *_image = [[NSImage alloc] initWithSize:[imageRep size]];
    [_image addRepresentation:imageRep];

    NSData *bitmapData = [_image TIFFRep    resentation];
    NSBitmapImageRep *bitmapRep = [NSBitmapImageRep imageRepWithData:bitmapData];
    NSData *imageData = [bitmapRep representationUsingType:NSJPEGFileType properties:nil];

    [_image release];
    _image = [[NSImage alloc] initWithData:imageData];

    NSBitmapImageRep *imgRep = [[_image representations] objectAtIndex: 0];
    NSData *data = [imgRep representationUsingType: NSPNGFileType properties: nil];
    [data writeToFile: [self getSaveString] atomically: NO];

    [_image release];
}

1

您在实际处理图像之前就已经解锁了图像缓冲区。尝试将此行移动:

CVPixelBufferUnlockBaseAddress(videoFrame,0);

到最后


1

我正在做类似的事情,也遇到了同样的问题。我稍微修改了你的代码,并将kCGImageAlphaPremultipliedFirst更改为kCGImageAlphaNoneSkipFirst。可能是因为帧不包含alpha,即使每像素32位,这个修改对我解决了问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接