Skip to content

Instantly share code, notes, and snippets.

@ambujshukla
Forked from Alex04/AMScanViewController.h
Created November 29, 2013 07:33
Show Gist options
  • Select an option

  • Save ambujshukla/7702599 to your computer and use it in GitHub Desktop.

Select an option

Save ambujshukla/7702599 to your computer and use it in GitHub Desktop.

Revisions

  1. @Alex04 Alex04 revised this gist Oct 14, 2013. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion AMScanViewController.h
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    //
    // AMScanViewController.h
    // EasyOrder
    //
    //
    // Created by Alexander Mack on 11.10.13.
    // Copyright (c) 2013 Alexander Mack. All rights reserved.
    2 changes: 1 addition & 1 deletion AMScanViewController.m
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    //
    // AMScanViewController.m
    // EasyOrder
    //
    //
    // Created by Alexander Mack on 11.10.13.
    // Copyright (c) 2013 Alexander Mack. All rights reserved.
  2. @Alex04 Alex04 revised this gist Oct 14, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions AMScanViewController.h
    Original file line number Diff line number Diff line change
    @@ -24,11 +24,11 @@

    @end

    @protocol EDScanViewControllerDelegate <NSObject>
    @protocol AMScanViewControllerDelegate <NSObject>

    @optional

    - (void) scanViewController:(EDScanViewController *) aCtler didTabToFocusOnPoint:(CGPoint) aPoint;
    - (void) scanViewController:(EDScanViewController *) aCtler didSuccessfullyScan:(NSString *) aScannedValue;
    - (void) scanViewController:(AMScanViewController *) aCtler didTabToFocusOnPoint:(CGPoint) aPoint;
    - (void) scanViewController:(AMScanViewController *) aCtler didSuccessfullyScan:(NSString *) aScannedValue;

    @end
  3. @Alex04 Alex04 created this gist Oct 14, 2013.
    34 changes: 34 additions & 0 deletions AMScanViewController.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    //
    // AMScanViewController.h
    // EasyOrder
    //
    // Created by Alexander Mack on 11.10.13.
    // Copyright (c) 2013 Alexander Mack. All rights reserved.
    //

    #import <UIKit/UIKit.h>
    #import <AVFoundation/AVFoundation.h>

    @protocol AMScanViewControllerDelegate;

    @interface AMScanViewController : UIViewController <AVCaptureMetadataOutputObjectsDelegate>

    @property (nonatomic, weak) id<EDScanViewControllerDelegate> delegate;

    @property (assign, nonatomic) BOOL touchToFocusEnabled;

    - (BOOL) isCameraAvailable;
    - (void) startScanning;
    - (void) stopScanning;
    - (void) setTourch:(BOOL) aStatus;

    @end

    @protocol EDScanViewControllerDelegate <NSObject>

    @optional

    - (void) scanViewController:(EDScanViewController *) aCtler didTabToFocusOnPoint:(CGPoint) aPoint;
    - (void) scanViewController:(EDScanViewController *) aCtler didSuccessfullyScan:(NSString *) aScannedValue;

    @end
    204 changes: 204 additions & 0 deletions AMScanViewController.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,204 @@
    //
    // AMScanViewController.m
    // EasyOrder
    //
    // Created by Alexander Mack on 11.10.13.
    // Copyright (c) 2013 Alexander Mack. All rights reserved.
    //

    #import "AMScanViewController.h"

    @interface AMScanViewController ()

    @property (strong, nonatomic) AVCaptureDevice* device;
    @property (strong, nonatomic) AVCaptureDeviceInput* input;
    @property (strong, nonatomic) AVCaptureMetadataOutput* output;
    @property (strong, nonatomic) AVCaptureSession* session;
    @property (strong, nonatomic) AVCaptureVideoPreviewLayer* preview;

    @end

    @implementation AMScanViewController

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

    - (void)viewWillAppear:(BOOL)animated;
    {
    [super viewWillAppear:animated];
    if(![self isCameraAvailable]) {
    [self setupNoCameraView];
    }
    }

    - (void) viewDidAppear:(BOOL)animated;
    {
    [super viewDidAppear:animated];
    }

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    if([self isCameraAvailable]) {
    [self setupScanner];
    }
    }

    - (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

    - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)evt
    {
    if(self.touchToFocusEnabled) {
    UITouch *touch=[touches anyObject];
    CGPoint pt= [touch locationInView:self.view];
    [self focus:pt];
    }
    }

    #pragma mark -
    #pragma mark NoCamAvailable

    - (void) setupNoCameraView;
    {
    UILabel *labelNoCam = [[UILabel alloc] init];
    labelNoCam.text = @"No Camera available";
    labelNoCam.textColor = [UIColor blackColor];
    [self.view addSubview:labelNoCam];
    [labelNoCam sizeToFit];
    labelNoCam.center = self.view.center;
    }

    - (NSUInteger)supportedInterfaceOrientations;
    {
    return UIInterfaceOrientationMaskLandscape;
    }

    - (BOOL)shouldAutorotate;
    {
    return (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation]));
    }

    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;
    {
    if([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) {
    AVCaptureConnection *con = self.preview.connection;
    con.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
    } else {
    AVCaptureConnection *con = self.preview.connection;
    con.videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
    }
    }

    #pragma mark -
    #pragma mark AVFoundationSetup

    - (void) setupScanner;
    {
    self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];

    self.session = [[AVCaptureSession alloc] init];

    self.output = [[AVCaptureMetadataOutput alloc] init];
    [self.session addOutput:self.output];
    [self.session addInput:self.input];

    [self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    self.output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];

    self.preview = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
    self.preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
    self.preview.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

    AVCaptureConnection *con = self.preview.connection;

    con.videoOrientation = AVCaptureVideoOrientationLandscapeLeft;

    [self.view.layer insertSublayer:self.preview atIndex:0];
    }

    #pragma mark -
    #pragma mark Helper Methods

    - (BOOL) isCameraAvailable;
    {
    NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    return [videoDevices count] > 0;
    }

    - (void)startScanning;
    {
    [self.session startRunning];

    }

    - (void) stopScanning;
    {
    [self.session stopRunning];
    }

    - (void) setTourch:(BOOL) aStatus;
    {
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    [device lockForConfiguration:nil];
    if ( [device hasTorch] ) {
    if ( aStatus ) {
    [device setTorchMode:AVCaptureTorchModeOn];
    } else {
    [device setTorchMode:AVCaptureTorchModeOff];
    }
    }
    [device unlockForConfiguration];
    }

    - (void) focus:(CGPoint) aPoint;
    {
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if([device isFocusPointOfInterestSupported] &&
    [device isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    double screenWidth = screenRect.size.width;
    double screenHeight = screenRect.size.height;
    double focus_x = aPoint.x/screenWidth;
    double focus_y = aPoint.y/screenHeight;
    if([device lockForConfiguration:nil]) {
    if([self.delegate respondsToSelector:@selector(scanViewController:didTabToFocusOnPoint:)]) {
    [self.delegate scanViewController:self didTabToFocusOnPoint:aPoint];
    }
    [device setFocusPointOfInterest:CGPointMake(focus_x,focus_y)];
    [device setFocusMode:AVCaptureFocusModeAutoFocus];
    if ([device isExposureModeSupported:AVCaptureExposureModeAutoExpose]){
    [device setExposureMode:AVCaptureExposureModeAutoExpose];
    }
    [device unlockForConfiguration];
    }
    }
    }

    #pragma mark -
    #pragma mark AVCaptureMetadataOutputObjectsDelegate

    - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects
    fromConnection:(AVCaptureConnection *)connection
    {
    for(AVMetadataObject *current in metadataObjects) {
    if([current isKindOfClass:[AVMetadataMachineReadableCodeObject class]]) {
    if([self.delegate respondsToSelector:@selector(scanViewController:didSuccessfullyScan:)]) {
    NSString *scannedValue = [((AVMetadataMachineReadableCodeObject *) current) stringValue];
    [self.delegate scanViewController:self didSuccessfullyScan:scannedValue];
    }
    }
    }
    }

    @end