iOS相机CVPixelBuffer格式详解:YUV、RGB获取byte

1.iOS相机支持的CVPixelBuffer格式:

kCVPixelFormatType_32BGRA         = 'BGRA',     /* 32 bit BGRA */

kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange = '420v', /* Bi-Planar Component Y'CbCr 8-bit 4:2:0, video-range (luma=[16,235] chroma=[16,240]).  baseAddr points to a big-endian CVPlanarPixelBufferInfo_YCbCrBiPlanar struct */

kCVPixelFormatType_420YpCbCr8BiPlanarFullRange  = '420f', /* Bi-Planar Component Y'CbCr 8-bit 4:2:0, full-range (luma=[0,255] chroma=[1,255]).  baseAddr points to a big-endian CVPlanarPixelBufferInfo_YCbCrBiPlanar struct */ 

AVCaptureVideoDataOutputSampleBufferDelegate

2.如何设置相机的输出格式

2.1.需要先查询相机实际上支持哪些格式:
@interface AVCaptureVideoDataOutput
NSArray<NSNumber *> *availableVideoCVPixelFormatTypes
2.2.给相机设置格式
//'420f'
[videoOutput setVideoSettings:@{(id)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)}];

//'420v'
[videoOutput setVideoSettings:@{(id)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange)}];

//'BGRA'
[videoOutput setVideoSettings:@{(id)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA)}];

3.如何获取到CVPixelBuffer

  • 通过AVCaptureVideoDataOutput类的AVCaptureVideoDataOutputSampleBufferDelegate获取到相机每一帧的预览原始画面:
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
	   fromConnection:(AVCaptureConnection *)connection;
  • 如果使用了GPUImage的相机📷,那么可以直接使用GPUImageGPUImageVideoCameraDelegate也可以获取到相机每一帧原始画面
- (void)willOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer;

4.如何获取BGRA格式的byte

- (void)willOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer {
    CVImageBufferRef cvImageBufferRef = CMSampleBufferGetImageBuffer(sampleBuffer);
    
    CVPixelBufferLockBaseAddress(cvImageBufferRef, 0);
    
    //获取BGRA的信息和byte地址
    size_t width = CVPixelBufferGetWidth(cvImageBufferRef);
    size_t height = CVPixelBufferGetHeight(cvImageBufferRef);
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(cvImageBufferRef);
    unsigned char *pImageData = (unsigned char *)CVPixelBufferGetBaseAddress(cvImageBufferRef);
    
    CVPixelBufferUnlockBaseAddress(cvImageBufferRef, 0);
}

5.如何获取YUV格式的byte

- (void)willOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer {
    CVImageBufferRef cvImageBufferRef = CMSampleBufferGetImageBuffer(sampleBuffer);
    
    CVPixelBufferLockBaseAddress(cvImageBufferRef, 0);
    
    //获取Y的信息和byte地址
    size_t widthY = CVPixelBufferGetWidthOfPlane(cvImageBufferRef, 0);
    size_t heightY = CVPixelBufferGetHeightOfPlane(cvImageBufferRef, 0);
    size_t bytesPerRowY = CVPixelBufferGetBytesPerRowOfPlane(cvImageBufferRef, 0);
    unsigned char *pImageDataY = (unsigned char *)CVPixelBufferGetBaseAddressOfPlane(cvImageBufferRef, 0);

    //获取UV的信息和byte地址
    size_t widthUV = CVPixelBufferGetWidthOfPlane(cvImageBufferRef, 1);
    size_t heightUV = CVPixelBufferGetHeightOfPlane(cvImageBufferRef, 1);
    size_t bytesPerRowUV = CVPixelBufferGetBytesPerRowOfPlane(cvImageBufferRef, 1);
    unsigned char *pImageDataUV = (unsigned char *)CVPixelBufferGetBaseAddressOfPlane(cvImageBufferRef, 1);
    
    CVPixelBufferUnlockBaseAddress(cvImageBufferRef, 0);
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值