【问题标题】:How to convert BGRA bytes to UIImage for saving?如何将 BGRA 字节转换为 UIImage 进行保存?
【发布时间】:2017-10-06 17:47:00
【问题描述】:

我想捕获原始像素数据以使用 GPUImage 框架进行操作。我像这样捕获数据:

 CVImageBufferRef cameraFrame = CMSampleBufferGetImageBuffer(imageSampleBuffer);
     CVPixelBufferLockBaseAddress(cameraFrame, 0);
     GLubyte *rawImageBytes = CVPixelBufferGetBaseAddress(cameraFrame);
     size_t bytesPerRow = CVPixelBufferGetBytesPerRow(cameraFrame);
     NSData *dataForRawBytes = [NSData dataWithBytes:rawImageBytes length:bytesPerRow * CVPixelBufferGetHeight(cameraFrame)];

     //raw values
     UInt32 *values = [dataForRawBytes bytes];//, cnt = [dataForRawBytes length]/sizeof(int);

     //test out dropbox upload here
     [self uploadDropbox:dataForRawBytes];
     //end of dropbox upload


     // Do whatever with your bytes
     //         [self processImages:dataForRawBytes];

     CVPixelBufferUnlockBaseAddress(cameraFrame, 0);     }];

我正在使用以下相机设置:

 NSDictionary *settings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey,[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA], (id)kCVPixelBufferPixelFormatTypeKey, nil];

出于测试目的,我想将捕获的图像保存到保管箱,为此我需要将其保存到 tmp 目录,我将如何保存 dataForRawBytes? 任何帮助将不胜感激!

【问题讨论】:

  • 'UIImage *image = [UIImage imageWithData:[self imageToBuffer:sampleBuffer]];'我正在尝试这个,但它给了我零,我不知道为什么?

标签: ios iphone uiimage avcapturesession


【解决方案1】:

所以我能够弄清楚如何从原始数据中获取 UIImage,这是我修改后的代码:

CVImageBufferRef cameraFrame = CMSampleBufferGetImageBuffer(imageSampleBuffer);
     CVPixelBufferLockBaseAddress(cameraFrame, 0);
     Byte *rawImageBytes = CVPixelBufferGetBaseAddress(cameraFrame);
     size_t bytesPerRow = CVPixelBufferGetBytesPerRow(cameraFrame);
     size_t width = CVPixelBufferGetWidth(cameraFrame);
     size_t height = CVPixelBufferGetHeight(cameraFrame);
     NSData *dataForRawBytes = [NSData dataWithBytes:rawImageBytes length:bytesPerRow * CVPixelBufferGetHeight(cameraFrame)];
     // Do whatever with your bytes

     // create suitable color space
     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

     //Create suitable context (suitable for camera output setting kCVPixelFormatType_32BGRA)
     CGContextRef newContext = CGBitmapContextCreate(rawImageBytes, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);

     CVPixelBufferUnlockBaseAddress(cameraFrame, 0);

     // release color space
     CGColorSpaceRelease(colorSpace);

     //Create a CGImageRef from the CVImageBufferRef
     CGImageRef newImage = CGBitmapContextCreateImage(newContext);
     UIImage *FinalImage = [[UIImage alloc] initWithCGImage:newImage];
     //is the image captured, now we can test saving it.

我需要创建诸如颜色空间之类的属性并生成一个 CDContexyRef 并使用它来最终获得一个 UIImage,并且在调试时我可以正确地看到我捕获的图像。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多