当前位置 : 主页 > 手机开发 > ios >

ios – 将CMSampleBufferRef转换为UIImage

来源:互联网 收集:自由互联 发布时间:2021-06-11
我用AVCaptureSession拍摄视频. 但我想将捕获的图像转换为UI Image. 我在互联网上找到了一些代码: - (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer{ NSLog(@"imageFromSampleBuffer: called"); // G
我用AVCaptureSession拍摄视频.
但我想将捕获的图像转换为UI Image.

我在互联网上找到了一些代码:

- (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);
}

但是我遇到了一些错误:

06001

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

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

}
你最好的选择是 set the capture video data output’s videoSettings到一个 specifies the pixel format you want的字典,你需要设置为RGB的一些CGBitmapContext可以处理的变体.

文档有a list of all of the pixel formats that Core Video can process.CGBitmapContext只支持其中的一小部分.您在互联网上找到的代码所期望的格式是kCVPixelFormatType_32BGRA,但这可能是针对Macs-on iOS设备编写的,kCVPixelFormatType_32ARGB(big-endian)可能更快.在设备上尝试它们,并比较帧速率.

网友评论