iOS - 检测像素的颜色?

时间:2022-09-10 19:02:30

For example, suppose I want to detect the color of the pixel with screen coordinates (100, 200). Is there a way to do this?

例如,假设我想用屏幕坐标(100,200)检测像素的颜色。有没有办法做到这一点?

EDIT -- I'm not worried about retina display issues for now.

编辑 - 我现在不担心视网膜显示问题。

3 个解决方案

#1


7  

This may not be the most direct route, but you could:

这可能不是最直接的路线,但您可以:

  1. Use UIGraphicsBeginImageContextWithOptions to grab the screen (see the Apple Q&A QA1703 - "Screen Capture in UIKit Applications").

    使用UIGraphicsBeginImageContextWithOptions抓取屏幕(参见Apple Q&A QA1703 - “UIKit应用程序中的屏幕捕获”)。

  2. Then use CGImageCreateWithImageInRect to grab the portion of the resultant image you require.

    然后使用CGImageCreateWithImageInRect来获取所需结果图像的一部分。

  3. Finally analyse the resultant image. It gets complicated at this point, but thankfully there's an existing question that should show you the way: How to get the RGB values for a pixel on an image on the iphone

    最后分析得到的图像。此时它变得复杂,但幸运的是,现有的问题应该向您展示:如何获取iphone上图像上像素的RGB值

Alternatively, there's the following blog article that has accompanying code: What Color is My Pixel? Image based color picker on iPhone

另外,还有以下博客文章附带代码:什么颜色是我的像素? iPhone上基于图像的颜色选择器

#2


5  

Here is how to do it

这是怎么做的

CGContextRef ctx;
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                             bitsPerComponent, bytesPerRow, colorSpace,
                                             kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);

//GET PIXEL FROM POINT
int index = 4*((width*round(yCoor))+round(xCoor));

int R = rawData[index];
int G = rawData[index+1];
int B = rawData[index+2];

NSLog(@"%d   %d   %d", R, G, B);

//IF YOU WANT TO ALTER THE PIXELS

int byteIndex = 0;

for(int ii = 0 ; ii < width * height ; ++ii)
{ 
    rawData[byteIndex] = (char)(newPixelValue);
    rawData[byteIndex+1] = (char)(newPixelValue);
    rawData[byteIndex+2] = (char)(newPixelValue);

    byteIndex += 4;
}


ctx = CGBitmapContextCreate(rawData,
                            CGImageGetWidth( imageRef ),
                            CGImageGetHeight( imageRef ),
                            8,
                            CGImageGetBytesPerRow( imageRef ),
                            CGImageGetColorSpace( imageRef ),
                            kCGImageAlphaPremultipliedLast ); 

imageRef = CGBitmapContextCreateImage(ctx);
UIImage* rawImage = [UIImage imageWithCGImage:imageRef];  

CGContextRelease(ctx);  

image = rawImage;  

free(rawData);

#3


0  

Try This One Where "self.m_imgvwSource" is the uiview/uiimageview as per your need

试试这个“self.m_imgvwSource”是uiview / uiimageview根据你的需要

- (UIColor *) GetCurrentPixelColorAtPoint:(CGPoint)point
{
    unsigned char pixel[4] = {0};

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast);

    CGContextTranslateCTM(context, -point.x, -point.y);

    [self.m_imgvwSource.layer renderInContext:context];

    CGContextRelease(context);

    CGColorSpaceRelease(colorSpace);

    NSLog(@"pixel: %d %d %d %d", pixel[0], pixel[1], pixel[2], pixel[3]);

    UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 green:pixel[1]/255.0 blue:pixel[2]/255.0 alpha:pixel[3]/255.0];

    return color;
}

#1


7  

This may not be the most direct route, but you could:

这可能不是最直接的路线,但您可以:

  1. Use UIGraphicsBeginImageContextWithOptions to grab the screen (see the Apple Q&A QA1703 - "Screen Capture in UIKit Applications").

    使用UIGraphicsBeginImageContextWithOptions抓取屏幕(参见Apple Q&A QA1703 - “UIKit应用程序中的屏幕捕获”)。

  2. Then use CGImageCreateWithImageInRect to grab the portion of the resultant image you require.

    然后使用CGImageCreateWithImageInRect来获取所需结果图像的一部分。

  3. Finally analyse the resultant image. It gets complicated at this point, but thankfully there's an existing question that should show you the way: How to get the RGB values for a pixel on an image on the iphone

    最后分析得到的图像。此时它变得复杂,但幸运的是,现有的问题应该向您展示:如何获取iphone上图像上像素的RGB值

Alternatively, there's the following blog article that has accompanying code: What Color is My Pixel? Image based color picker on iPhone

另外,还有以下博客文章附带代码:什么颜色是我的像素? iPhone上基于图像的颜色选择器

#2


5  

Here is how to do it

这是怎么做的

CGContextRef ctx;
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                             bitsPerComponent, bytesPerRow, colorSpace,
                                             kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);

//GET PIXEL FROM POINT
int index = 4*((width*round(yCoor))+round(xCoor));

int R = rawData[index];
int G = rawData[index+1];
int B = rawData[index+2];

NSLog(@"%d   %d   %d", R, G, B);

//IF YOU WANT TO ALTER THE PIXELS

int byteIndex = 0;

for(int ii = 0 ; ii < width * height ; ++ii)
{ 
    rawData[byteIndex] = (char)(newPixelValue);
    rawData[byteIndex+1] = (char)(newPixelValue);
    rawData[byteIndex+2] = (char)(newPixelValue);

    byteIndex += 4;
}


ctx = CGBitmapContextCreate(rawData,
                            CGImageGetWidth( imageRef ),
                            CGImageGetHeight( imageRef ),
                            8,
                            CGImageGetBytesPerRow( imageRef ),
                            CGImageGetColorSpace( imageRef ),
                            kCGImageAlphaPremultipliedLast ); 

imageRef = CGBitmapContextCreateImage(ctx);
UIImage* rawImage = [UIImage imageWithCGImage:imageRef];  

CGContextRelease(ctx);  

image = rawImage;  

free(rawData);

#3


0  

Try This One Where "self.m_imgvwSource" is the uiview/uiimageview as per your need

试试这个“self.m_imgvwSource”是uiview / uiimageview根据你的需要

- (UIColor *) GetCurrentPixelColorAtPoint:(CGPoint)point
{
    unsigned char pixel[4] = {0};

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast);

    CGContextTranslateCTM(context, -point.x, -point.y);

    [self.m_imgvwSource.layer renderInContext:context];

    CGContextRelease(context);

    CGColorSpaceRelease(colorSpace);

    NSLog(@"pixel: %d %d %d %d", pixel[0], pixel[1], pixel[2], pixel[3]);

    UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 green:pixel[1]/255.0 blue:pixel[2]/255.0 alpha:pixel[3]/255.0];

    return color;
}