[英]How to get raw image data (RGB value of each pixel) from iPhone camera when using in video capture mode?
[英]How can I get RGB value of pixel at specified point on the camera screen?
我想获取相机屏幕上指定点的颜色值(不捕获),以RGB表示。
我有以下代码段,但它提供的是视图背景色的值,而不是相机屏幕上的图片。
CGPoint point=CGPointMake(100,200);
unsigned char pixel[4] = {0};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast);
CGContextTranslateCTM(context, -point.x, -point.y);
[self.view.layer renderInContext:context];
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
NSLog(@"pixel: R=%d G=%d B=%d Alpha=%d", pixel[0], pixel[1], pixel[2], pixel[3]);
假设您想实时执行此操作(而不是使用截屏,而您明确表示不希望这样做 ):
首先,您需要捕获Apple此处概述的视频缓冲区。
然后,您可以使用CMSampleBufferRef
做您喜欢的事情。 Apple的示例应用程序制作了一个UIImage
,但是您可以简单地将其复制到一个unsigned char
指针中(通过CVImageBufferRef
或CVPixelBufferRef
),然后提取所涉及像素的BGRA值,类似这样(未经测试的代码:示例适用于该像素)在100x,200y时):
int x = 100;
int y = 200;
CVPixelBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0);
tempAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
int bufferSize = bytesPerRow * height;
uint8_t *myPixelBuf = malloc(bufferSize);
memmove(myPixelBuf, tempAddress, bufferSize);
tempAddress = nil;
// remember it's BGRA data
int b = myPixelBuf[(x*4)+(y*bytesPerRow)];
int g = myPixelBuf[((x*4)+(y*bytesPerRow))+1];
int r = myPixelBuf[((x*4)+(y*bytesPerRow))+2];
free(myPixelBuf);
NSLog(@"r:%i g:%i b:%i",r,g,b);
这将获得相对于视频源本身的像素的位置,而这可能不是您想要的:如果要使像素位置与iPhone显示屏上显示的位置相同,则可能需要缩放比例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.