CMSampleBufferRef直接转灰度图像数据

两个注意点

在初始化AVCaptureSession时设置输出图像尺寸

AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPreset640x480;

在设置AVCaptureVideoDataOutput时要注意设置YUV输出而不是RGB

 AVCaptureVideoDataOutput * dataOutput = [[AVCaptureVideoDataOutput alloc] init];
[dataOutput setVideoSettings:@{(id)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)}];

在Camera的代理方法中可以拿到CMSampleBufferRef

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
    
}
//顺时针旋转90度
//M N 分别代表宽和高
void Matrix_Rotate_90(uint8_t *src,uint8_t *dst,int M ,int N)
{
    for(int i=0;i

转灰度图像数据

CVImageBufferRef imageBufferRef=CMSampleBufferGetImageBuffer(sampleBuffer);
 
    CVPixelBufferLockBaseAddress(imageBufferRef, 0);
unsigned char *ptr_image=CVPixelBufferGetBaseAddressOfPlane(imageBufferRef, 0);

unsigned char* grayImg = (unsigned char *)malloc(sizeof(unsigned char) * 640 * 480 * 4);
    
Matrix_Rotate_90(ptr_image,grayImg,640,480);

你可能感兴趣的:(CMSampleBufferRef直接转灰度图像数据)