Advertisement
Guest User

Untitled

a guest
May 24th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.67 KB | None | 0 0
  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface igViewController : UIViewController
  4.  
  5. @end
  6.  
  7. #import <AVFoundation/AVFoundation.h>
  8. #import "igViewController.h"
  9.  
  10. @interface igViewController () <AVCaptureMetadataOutputObjectsDelegate>
  11. {
  12. AVCaptureSession *_session;
  13. AVCaptureDevice *_device;
  14. AVCaptureDeviceInput *_input;
  15. AVCaptureMetadataOutput *_output;
  16. AVCaptureVideoPreviewLayer *_prevLayer;
  17.  
  18. UIView *_highlightView;
  19. UILabel *_label;
  20. }
  21. @end
  22.  
  23. @implementation igViewController
  24.  
  25. - (void)viewDidLoad
  26. {
  27. [super viewDidLoad];
  28.  
  29. _highlightView = [[UIView alloc] init];
  30. _highlightView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin;
  31. _highlightView.layer.borderColor = [UIColor greenColor].CGColor;
  32. _highlightView.layer.borderWidth = 3;
  33. [self.view addSubview:_highlightView];
  34.  
  35. _label = [[UILabel alloc] init];
  36. _label.frame = CGRectMake(0, self.view.bounds.size.height - 40, self.view.bounds.size.width, 40);
  37. _label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
  38. _label.backgroundColor = [UIColor colorWithWhite:0.15 alpha:0.65];
  39. _label.textColor = [UIColor whiteColor];
  40. _label.textAlignment = NSTextAlignmentCenter;
  41. _label.text = @"(none)";
  42. [self.view addSubview:_label];
  43.  
  44. _session = [[AVCaptureSession alloc] init];
  45. _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  46. NSError *error = nil;
  47.  
  48. _input = [AVCaptureDeviceInput deviceInputWithDevice:_device error:&error];
  49. if (_input) {
  50. [_session addInput:_input];
  51. } else {
  52. NSLog(@"Error: %@", error);
  53. }
  54.  
  55. _output = [[AVCaptureMetadataOutput alloc] init];
  56. [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
  57. [_session addOutput:_output];
  58.  
  59. _output.metadataObjectTypes = [_output availableMetadataObjectTypes];
  60. for (NSString* avail in _output.metadataObjectTypes) {
  61. NSLog(@"Avail...%@", avail);
  62. }
  63. _prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
  64. _prevLayer.frame = self.view.bounds;
  65. _prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
  66. [self.view.layer addSublayer:_prevLayer];
  67.  
  68. [_session startRunning];
  69.  
  70. [self.view bringSubviewToFront:_highlightView];
  71. [self.view bringSubviewToFront:_label];
  72. }
  73.  
  74. - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
  75. {
  76. NSLog(@"Failed...");
  77. CGRect highlightViewRect = CGRectZero;
  78. AVMetadataMachineReadableCodeObject *barCodeObject;
  79. NSString *detectionString = nil;
  80. NSArray *barCodeTypes = @[AVMetadataObjectTypeAztecCode];
  81.  
  82. for (AVMetadataObject *metadata in metadataObjects) {
  83. NSLog(@".....%@", metadata.type);
  84. for (NSString *type in barCodeTypes) {
  85. if ([metadata.type isEqualToString:type])
  86. {
  87. barCodeObject = (AVMetadataMachineReadableCodeObject *)[_prevLayer transformedMetadataObjectForMetadataObjectAVMetadataMachineReadableCodeObject *)metadata];
  88. highlightViewRect = barCodeObject.bounds;
  89. detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
  90. break;
  91. }
  92. }
  93.  
  94. if (detectionString != nil)
  95. {
  96. _label.text = detectionString;
  97. break;
  98. }
  99. else
  100. _label.text = @"(none)";
  101. }
  102.  
  103. //_label.text = @"(nonessss)";
  104.  
  105. _highlightView.frame = highlightViewRect;
  106. }
  107.  
  108. @end
  109.  
  110. #import <UIKit/UIKit.h>
  111. #import <AVFoundation/AVFoundation.h>
  112.  
  113. @interface ScanVC : UIViewController <AVCaptureMetadataOutputObjectsDelegate>
  114.  
  115. @property (retain, nonatomic) UILabel *scannerWindow;
  116. @property (retain, nonatomic) UILabel *statusLabel;
  117. @property (retain, nonatomic) UIButton *cancelButton;
  118.  
  119. @end
  120.  
  121. #import "ScanVC.h"
  122.  
  123. @interface ScanVC ()
  124.  
  125. @property (nonatomic) BOOL isReading;
  126. @property (nonatomic, strong) AVCaptureSession *captureSession;
  127. @property (nonatomic, strong) AVCaptureVideoPreviewLayer *videoPreviewLayer;
  128.  
  129. @end
  130.  
  131.  
  132. @implementation ScanVC
  133.  
  134. @synthesize cancelButton;
  135. @synthesize statusLabel;
  136. @synthesize scannerWindow;
  137.  
  138.  
  139. - (void)viewDidLoad {
  140. [super viewDidLoad];
  141.  
  142. _isReading = NO;
  143. _captureSession = nil;
  144.  
  145. //place a close button
  146. cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
  147. [cancelButton addTarget:self action:@selector(closeScan) forControlEvents:UIControlEventTouchUpInside];
  148. [cancelButton setTitle:@"Close" forState:UIControlStateNormal];
  149. cancelButton.frame = CGRectMake(0, 410, 250, 40);
  150. [self.view addSubview:cancelButton];
  151.  
  152. //place a status label
  153. statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 340, 250, 40)];
  154. statusLabel.text = @"Currently not scanning";
  155. [self.view addSubview:statusLabel];
  156.  
  157. //place the scanner window (adjust the size)
  158. scannerWindow = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 250, 250)];
  159. scannerWindow.text = @"Camera Capture Window";
  160. [self.view addSubview:scannerWindow];
  161.  
  162. //start the scan immediately when the view loads
  163. [self startStopScan];
  164. }
  165.  
  166. - (void)closeScan {
  167. if(_isReading) {
  168. [self stopReading];
  169. }
  170.  
  171. _isReading = !_isReading;
  172.  
  173. //dismiss the view controller here?
  174. }];
  175. }
  176.  
  177. - (void)startStopScan {
  178.  
  179. if (!_isReading) {
  180. if([self startReading]) {
  181. [statusLabel setText:@"Scanning for Barcode"];
  182. }
  183. } else {
  184. [self stopReading];
  185. }
  186.  
  187. _isReading = !_isReading;
  188. }
  189.  
  190. - (BOOL)startReading {
  191. NSError *error;
  192.  
  193. AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  194. AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
  195.  
  196. if(!input) {
  197. NSLog(@"%@", [error localizedDescription]);
  198. return NO;
  199. }
  200.  
  201. _captureSession = [[AVCaptureSession alloc] init];
  202. [_captureSession addInput:input];
  203.  
  204. AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
  205. [_captureSession addOutput:captureMetadataOutput];
  206.  
  207. dispatch_queue_t dispatchQueue;
  208. dispatchQueue = dispatch_queue_create("myQueue", NULL);
  209. [captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];
  210. [captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeAztecCode]];
  211.  
  212. //show the preview to the user
  213. _videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
  214. [_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
  215. [_videoPreviewLayer setFrame:scannerWindow.layer.bounds];
  216. [scannerWindow.layer addSublayer:_videoPreviewLayer];
  217.  
  218. [_captureSession startRunning];
  219.  
  220. return YES;
  221. }
  222.  
  223. -(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
  224. if (metadataObjects != nil && [metadataObjects count] > 0) {
  225. AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
  226. if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeAztecCode]) {
  227. [statusLabel performSelectorOnMainThread:@selector(setText:) withObject:[metadataObj stringValue] waitUntilDone:NO];
  228.  
  229. [self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO];
  230. _isReading = NO;
  231.  
  232.  
  233.  
  234. //do things after a successful scan here
  235. NSLog(@"scanner output %@", [metadataObj stringValue]);
  236. }
  237. }
  238. }
  239.  
  240. - (void)stopReading {
  241. [_captureSession stopRunning];
  242. _captureSession = nil;
  243.  
  244. [_videoPreviewLayer removeFromSuperlayer];
  245. }
  246.  
  247.  
  248. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement