2011-01-06 4 views
13

Par exemple, supposons que je souhaite détecter la couleur du pixel avec les coordonnées d'écran (100, 200). Y a-t-il un moyen de faire cela?iOS - détecter la couleur d'un pixel?

EDIT - Je ne suis pas préoccupé par les problèmes d'affichage de la rétine pour l'instant.

Répondre

6

Cela peut ne pas être la route la plus directe, mais vous pouvez:

  1. Utilisez UIGraphicsBeginImageContextWithOptions pour saisir l'écran (voir l'Apple Q & A QA1703 - "Screen Capture in UIKit Applications").

  2. Ensuite, utilisez CGImageCreateWithImageInRect pour récupérer la partie de l'image résultante dont vous avez besoin.

  3. Enfin analyser l'image résultante. Il se complique à ce moment, mais heureusement, il y a une question existante qui devrait vous montrer le chemin: How to get the RGB values for a pixel on an image on the iphone

Sinon, il y a l'article de blog suivant qui a un code d'accompagnement: What Color is My Pixel? Image based color picker on iPhone

+0

Laide mais devrait fonctionner –

+0

@WilliamJockusch J'ai implémenté ceci mais je veux détecter la couleur (de seulement 1 pixel sur l'écran) régulièrement (toutes les 0.04 secondes en utilisant une minuterie) créant une capture d'écran de tout l'écran lag mon application ... vous n'avez pas trouvé une façon plus efficace de mémoire de le faire avez-vous? Et des excuses pour avoir commenté un tel vieux fil! ;) – simonthumper

+0

@simonthumper Malheureusement non. Si vous n'avez pas besoin de saisir l'écran, vous pouvez probablement accélérer les choses. (Cela dépend vraiment de la source des données d'image.) Cela dit, je serais tenté de créer une nouvelle question (bien que je mentionne que vous avez lu/absorbé celui-ci) si le problème que vous essayez de résoudre est suffisamment différent. –

5

Voici comment faire il

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); 
+0

Pourquoi multipliez-vous l'index avec 4? Parce que c'est 4 octets par pixel? – c0d3Junk13

+0

Oui! Red Green Blue Alpha –

+0

Comment obtenir la couleur rgb de uiview? –

0

Try This One Où "self.m_imgvwSource" est le UIView/UIImageView selon vos besoins

- (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; 
}