我试图找出一种方法来使用objective-c来计算屏幕的平均颜色。Mac Screen的平均颜色
到目前为止,我用这个代码来获得一个屏幕截图,伟大的工程:
CGImageRef image1 = CGDisplayCreateImage(kCGDirectMainDisplay);
NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:image1];
// Create an NSImage and add the bitmap rep to it...
NSImage *image = [[NSImage alloc] init];
[image addRepresentation:bitmapRep];
现在我的问题是计算该图像的平均RGB颜色。
我找到一个解决方案,但RG和B颜色分量总是计算为相同(相等):
NSInteger i = 0;
NSInteger components[3] = {0,0,0};
unsigned char *data = [bitmapRep bitmapData];
NSInteger pixels = ([bitmapRep size].width *[bitmapRep size].height);
do {
components[0] += *data++;
components[1] += *data++;
components[2] += *data++;
} while (++i < pixels);
int red = (CGFloat)components[0]/pixels;
int green = (CGFloat)components[1]/pixels;
int blue = (CGFloat)components[2]/pixels;
他们出来零?或者三个频道的价值是什么? – I82Much 2011-12-26 22:22:26
你的算法假定:a)像素i)中的每个通道适合'unsigned char',ii)以整数存储,b)通道按RGB顺序存储,不含alpha分量。你确定所有这些事情都是真的吗?你知道'bitsPerPixel'和'samplesPerPixel'的值吗? – 2011-12-26 22:37:21
它总是给我红色,绿色和蓝色值相等。例如,红色= 87绿色= 87蓝色= 87 乔希, 不,我不确定。基本上我需要一种方法来找到获得屏幕平均RGB颜色的最佳方式。 – 2011-12-27 10:33:09