2

カメラ画面上の指定した点の色値を(キャプチャせずに)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]);
4

1 に答える 1

8

これをリアルタイムで行いたいと仮定します (スクリーン キャプチャを使用するのではなく、具体的には望まないと言います):

まず、Apple がここで概説しているように、ビデオ バッファをキャプチャする必要があります。

その後、 で好きなことを行うことができますCMSampleBufferRef。Apple のサンプル アプリは をUIImage作成しますが、単純に(またはunsigned charを介し​​て) ポインターにコピーし、次のような問題のピクセルの BGRA 値を取得できます (テストされていないコード: 例は 100x,200y のピクセルの場合です)。 :CVImageBufferRefCVPixelBufferRef

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 のディスプレイに表示されるピクセルの位置が必要な場合は、これをスケーリングする必要があるかもしれません。

于 2012-09-02T17:12:54.163 に答える