CVPixelBufferRef取出RDB和YUV

取出YUV

-(int) getImageData:(const CVImageBufferRef&) imageBuffer senddata:(unsigned char)data
{
int w,h,linesizey,linesizeuv;
unsigned char
srcy=NULL;
unsigned char* srcu=NULL;
unsigned char* srcv=NULL;

CVPixelBufferLockBaseAddress(imageBuffer, 0);
int count=CVPixelBufferGetPlaneCount(imageBuffer);
//printf("CVPixelBufferGetPlaneCount=%d\n",count);

if (CVPixelBufferIsPlanar(imageBuffer)) {
    w = CVPixelBufferGetWidth(imageBuffer);
    h = CVPixelBufferGetHeight(imageBuffer);
    
    // printf("w: %d,h: %d.\n",w,h);
    
    linesizey = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer, 0);
    linesizeuv = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer, 1);
    // printf("CVPixelBufferGetBytesPerRowOfPlane is ok.\n");
    srcy = (unsigned char*) CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0);
    srcu = (unsigned char*) CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 1);
    srcv = (unsigned char*) CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 2);
    
    memcpy(data,srcy,720*1280);
    memcpy(data+720*1280,srcu,720*1280/4);
    memcpy(data+720*1280*5/4,srcv,720*1280/4);
    
    // printf("CVPixelBufferGetBaseAddressOfPlane is ok.\n");
}
else
{
    printf("CVPixelBufferIsPlanar error\n");
    return -1;
}
return 0;

}

取出RGB

  • (void)captureOutput:(AVCaptureOutput *)captureOutput
    didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
    fromConnection:(AVCaptureConnection *)connection
    {

    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CVPixelBufferLockBaseAddress(imageBuffer,0);

    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);
    uint8_t src_buff = (uint8_t)CVPixelBufferGetBaseAddress(imageBuffer);

    CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
    RGBPixel *pixelData = (RGBPixel *)src_buff;

    int len = bytesPerRow * height;
    for(int i=0; i<len; i+=4){

      RGBPixel pixel = pixelData[i/4];
      
      int a = 0;
      int r = pixel.red;
      int g = pixel.green;
      int b = pixel.blue;
    
      NSLog(@"first values = r:%d g:%d b:%d", r, g, b);
      
      a = src_buff[i+3];
      r = src_buff[i+2];
      g = src_buff[i+1];
      b = src_buff[i];
    
      NSLog(@"second values = r:%d g:%d b:%d", r, g, b);
    

    }

}

http://www.bubuko.com/infodetail-526746.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容