Skip to content

Instantly share code, notes, and snippets.

@nhisyam
Created April 19, 2012 02:05
Show Gist options
  • Save nhisyam/2417864 to your computer and use it in GitHub Desktop.
Save nhisyam/2417864 to your computer and use it in GitHub Desktop.

Revisions

  1. nhisyam revised this gist Apr 19, 2012. 1 changed file with 6 additions and 9 deletions.
    15 changes: 6 additions & 9 deletions ColorDetect.m
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    #import "ColorDetectVC.h"

    @interface ColorDetectVC ()
    @@ -82,15 +81,13 @@ - (void)newImageFrameAvailableFromDataSource:(GPUImageRawData *)rawDataSource;
    [_centerView setFrame:rect];

    GPUByteColorVector colorVector = [rawDataSource colorAtLocation:centerPosition];
    float red = (float)colorVector.red;
    float green = (float)colorVector.green;
    float blue = (float)colorVector.blue;
    float alpha = (float)colorVector.alpha;

    int red = [[NSNumber numberWithUnsignedChar:colorVector.red] intValue];
    int green = [[NSNumber numberWithUnsignedChar:colorVector.green] intValue];
    int blue = [[NSNumber numberWithUnsignedChar:colorVector.blue] intValue];
    int alpha = [[NSNumber numberWithUnsignedChar:colorVector.alpha] intValue];

    NSLog(@"R:%d G:%d B:%d A:%d",red, green, blue, alpha);

    UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
    NSLog(@"R:%f G:%f B:%f A:%f",red, green, blue, alpha);
    UIColor *color = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha/255.0];
    [_centerView setBackgroundColor:color];
    }
    }
  2. @invalid-email-address Anonymous created this gist Apr 19, 2012.
    96 changes: 96 additions & 0 deletions ColorDetect.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,96 @@

    #import "ColorDetectVC.h"

    @interface ColorDetectVC ()
    {
    GPUImageVideoCamera *videoCamera;
    GPUImageFilter *rotationFilter;
    GPUImageView *filteredVideoView;
    GPUImageRawData *positionRawData;
    }
    @end

    @implementation ColorDetectVC
    @synthesize centerView = _centerView;

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    }
    return self;
    }

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    [self configureVideoFiltering:AVCaptureDevicePositionBack];

    }

    - (void)viewDidUnload
    {
    [self setCenterView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    }

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }

    - (void)configureVideoFiltering:(AVCaptureDevicePosition)devicePosition
    {
    CGRect mainScreenFrame = [[UIScreen mainScreen] applicationFrame];
    videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:devicePosition];

    filteredVideoView = [[GPUImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, mainScreenFrame.size.width, mainScreenFrame.size.height)];
    [self.view addSubview:filteredVideoView];
    [self.view sendSubviewToBack:filteredVideoView];

    rotationFilter = [[GPUImageRotationFilter alloc] initWithRotation:kGPUImageRotateRight];

    CGSize videoPixelSize = CGSizeMake(480.0, 640.0);
    positionRawData = [[GPUImageRawData alloc] initWithImageSize:videoPixelSize];
    positionRawData.delegate = self;


    [videoCamera addTarget:rotationFilter];
    [rotationFilter addTarget:positionRawData];
    [rotationFilter addTarget:filteredVideoView];

    [videoCamera startCameraCapture];
    }

    #pragma mark - GPUImageRawDataProcessor

    - (void)newImageFrameAvailableFromDataSource:(GPUImageRawData *)rawDataSource;
    {
    if (rawDataSource == positionRawData)
    {
    CGRect mainScreenFrame = [[UIScreen mainScreen] applicationFrame];
    CGPoint centerPosition = CGPointMake(CGRectGetMidX(mainScreenFrame), CGRectGetMidY(mainScreenFrame));

    CGRect rect = _centerView.frame;
    rect.origin.x = centerPosition.x;
    rect.origin.y = centerPosition.y;
    [_centerView setFrame:rect];

    GPUByteColorVector colorVector = [rawDataSource colorAtLocation:centerPosition];

    int red = [[NSNumber numberWithUnsignedChar:colorVector.red] intValue];
    int green = [[NSNumber numberWithUnsignedChar:colorVector.green] intValue];
    int blue = [[NSNumber numberWithUnsignedChar:colorVector.blue] intValue];
    int alpha = [[NSNumber numberWithUnsignedChar:colorVector.alpha] intValue];

    NSLog(@"R:%d G:%d B:%d A:%d",red, green, blue, alpha);

    UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
    [_centerView setBackgroundColor:color];
    }
    }