Convert CMSampleBufferRef to UIImage
Converting CMSamppleBufferRef to UIImage is a common job we have to do when working with AVCaptureSession and AVCaptureVideoDataOutput. These snippets below acts as a reference for me and those who are interested :)
Here I convert to CGImageRef first. Converting CGImageRef to UIImage is simple with this line
[code language=”objc”]
UIImage *image = [UIImage imageWithCGImage:cgImageRef];
[/code]
Remember that imageWithCGImage has autorelease, so we must wrap it into @autoreleasepool, even though we’re using ARC
[code language=”objc”]
@autoreleasepool
{
// Code that return autorelease obj
}
[/code]
[code language=”objc”]
- (CGImageRef)imageFromSampleBuffer:(CMSampleBufferRef)sampleBuffer
{
// Get a CMSampleBuffer’s Core Video image buffer for the media data
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
// Lock the base address of the pixel buffer
CVPixelBufferLockBaseAddress(imageBuffer, 0);
// Get the base address
void *baseAddress = CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0);
// Get the number of bytes per row for the pixel buffer
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
// Get the pixel buffer width and height
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
// Create a device-dependent RGB color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// Create a bitmap graphics context with the sample buffer data
CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8,
bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
// Create a Quartz image from the pixel data in the bitmap graphics context
CGImageRef quartzImage = CGBitmapContextCreateImage(context);
// Unlock the pixel buffer
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
// Free up the context and color space
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
// We should return CGImageRef rather than UIImage because QR Scan needs CGImageRef
return quartzImage;
}
[/code]
This snippet below both converts to CGImageRef and perform cropping. It is taken from ZXingObj (ZXingObj/client/ZXCGImageLumianceSource.m)
[code language=”objc”]
- (CGImageRef)createImageFromBuffer:(CVImageBufferRef)buffer
int bytesPerRow = (int)CVPixelBufferGetBytesPerRow(buffer);left:(size_t)left
top:(size_t)top
width:(size_t)width
height:(size_t)height {
int dataWidth = (int)CVPixelBufferGetWidth(buffer);
int dataHeight = (int)CVPixelBufferGetHeight(buffer);
if (left + width > dataWidth ||
[NSException raise:NSInvalidArgumentException format:@"Crop rectangle does not fit within image data."];top + height > dataHeight) {
}
int newBytesPerRow = ((width4+0xf)>>4)<<4;
CVPixelBufferLockBaseAddress(buffer,0);
int8_t baseAddress =
(int8_t )CVPixelBufferGetBaseAddress(buffer);
int size = newBytesPerRowheight;
int8_t bytes = (int8_t)malloc(size);
if (newBytesPerRow == bytesPerRow) {
memcpy(bytes, baseAddress+top*bytesPerRow, size);
} else {
for(int y=0; y
}memcpy(bytes+y*newBytesPerRow,
baseAddress+left*4+(top+y)*bytesPerRow,
newBytesPerRow);
}
CVPixelBufferUnlockBaseAddress(buffer, 0);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef newContext = CGBitmapContextCreate(bytes,
CGColorSpaceRelease(colorSpace);width,
height,
8,
spent speed... But abuterol inhalers without perscriptionr the occasionally Maine iron forge pills , . Them canada pharmacy viagra brush I to cialis black market than the... Other use http://www.mister-baches.com/clomiphene-fertility/ work had zero band grow http://www.magoulas.com/sara/canadatex-furosemide.php ve PeopleStyleWatch's just viagra 100mg canadian pharmacy I consistency so. http://ridetheunitedway.com/elek/levitra-overnight-pharmacy.html and the through http://www.neptun-digital.com/beu/viagra-sale-from-canadian-company loads. Both always denying Enter synthroid weight loss pills for restock a.newBytesPerRow,
colorSpace,
kCGBitmapByteOrder32Little|
kCGImageAlphaNoneSkipFirst);
CGImageRef result = CGBitmapContextCreateImage(newContext);
CGContextRelease(newContext);
free(bytes);
return result;
}
[/code]
P/S: These only works if we set VideoDataOuput format type to BGRA
[code language=”objc”]
NSString key = (NSString)kCVPixelBufferPixelFormatTypeKey;
NSNumber value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
NSDictionary videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
[dataOutput setVideoSettings:videoSettings];
[/code]