Skip to content

Instantly share code, notes, and snippets.

@agmcleod
Created September 23, 2014 20:00
Show Gist options
  • Save agmcleod/64d5436a691648a4dc56 to your computer and use it in GitHub Desktop.
Save agmcleod/64d5436a691648a4dc56 to your computer and use it in GitHub Desktop.
//
// BarcodeViewController.m
// HSTestApp
//
// Created by McLeod, Aaron on 2014-08-14.
// Copyright (c) 2014 McLeod, Aaron. All rights reserved.
//
#import "BarcodeViewController.h"
#import "HTMLViewHelper.h"
@interface BarcodeViewController ()
@end
@implementation BarcodeViewController
- (void)viewDidLoad {
[super viewDidLoad];
HTMLViewHelper *htmlViewHelper = [[HTMLViewHelper alloc] init];
[_webView loadRequest:[NSURLRequest requestWithURL:[htmlViewHelper htmlFilePathFor:@"barcode"]]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma AVCapture delegates
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
AVMetadataMachineReadableCodeObject *barCodeObject;
NSString *detectionString = nil;
NSArray *barCodeTypes = @[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode];
for (AVMetadataObject *metadata in metadataObjects) {
for (NSString *type in barCodeTypes) {
if ([metadata.type isEqualToString:type]) {
barCodeObject = (AVMetadataMachineReadableCodeObject *)[_prevLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata];
detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
break;
}
}
}
if (detectionString != nil) {
[_session stopRunning];
[_prevLayer removeFromSuperlayer];
[_webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"setBarcodeLabel('%@')", detectionString]];
}
}
- (void)scanBarCode
{
_session = [[AVCaptureSession alloc] init];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (input) {
[_session addInput:input];
}
else {
NSLog(@"Error: %@", error);
}
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
[_session addOutput:output];
output.metadataObjectTypes = [output availableMetadataObjectTypes];
_prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
_prevLayer.frame = self.view.bounds;
_prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:_prevLayer];
[_session startRunning];
}
#pragma UIWebView delegates
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *requestURL = [request URL];
if ([[requestURL scheme] isEqualToString:@"callback"]) {
if ([[requestURL path] isEqualToString:@"/scan"]) {
[self scanBarCode];
return NO;
}
else if ([[requestURL path] isEqualToString:@"/next"]) {
[self performSegueWithIdentifier:@"toFinish" sender:self];
return NO;
}
}
return YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment