CVPixelbuffer 取 BGRA

首要要确定CVPixelbuffer的类型, 有 YUV/RGBA 的等等

从摄像头取BGRA式例代码:

- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
       fromConnection:(AVCaptureConnection *)connection
{
    //BGRA格式的cvpixelbuffer中获取bgra
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CVPixelBufferLockBaseAddress(imageBuffer,0);
    uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);
    
    size_t bufferSize = bytesPerRow * height;
    uint8_t *myPixelBuf = (uint8_t *)malloc(bufferSize);
    memmove(myPixelBuf, baseAddress, bufferSize);
    baseAddress = nil;
    IFrameUploaded cb = callback;
    [imageEngine pushNewFrame:myPixelBuf dataType:IMAGE_BGRA8888 withWidth:(int)width withHeight:(int)height withSize:(int)bufferSize frameUploadedNotify:cb withTimestamp:0 ctx:myPixelBuf];
}

你可能感兴趣的:(CVPixelbuffer 取 BGRA)