cocoa touch - Realtime access to iPhone's camera images -
i'm trying read (average) rgb-value of center pixel(s) of iphone camera. should happen in realtime. therefore open uiimagepickercontroller, use timer take picture every x seconds. processing picture made in separate thread such not block app while computing rgb-value. tried several ways access rgb/pixel values of taken image, have problem slow , cause camera view lag. tried following:
- (uicolor *)getaveragecolorofimage:(uiimage*)image { int pixelcount = kdetectorsize * kdetectorsize; cgcolorspaceref colorspace = cgcolorspacecreatedevicergb(); nsuinteger bytesperpixel = 4; nsuinteger bytesperrow = bytesperpixel * kdetectorsize; nsuinteger bitspercomponent = 8; unsigned char *rawdata = malloc(pixelcount * bytesperpixel); cgcontextref context = cgbitmapcontextcreate(rawdata, kdetectorsize, kdetectorsize, bitspercomponent, bytesperrow, colorspace, kcgimagealphapremultipliedlast | kcgbitmapbyteorder32big); cgcolorspacerelease(colorspace); cgcontextsetinterpolationquality(context, kcginterpolationnone); nslog(@"drawing image"); cgcontextdrawimage(context, cgrectmake(0, 0, kdetectorsize, kdetectorsize), [image cgimage]); nslog(@"image drawn"); cgcontextrelease(context); // rawdata contains image data in rgba8888 pixel format. alpha values ignored int byteindex = 0; cgfloat red = 0.0; cgfloat green = 0.0; cgfloat blue = 0.0; (int = 0 ; < pixelcount; ++i) { red += rawdata[byteindex]; green += rawdata[byteindex + 1]; blue += rawdata[byteindex + 2]; byteindex += bytesperpixel; } free(rawdata); return [uicolor colorwithred:red/pixelcount/255.0 green:green/pixelcount/255.0 blue:blue/pixelcount/255.0 alpha:1.0]; }
kdetectorsize set 6 such processed image has size of 6x6 pixels. 1 of image-parameter has been cropped 6x6 pixels before. slow part cgcontextdrawimage takes 500-600ms on iphone 4. tried alternatives line:
uigraphicspushcontext(context); [image drawatpoint:cgpointmake(0.0, 0.0)]; uigraphicspopcontext();
or
uigraphicspushcontext(context); [image drawinrect:cgrectmake(0.0, 0.0, kdetectorsize, kdetectorsize)]; uigraphicspopcontext();
both approaches slow 1 above. image size not have significant influence (i'd has no influence). know faster way access rgb value?
it ok, too, if thread not cause camera view lag. call thread that:
- (void)imagepickercontroller:(uiimagepickercontroller *)picker didfinishpickingmediawithinfo:(nsdictionary *)info { uiimage *image = [info objectforkey:uiimagepickercontrolleroriginalimage]; [nsthread detachnewthreadselector:@selector(pickcolorfromimage:) totarget:self withobject:image]; } - (void)pickcolorfromimage:(uiimage *)image { nsautoreleasepool *pool = [[nsautoreleasepool alloc] init]; [nsthread setthreadpriority:0.0]; [...cropping image...] uicolor *averagecolor = [self getaveragecolorofimage:croppedimage]; [self performselectoronmainthread:@selector(applypickedcolor:) withobject:averagecolor waituntildone:no]; [pool release]; }
thanks help!
you're approaching in wrong way - apple offer class want without messing around timers , uiimagepickers. avcapturesession
, related classes give realtime access raw pixel data camera(s).
for more information refer documentation:
Comments
Post a Comment