captureStillImageAsynchronouslyFromConnection without JPG intermediary

我与影子孤独终老i 提交于 2019-12-19 03:07:23

问题


I'm trying to get as good an image as possible from the camera, but can only find examples that captureStillImageAsynchronouslyFromConnection and then go straight to:

NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];    
UIImage *image = [[UIImage alloc] initWithData:imageData];

JPEG being lossy and all, is there any way to get the data as PNG, or even just RGBA (BGRA, what-have-you?). AVCaptureStillImageOutput doesn't seem to have any other NSData* methods....

Actually looking at the CMSampleBufferRef, it seems like it's already locked as JPEG ~

formatDescription = <CMVideoFormatDescription 0xfe5e1f0 [0x3e5ac650]> {
mediaType:'vide' 
mediaSubType:'jpeg' 
mediaSpecific: {
    codecType: 'jpeg'       dimensions: 2592 x 1936 
} 
extensions: {(null)}
}

Is there some other way to take a full-res picture and get the raw data?


回答1:


You'll need to set the outputSettings with a different pixel format. If you want 32-bit BGRA, for example, you can set:

NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA], (id)kCVPixelBufferPixelFormatTypeKey, nil];

From https://developer.apple.com/library/mac/#documentation/AVFoundation/Reference/AVCaptureStillImageOutput_Class/Reference/Reference.html, the "recommended" pixel formats are:

  • kCMVideoCodecType_JPEG
  • kCVPixelFormatType_420YpCbCr8BiPlanarFullRange
  • kCVPixelFormatType_32BGRA

Of course, if you're not using JPEG output, you can't use jpegStillImageNSDataRepresentation:, but there's an example here: how to convert a CVImageBufferRef to UIImage




回答2:


Just change the Output settings of your connection:

outputSettings The compression settings for the output.

@property(nonatomic, copy) NSDictionary *outputSettings

You can retreive an NSDictionary of the supported values with

availableImageDataCodecTypes The supported image codec formats that can be specified in outputSettings. (read-only)

@property(nonatomic, readonly) NSArray *availableImageDataCodecTypes


来源:https://stackoverflow.com/questions/11571242/capturestillimageasynchronouslyfromconnection-without-jpg-intermediary

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