convert image type

来源:互联网 发布:如何下载数据精灵 编辑:程序博客网 时间:2024/05/19 13:29

http://bezstellar.blog.163.com/blog/static/174868780201123035346108/


convert image type  

2011-03-30 15:53:46|  分类: iOS|字号 订阅

CGImageRef => CVPixelBufferRef
- (CVPixelBufferRef) pixelBufferFromCGImage: (CGImageRef) image{    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:        [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,        [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,        nil];    CVPixelBufferRef pxbuffer = NULL;    CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, frameSize.width,        frameSize.height, kCVPixelFormatType_32ARGB, (CFDictionaryRef) options,         &pxbuffer);    NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);    CVPixelBufferLockBaseAddress(pxbuffer, 0);    void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);    NSParameterAssert(pxdata != NULL);    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();    CGContextRef context = CGBitmapContextCreate(pxdata, frameSize.width,        frameSize.height, 8, 4*frameSize.width, rgbColorSpace,         kCGImageAlphaNoneSkipFirst);    NSParameterAssert(context);    CGContextConcatCTM(context, frameTransform);    CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image),         CGImageGetHeight(image)), image);    CGColorSpaceRelease(rgbColorSpace);    CGContextRelease(context);    CVPixelBufferUnlockBaseAddress(pxbuffer, 0);    return pxbuffer;}
CMSampleBufferRef => CVImageBufferRef [CMSampleBufferGetImageBuffer]
CVPixelBuffer(CVImageBuffer) => CMSampleBuffer
CVImageBufferRef cvimgRef = CMSampleBufferGetImageBuffer(sampleBuffer);CVPixelBufferLockBaseAddress(cvimgRef,0);uint8_t *buf=(uint8_t *)CVPixelBufferGetBaseAddress(cvimgRef);int width = 480;int height = 360;int bitmapBytesPerRow   = width*4;int bitmapByteCount     = bitmapBytesPerRow*height;CVPixelBufferRef pixelBufRef = NULL;CMSampleBufferRef newSampleBuffer = NULL;CMSampleTimingInfo timimgInfo = kCMTimingInfoInvalid;CMSampleBufferGetSampleTimingInfo(sampleBuffer, 0, &timimgInfo);OSStatus result = 0;OSType pixFmt = CVPixelBufferGetPixelFormatType(cvimgRef);CVPixelBufferCreateWithBytes(kCFAllocatorDefault, width, height, pixFmt, buf, bitmapBytesPerRow, NULL, NULL, NULL, &pixelBufRef);CMVideoFormatDescriptionRef videoInfo = NULL;result = CMVideoFormatDescriptionCreateForImageBuffer(NULL, pixelBufRef, &videoInfo);CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pixelBufRef, true, NULL, NULL, videoInfo, &timimgInfo, &newSampleBuffer);