|
9 | 9 | #import "CMGViewController.h" |
10 | 10 | #import "CMGOverlayView.h" |
11 | 11 |
|
12 | | -@interface CMGViewController ()<AVCaptureMetadataOutputObjectsDelegate> |
| 12 | +@interface CMGViewController ()<AVCaptureMetadataOutputObjectsDelegate, UIAlertViewDelegate> |
13 | 13 |
|
14 | 14 | @property(nonatomic, strong) AVCaptureSession *session; |
15 | 15 | @property(nonatomic, strong) UIView *previewView; |
16 | 16 | @property(nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer; |
17 | 17 | @property(nonatomic, strong) CMGOverlayView *overlayView; |
| 18 | +@property(nonatomic, strong) IBOutlet UILabel *barcode; |
18 | 19 |
|
19 | 20 | @end |
20 | 21 |
|
@@ -51,9 +52,21 @@ - (void)viewDidLoad |
51 | 52 | break; |
52 | 53 | } |
53 | 54 | } |
| 55 | + |
| 56 | + NSError *error = nil; |
| 57 | + [camera lockForConfiguration:&error]; |
| 58 | + if([camera isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]) { |
| 59 | + [camera setFocusMode:AVCaptureFocusModeContinuousAutoFocus]; |
| 60 | + } |
| 61 | + if([camera isAutoFocusRangeRestrictionSupported]) { |
| 62 | + [camera setAutoFocusRangeRestriction:AVCaptureAutoFocusRangeRestrictionNear]; |
| 63 | + } |
| 64 | + [camera unlockForConfiguration]; |
| 65 | + if(error) { |
| 66 | + NSLog(@"Erorr locking for configuration, %@", error); |
| 67 | + } |
54 | 68 |
|
55 | 69 | // Create a AVCaptureInput with the camera device |
56 | | - NSError *error=nil; |
57 | 70 | AVCaptureDeviceInput *cameraInput = [[AVCaptureDeviceInput alloc] initWithDevice:camera error:&error]; |
58 | 71 | if (cameraInput == nil) { |
59 | 72 | NSLog(@"Error to create camera capture:%@",error); |
@@ -74,7 +87,7 @@ - (void)viewDidLoad |
74 | 87 | AVMetadataObjectTypeEAN13Code, |
75 | 88 | AVMetadataObjectTypeEAN8Code, |
76 | 89 | AVMetadataObjectTypePDF417Code, |
77 | | - AVMetadataObjectTypeQRCode, |
| 90 | +// AVMetadataObjectTypeQRCode, |
78 | 91 | AVMetadataObjectTypeUPCECode]]; |
79 | 92 |
|
80 | 93 | NSMutableArray *supportedMetaDataTypes = [NSMutableArray array]; |
@@ -93,32 +106,41 @@ - (void)viewDidLoad |
93 | 106 | [self.session startRunning]; |
94 | 107 | } |
95 | 108 |
|
| 109 | +-(void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { |
| 110 | + [self.session startRunning]; |
| 111 | +} |
| 112 | + |
96 | 113 | - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection { |
97 | | - // draw where the recognised data is - for 1D barcodes this turns out to be just a line accross the code. For 2D barcodes it's a bit more interesting |
98 | | - CGMutablePathRef pathRef = CGPathCreateMutable(); |
99 | | - CGAffineTransform transform = CGAffineTransformMakeScale(self.overlayView.bounds.size.width, self.overlayView.bounds.size.height); |
100 | | - |
101 | | - for(AVMetadataMachineReadableCodeObject *recognizedObject in metadataObjects) { |
102 | | - NSLog(@"%@", recognizedObject.stringValue); |
103 | | - |
104 | | - CGPoint p1 = CGPointMake(0.4, 0.4); |
105 | | - CGPointMakeWithDictionaryRepresentation((__bridge CFDictionaryRef)(recognizedObject.corners[0]), &p1); |
106 | | - CGPoint p2 = CGPointMake(0.5, 0.4); |
107 | | - CGPointMakeWithDictionaryRepresentation((__bridge CFDictionaryRef)(recognizedObject.corners[1]), &p2); |
108 | | - CGPoint p3 = CGPointMake(0.5, 0.5); |
109 | | - CGPointMakeWithDictionaryRepresentation((__bridge CFDictionaryRef)(recognizedObject.corners[2]), &p3); |
110 | | - CGPoint p4 = CGPointMake(0.4, 0.5); |
111 | | - CGPointMakeWithDictionaryRepresentation((__bridge CFDictionaryRef)(recognizedObject.corners[3]), &p4); |
112 | | - |
113 | | - CGPathMoveToPoint(pathRef, &transform, 1-p1.y, p1.x); |
114 | | - CGPathAddLineToPoint(pathRef, &transform, 1-p2.y, p2.x); |
115 | | - CGPathAddLineToPoint(pathRef, &transform, 1-p3.y, p3.x); |
116 | | - CGPathAddLineToPoint(pathRef, &transform, 1-p4.y, p4.x); |
117 | | - CGPathCloseSubpath(pathRef); |
| 114 | + if(metadataObjects.count > 0) { |
| 115 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 116 | + AVMetadataMachineReadableCodeObject *recognizedObject = metadataObjects.firstObject; |
| 117 | + self.barcode.text = recognizedObject.stringValue; |
| 118 | + [[[UIAlertView alloc] initWithTitle:@"Scanned" message:recognizedObject.stringValue delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; |
| 119 | + }); |
| 120 | + [self.session stopRunning]; |
| 121 | + // draw where the recognised data is - for 1D barcodes this turns out to be just a line accross the code. For 2D barcodes it's a bit more interesting |
| 122 | + CGMutablePathRef pathRef = CGPathCreateMutable(); |
| 123 | + CGAffineTransform transform = CGAffineTransformMakeScale(self.overlayView.bounds.size.width, self.overlayView.bounds.size.height); |
118 | 124 |
|
| 125 | + for(AVMetadataMachineReadableCodeObject *recognizedObject in metadataObjects) { |
| 126 | + CGPoint p1 = CGPointMake(0.4, 0.4); |
| 127 | + CGPointMakeWithDictionaryRepresentation((__bridge CFDictionaryRef)(recognizedObject.corners[0]), &p1); |
| 128 | + CGPoint p2 = CGPointMake(0.5, 0.4); |
| 129 | + CGPointMakeWithDictionaryRepresentation((__bridge CFDictionaryRef)(recognizedObject.corners[1]), &p2); |
| 130 | + CGPoint p3 = CGPointMake(0.5, 0.5); |
| 131 | + CGPointMakeWithDictionaryRepresentation((__bridge CFDictionaryRef)(recognizedObject.corners[2]), &p3); |
| 132 | + CGPoint p4 = CGPointMake(0.4, 0.5); |
| 133 | + CGPointMakeWithDictionaryRepresentation((__bridge CFDictionaryRef)(recognizedObject.corners[3]), &p4); |
| 134 | + |
| 135 | + CGPathMoveToPoint(pathRef, &transform, 1-p1.y, p1.x); |
| 136 | + CGPathAddLineToPoint(pathRef, &transform, 1-p2.y, p2.x); |
| 137 | + CGPathAddLineToPoint(pathRef, &transform, 1-p3.y, p3.x); |
| 138 | + CGPathAddLineToPoint(pathRef, &transform, 1-p4.y, p4.x); |
| 139 | + CGPathCloseSubpath(pathRef); |
| 140 | + } |
| 141 | + ((CAShapeLayer *) self.overlayView.layer).path = pathRef; |
| 142 | + CGPathRelease(pathRef); |
119 | 143 | } |
120 | | - ((CAShapeLayer *) self.overlayView.layer).path = pathRef; |
121 | | - CGPathRelease(pathRef); |
122 | 144 | } |
123 | 145 |
|
124 | 146 | -(void) viewDidLayoutSubviews { |
|