kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange 格式的CVPixelBufferRef 轉(zhuǎn)kCVPixelFormatType_32BGRA
void stillImageDataReleaseCallback(void *releaseRefCon, const void baseAddress){
free((void )baseAddress);
}
-(CGImageRef )createImageFromPixelBuffer:(CVPixelBufferRef)p {
CIImage ciImage = [CIImage imageWithCVPixelBuffer:p];
CIContext context = [CIContext contextWithOptions:@{kCIContextUseSoftwareRenderer:@(YES)}];
CGRect rect = CGRectMake(0, 0, CVPixelBufferGetWidth(p), CVPixelBufferGetHeight(p));
CGImageRef videoImage = [context createCGImage:ciImage fromRect:rect];
return videoImage;
}
// 420v 轉(zhuǎn)RGB
-
(CVPixelBufferRef )GPUImageCreateResizedSampleBuffer:(CVPixelBufferRef)cameraFrame {
// CVPixelBufferCreateWithPlanarBytes for YUV input
CGSize originalSize = CGSizeMake(CVPixelBufferGetWidth(cameraFrame), CVPixelBufferGetHeight(cameraFrame));
GLubyte *sourceImageBytes = (GLubyte *)CVPixelBufferGetBaseAddress(cameraFrame);
CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, sourceImageBytes, CVPixelBufferGetBytesPerRow(cameraFrame) * originalSize.height, NULL);
CGColorSpaceRef genericRGBColorspace = CGColorSpaceCreateDeviceRGB();
CGImageRef cgImageFromBytes = [self createImageFromPixelBuffer:cameraFrame];
GLubyte *imageData = (GLubyte *) calloc(1, (int)originalSize.width * (int)originalSize.height * 4);CGContextRef imageContext = CGBitmapContextCreate(imageData, (int)originalSize.width, (int)originalSize.height, 8, (int)originalSize.width * 4, genericRGBColorspace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(imageContext, CGRectMake(0.0, 0.0, originalSize.width, originalSize.height), cgImageFromBytes);
CGImageRelease(cgImageFromBytes);
CGContextRelease(imageContext);
CGColorSpaceRelease(genericRGBColorspace);
CGDataProviderRelease(dataProvider);CVPixelBufferRef pixel_buffer = NULL;
CVPixelBufferCreateWithBytes(kCFAllocatorDefault, originalSize.width, originalSize.height, kCVPixelFormatType_32BGRA, imageData, originalSize.width * 4, stillImageDataReleaseCallback, NULL, NULL, &pixel_buffer);
CVPixelBufferUnlockBaseAddress(cameraFrame, 0);
return pixel_buffer;
}