• iOS UIImage和CVPixelBuffer互相转换


    UIImage转CVPixelBuffer

    1. + (CVPixelBufferRef)pixelBufferFromCGImage:(CGImageRef)image {
    2. NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
    3. [NSNumber numberWithBool:true], kCVPixelBufferCGImageCompatibilityKey,
    4. [NSNumber numberWithBool:true], kCVPixelBufferCGBitmapContextCompatibilityKey, nil];
    5. CVPixelBufferRef pxbuffer = NULL;
    6. size_t width = CGImageGetWidth(image);
    7. size_t height = CGImageGetHeight(image);
    8. CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_32BGRA, (__bridge CFDictionaryRef)options, &pxbuffer);
    9. if (status != kCVReturnSuccess) {
    10. NSLog(@"CVPixelBufferCreate Fail");
    11. }
    12. CVPixelBufferLockBaseAddress(pxbuffer, 0);
    13. void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
    14. CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
    15. CGContextRef context = CGBitmapContextCreate(pxdata, width, height, 8, CVPixelBufferGetBytesPerRow(pxbuffer), rgbColorSpace, (CGBitmapInfo)kCGImageAlphaNoneSkipFirst);
    16. CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
    17. CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
    18. CGColorSpaceRelease(rgbColorSpace);
    19. CGContextRelease(context);
    20. return pxbuffer;
    21. }

    CVPixelBuffer转UIImage

    1. + (UIImage *)imageFromPixelBuffer:(CVPixelBufferRef)pixelBuffer {
    2. CVImageBufferRef imageBuffer = pixelBuffer;
    3. CVPixelBufferLockBaseAddress(imageBuffer, 0);
    4. void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
    5. size_t width = CVPixelBufferGetWidth(imageBuffer);
    6. size_t height = CVPixelBufferGetHeight(imageBuffer);
    7. size_t bufferSize = CVPixelBufferGetDataSize(imageBuffer);
    8. size_t bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer, 0);
    9. CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
    10. CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, baseAddress, bufferSize, NULL);
    11. CGImageRef cgImage = CGImageCreate(width, height, 8, 32, bytesPerRow, rgbColorSpace, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrderDefault, provider, NULL, true, kCGRenderingIntentDefault);
    12. UIImage *image = [UIImage imageWithCGImage:cgImage];
    13. CGImageRelease(cgImage);
    14. CGDataProviderRelease(provider);
    15. CGColorSpaceRelease(rgbColorSpace);
    16. CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
    17. return image;
    18. }

    CMSampleBuffer转UIImage

    1. + (UIImage *)imageFromSampleBuffer:(CMSampleBufferRef)sampleBufferRef {
    2. CVImageBufferRef pxbuffer = CMSampleBufferGetImageBuffer(sampleBufferRef);
    3. CIImage *ciImage = [CIImage imageWithCVImageBuffer:pxbuffer];
    4. CIContext *ciContext = [[CIContext alloc] init];
    5. CGImage *cgImage = [ciContext createCGImage:ciImage fromRect:ciImage.extent];
    6. UIImage *image = [UIImage imageWithCGImage:cgImage];
    7. return image;
    8. }

  • 相关阅读:
    已解决module ‘keras.preprocessing.image‘ has no attribute ‘load_img‘
    程序员也许都缺一个“二舅”精神
    ue5 右击.uproject generator vs project file 错误
    每日三题 8.29
    C Primer Plus(6) 中文版 第4章 字符串和格式化输入/输出 4.3 常量和C预处理器
    面试题54:浏览器/HTTP缓存机制
    关于并发和并行,Go和Erlang之父都弄错了?
    AI 游戏工具收集
    集合框架----源码解读HashMap篇(一)
    浅谈无脚本自动化测试
  • 原文地址:https://blog.csdn.net/watson2017/article/details/133786776