I forgot to mention the current code
(void)setupNativeScanner
{
AVCaptureSession* captureSession = [[AVCaptureSession alloc] init];
self.session = captureSession;
AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
self.device = captureDevice;
NSError *error = nil;
self.input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
if (self.input)
[_session addInput:_input];
else
NSLog(@"Error: %@", error);
AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
[captureSession addOutput:captureMetadataOutput];
self.output = captureMetadataOutput;
dispatch_queue_t dispatchQueue;
dispatchQueue = dispatch_queue_create("myQueue", NULL);
[captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];
[captureMetadataOutput setMetadataObjectTypes:@[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode]];
// [self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
// [captureSession addOutput:_output];
self.output.metadataObjectTypes = [self.output availableMetadataObjectTypes];
self.prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
self.prevLayer.frame = self.view.bounds;
self.prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:self.prevLayer];
[self.session startRunning];
[self.view bringSubviewToFront:self.highlightView];
}
(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects
fromConnection:(AVCaptureConnection *)connection
{
connection.enabled = NO;
CGRect highlightViewRect = CGRectZero;
AVMetadataMachineReadableCodeObject *barCodeObject;
NSString *detectionString = nil;
NSArray *barCodeTypes = @[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode];
for (AVMetadataObject *metadata in metadataObjects)
{
NSLog(@"metadata type %@", [metadata type]);
if ([metadata respondsToSelector:@selector(stringValue)])
NSLog(@"metadata string %@", [(AVMetadataMachineReadableCodeObject *)metadata stringValue]);
for (NSString *type in barCodeTypes)
{
if ([metadata.type isEqualToString:type])
{
barCodeObject = (AVMetadataMachineReadableCodeObject *)[_prevLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata];
highlightViewRect = barCodeObject.bounds;
detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
break;
}
}
if (detectionString != nil)
{
[self.nativeScannerDelegate didFoundCode:metadata.type result:detectionString];
break;
}
}
[self performSelector:@selector(closeWithoutAnimation) withObject:nil afterDelay:0.1];
self.highlightView.frame = highlightViewRect;
}