title: Wrong result on Barcode scanning using AVFoundation framework
body: <p>I am using iOS 7 AVFoundation framework for Barcode scanning. It works fine for me but on some Barcodes it gives me wrong result.</p>
  
  <p>I am adding code file and Barcode also to generate the issue. Please take a look and help me to find out the problem.</p>
  
  <p>Code Sample:</p>
  
  <p>ViewController.h Class-</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  #import &lt;AVFoundation/AVFoundation.h&gt;
  
  @interface ViewController : UIViewController &lt;AVCaptureMetadataOutputObjectsDelegate&gt;
  {
      AVCaptureSession                *_session;
      AVCaptureDevice                 *_device;
      AVCaptureDeviceInput            *_input;
      AVCaptureMetadataOutput         *_output;
      AVCaptureVideoPreviewLayer      *_prevLayer;
  
      UIView                          *_highlightView;
  
      IBOutlet    UIView              *_viewBg;
      IBOutlet    UIButton            *_btnScanning;
  }
  
  @end
  </code></pre>
  
  <p>ViewController.m Class Methods -</p>
  
  <pre><code>- (void)setScanningVideoOrientation
  {
      if ([UIDevice currentDevice].orientation == UIInterfaceOrientationPortrait)
      {
          _prevLayer.connection.videoOrientation = AVCaptureVideoOrientationPortrait;
      }
      else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationPortraitUpsideDown)
      {
          _prevLayer.connection.videoOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
      }
      else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft)
      {
          _prevLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
      }
      else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeRight)
      {
          _prevLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
      }
  }
  
  
  - (void)startScanning
  {
      // Create Session.------------------------------------------------------
      _session = nil;
      _session = [[AVCaptureSession alloc] init];
      _device = nil;
      _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
      NSError *error = nil;
  
      _input = nil;
      _input = [AVCaptureDeviceInput deviceInputWithDevice:_device error:&amp;error];
      if (_input)
      {
          [_session addInput:_input];
  
          _output = nil;
          _output = [[AVCaptureMetadataOutput alloc] init];
          [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
          [_session addOutput:_output];
  
          _output.metadataObjectTypes = [_output availableMetadataObjectTypes];
  
          _prevLayer = nil;
          _prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
          _prevLayer.frame = _viewBg.frame;
          _prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
          [self setScanningVideoOrientation];
          [self.view.layer addSublayer:_prevLayer];
  
          [_session startRunning];
      } else
      {
          NSLog(@"Error: %@", error);
      }
      //----------------------------------------------------------------------
  }
  </code></pre>
  
  <p>In this delegate method. I am getting wrong barcode string. Please take a look on 'detectionString'.</p>
  
  <pre><code>- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray     *)metadataObjects fromConnection:(AVCaptureConnection *)connection
  {
      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) {
          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)
          {
              NSLog(@"ViewController--&gt;captureOutput--&gt;detectionString = %@",detectionString);
              [self stopScanning];
              break;
          }
      }
  
      barCodeObject   = nil;
      detectionString = nil;
      barCodeTypes    = nil;
  }
  </code></pre>
  
  <p>Barcode Image -</p>
  
  <p><img src="https://i.stack.imgur.com/TQ8gv.jpg" alt="enter image description here"></p>
  
  <p>I am getting result - 0649954006029</p>
  
  <p>But it should be - 649954006029.</p>
  
  <p>As for some other barcodes, i am seeing 'l' before actual barcode scanning string.</p>
  
  <p>Hope it will help you to identify the problem.</p>
  
  <p>Thanks.</p>