美文网首页
RGB2PixelBuffer

RGB2PixelBuffer

作者: 陆号 | 来源:发表于2018-07-11 10:47 被阅读12次

- (CVPixelBufferRef)RGB2PixelBuffer{
 
    int width = buf_Width;
    int height = buf_Height;
    if (!resultBuffer) {
        NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
                                 [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
                                 nil];
        CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault,
                                              width,
                                              height,
                                              kCVPixelFormatType_32ARGB,
                                              (__bridge CFDictionaryRef) options,
                                              &resultBuffer);
    }
    int bytesPerPixel = 4;
    CGContextRef context = CGBitmapContextCreate(rgbBuffer, width, height, 8, width * bytesPerPixel, colorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaNoneSkipFirst);
    CGImageRef quartzImage = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    
    //转为PixelBuffer
    CVPixelBufferLockBaseAddress(resultBuffer, 0);
    void *pxdata = CVPixelBufferGetBaseAddress(resultBuffer);
    context = CGBitmapContextCreate(pxdata,
                                                 width,
                                                 height,
                                                 8,
                                                 CVPixelBufferGetBytesPerRow(resultBuffer),
                                                 colorSpace,
                                                 (CGBitmapInfo)kCGImageAlphaNoneSkipFirst);
    CGContextConcatCTM(context, CGAffineTransformIdentity);
    CGContextDrawImage(context, CGRectMake(0,0,width,height),quartzImage);
    
    CGContextRelease(context);
    CGImageRelease(quartzImage);
    CVPixelBufferUnlockBaseAddress(resultBuffer, 0);
    return resultBuffer;
}

相关文章

网友评论

      本文标题:RGB2PixelBuffer

      本文链接:https://www.haomeiwen.com/subject/lkzcpftx.html