取出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);
}
}