如何在iPhone上读取RGB像素数据 [英] How to read RGB pixel data on iPhone

查看:302
本文介绍了如何在iPhone上读取RGB像素数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在iPhone上扫描图像并分析每个像素的RGB值,从而最终确定整个图像的平均RGB。如果有人能把我推向正确的方向,我们将不胜感激。我是图像分析的新手,不知道从哪里开始,或者iOS 5 API中是否包含这样的内容。

I am wondering how I can scan an image on the iPhone and analyze the RGB value of each pixel, thus finally determining the average RGB for the whole image. If anyone could push me in the right direction it would be greatly appreciated. I am new to image analysis and not sure where to start, or if something like this is included in the iOS 5 APIs.

推荐答案

只需粘贴它,我就会检测到触摸的颜色。

- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {

if (self.view.hidden==YES) {
    //color wheel is hidden, so don't handle  this as a color wheel event.
    [[self nextResponder] touchesEnded:touches withEvent:event];
    return;
}

UITouch* touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view]; //where image was tapped
UIColor * lastColor = [self getPixelColorAtLocation:point];
NSLog(@"color %@",lastColor);
UIImageView *lbl=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
lbl.layer.cornerRadius=50;
[imageView addSubview:lbl];
lbl.backgroundColor=lastColor;
lbl.center=CGPointMake(stillImageFilter.center.x*320, (stillImageFilter.center.y*320)-125);
NSLog(@"stillImageCenter = %f,%f",stillImageFilter.center.x,stillImageFilter.center.y);}

- (UIColor*) getPixelColorAtLocation:(CGPoint)point {
UIColor* color = nil;
CGImageRef inImage = imageView.image.CGImage;

CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];
if (cgctx == NULL) { return nil; /* error */ }

size_t w = CGImageGetWidth(inImage);
size_t h = CGImageGetHeight(inImage);
CGRect rect = {{0,0},{w,h}};


CGContextDrawImage(cgctx, rect, inImage);


unsigned char* data = CGBitmapContextGetData (cgctx);
if (data != NULL) {

    int offset = 4*((w*round(point.y))+round(point.x));
    int alpha =  data[offset];
    int red = data[offset+1];
    int green = data[offset+2];
    int blue = data[offset+3];
    NSLog(@"offset: %i colors: RGB A %i %i %i  %i",offset,red,green,blue,alpha);
    color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)];
}


CGContextRelease(cgctx);

if (data) { free(data); }

return color;

}

- (CGContextRef) createARGBBitmapContextFromImage:(CGImageRef) inImage {

CGContextRef    context = NULL;
CGColorSpaceRef colorSpace;
void *          bitmapData;
int             bitmapByteCount;
int             bitmapBytesPerRow;


size_t pixelsWide = CGImageGetWidth(inImage);
size_t pixelsHigh = CGImageGetHeight(inImage);


bitmapBytesPerRow   = (pixelsWide * 4);
bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);


colorSpace = CGColorSpaceCreateDeviceRGB();

if (colorSpace == NULL)
{
    fprintf(stderr, "Error allocating color space\n");
    return NULL;
}


bitmapData = malloc( bitmapByteCount );
if (bitmapData == NULL)
{
    fprintf (stderr, "Memory not allocated!");
    CGColorSpaceRelease( colorSpace );
    return NULL;
}


context = CGBitmapContextCreate (bitmapData,
                                 pixelsWide,
                                 pixelsHigh,
                                 8,      // bits per component
                                 bitmapBytesPerRow,
                                 colorSpace,
                                 kCGImageAlphaPremultipliedFirst);
if (context == NULL)
{
    free (bitmapData);
    fprintf (stderr, "Context not created!");
}


CGColorSpaceRelease( colorSpace );

return context;

}

这篇关于如何在iPhone上读取RGB像素数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆