首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将CMSampleBufferRef转换为UIImage

将CMSampleBufferRef转换为UIImage
EN

Stack Overflow用户
提问于 2013-01-17 16:45:08
回答 3查看 11.4K关注 0票数 3

我在用AVCaptureSession捕捉视频。但是,我想将捕获的图像转换为UIImage。

我在网上发现了一些代码:

代码语言:javascript
运行
复制
- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
{

    NSLog(@"imageFromSampleBuffer: called");
    // 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 number of bytes per row for the pixel buffer
    void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);

    // 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);

    // Create an image object from the Quartz image
    UIImage *image = [UIImage imageWithCGImage:quartzImage];

    // Release the Quartz image
    CGImageRelease(quartzImage);

    return (image);
}

但我有一些错误:

1月17日17:39:25 iPhone4-deXXX ThinkOutsideTheBox2363 : CGBitmapContextCreate:无效数据字节/行:对于8个整数位/组件,3个组件,kCGImageAlphaPremultipliedFirst,应该至少为2560。1月17日17:39:25 iPhone-4-de-XXX ThinkOutsideTheBox2363 : CGBitmapContextCreateImage:无效上下文0x0 2013-01- 17 :39:25.896 ThinkOutsideTheBox2363 2363:907图片 Jan 17 :39:39:25 iPhone-4-4XXX ThinkOutsideTheBox2363 : CGContextDrawImage:无效上下文0x0 17:39:25 iPhone-4-deXXX ThinkOutsideTheBox2363: CGBitmapContextGetData:无效上下文0x0

编辑:我还使用UIImage获取rgb颜色:

代码语言:javascript
运行
复制
-(void) captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection
{

    UIImage* image = [self imageFromSampleBuffer:sampleBuffer];
    unsigned char* pixels = [image rgbaPixels];
    double totalLuminance = 0.0;
    for(int p=0;p<image.size.width*image.size.height*4;p+=4)
    {
        totalLuminance += pixels[p]*0.299 + pixels[p+1]*0.587 + pixels[p+2]*0.114;
    }
    totalLuminance /= (image.size.width*image.size.height);
    totalLuminance /= 255.0;
    NSLog(@"totalLuminance %f",totalLuminance);

}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-01-18 17:00:52

您最好的选择是将videoSettings转到指定所需的像素格式。的字典中,您需要将其设置为CGBitmapContext可以处理的RGB上的一些变体。

文档有核心视频可以处理的所有像素格式的列表。CGBitmapContext只支持其中的一小部分。您在互联网上找到的代码所期望的格式是kCVPixelFormatType_32BGRA,但这可能是为iOS设备上的Mac编写的,kCVPixelFormatType_32ARGB (大端)可能更快。在设备上测试它们,并比较帧速率。

票数 7
EN

Stack Overflow用户

发布于 2020-01-15 09:32:40

你可以试试这段代码。

代码语言:javascript
运行
复制
-(UIImage *) screenshotOfVideoStream:(CMSampleBufferRef)samImageBuff
  {
       CVImageBufferRef imageBuffer =  
         CMSampleBufferGetImageBuffer(samImageBuff);
       CIImage *ciImage = [CIImage imageWithCVPixelBuffer:imageBuffer];
       CIContext *temporaryContext = [CIContext contextWithOptions:nil];
       CGImageRef videoImage = [temporaryContext
                         createCGImage:ciImage
                         fromRect:CGRectMake(0, 0,
                         CVPixelBufferGetWidth(imageBuffer),
                         CVPixelBufferGetHeight(imageBuffer))];

       UIImage *image = [[UIImage alloc] initWithCGImage:videoImage];
       CGImageRelease(videoImage);
      return image;
}

对我来说很管用。

票数 3
EN

Stack Overflow用户

发布于 2021-04-25 08:32:18

如果有人像我一样期待jpeg映像,苹果公司提供了简单的API:

代码语言:javascript
运行
复制
[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:photoSampleBuffer];

以及:

代码语言:javascript
运行
复制
[AVCapturePhotoOutput JPEGPhotoDataRepresentationForJPEGSampleBuffer:photoSampleBuffer previewPhotoSampleBuffer:previewPhotoSampleBuffer]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14383932

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档