Skip to content

Instantly share code, notes, and snippets.

@Cellane
Created February 1, 2014 10:14
Show Gist options
  • Save Cellane/8750366 to your computer and use it in GitHub Desktop.
Save Cellane/8750366 to your computer and use it in GitHub Desktop.
//
// ANConsumptionViewController.m
// Futruy
//
// Created by Milan Vít on 31.01.14.
// Copyright (c) 2014 AiNeuron. All rights reserved.
//
#import "ANConsumptionViewController.h"
@interface ANConsumptionViewController ()
@property (nonatomic, strong) UIView *highlightView;
@property (nonatomic, strong) AVCaptureSession *session;
@property (nonatomic, strong) AVCaptureDevice *device;
@property (nonatomic, strong) AVCaptureDeviceInput *input;
@property (nonatomic, strong) AVCaptureMetadataOutput *output;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *previousLayer;
@end
@implementation ANConsumptionViewController
#pragma mark Initialisation methods
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
}
return self;
}
#pragma mark Superclass methods
- (void)viewDidLoad
{
NSError *error = nil;
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
[self setHighlightView:[[UIView alloc] init]];
[[self highlightView] setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin];
[[[self highlightView] layer] setBorderColor:[[UIColor colorWithRed:227/255.0 green:0 blue:24/255.0 alpha:1] CGColor]];
[[[self highlightView] layer] setBorderWidth:3];
[[self view] addSubview:[self highlightView]];
[self setSession:[[AVCaptureSession alloc] init]];
[self setDevice:[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]];
[self setInput:[AVCaptureDeviceInput deviceInputWithDevice:[self device] error:&error]];
if ([self input])
{
[[self session] addInput:self.input];
}
else
{
NSLog(@"Error: %@", error);
}
[self setOutput:[[AVCaptureMetadataOutput alloc] init]];
[[self output] setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
[[self session] addOutput:[self output]];
[[self output] setMetadataObjectTypes:[[self output] availableMetadataObjectTypes]];
[self setPreviousLayer:[AVCaptureVideoPreviewLayer layerWithSession:[self session]]];
[[self previousLayer] setFrame:[[self view] bounds]];
[[self previousLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[[[self view] layer] addSublayer:[self previousLayer]];
[[self session] startRunning];
[[self view] bringSubviewToFront:[self highlightView]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[touches enumerateObjectsUsingBlock:^(id obj, BOOL *stop)
{
UITouch *touch = obj;
CGPoint touchPoint = [touch locationInView:[touch view]];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat focusX, focusY;
if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
{
focusX = touchPoint.x / screenRect.size.width;
focusY = touchPoint.y / screenRect.size.height;
}
else
{
focusX = touchPoint.x / screenRect.size.height;
focusY = touchPoint.y / screenRect.size.width;
}
CGPoint focusPoint = CGPointMake(focusX, focusY);
if ([[self device] isFocusPointOfInterestSupported])
{
NSError *error;
if ([[self device] lockForConfiguration:&error])
{
[[self device] setFocusPointOfInterest:focusPoint];
[[self device] setExposurePointOfInterest:focusPoint];
[[self device] setFocusMode:AVCaptureFocusModeAutoFocus];
if ([[self device] isExposureModeSupported:AVCaptureExposureModeAutoExpose])
{
[[self device] setExposureMode:AVCaptureExposureModeAutoExpose];
}
[[self device] unlockForConfiguration];
}
}
}];
}
#pragma mark Private methods
- (void)orientationChanged:(NSNotification *)notification
{
}
#pragma mark AVCaptureMetadataOutputObjects delegate methods
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
CGRect highlightViewRect = CGRectZero;
AVMetadataMachineReadableCodeObject *barCodeObject;
NSString *detectionString = nil;
NSArray *barCodeTypes = @[AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeQRCode];
for (AVMetadataObject *metadata in metadataObjects)
{
for (NSString *type in barCodeTypes)
{
if ([[metadata type] isEqualToString:type])
{
barCodeObject = (AVMetadataMachineReadableCodeObject *)[[self previousLayer] transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *) metadata];
highlightViewRect = [barCodeObject bounds];
detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
break;
}
}
if (detectionString != nil)
{
// TODO: uncomment when you are ready to pass the value to next view controller
//[[self session] stopRunning];
break;
}
else
{
}
}
[[self highlightView] setFrame:highlightViewRect];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment