title: QR Reader with VNDetectBarcodeRequest
body: <p>I've setup an <code>AVCaptureSession</code> with a video data output and am attempting to use iOS 11's Vision framework to read QR codes. The camera is setup like basically any <code>AVCaptureSession</code> is. I will abbreviate and just show setting up the output.</p>
  
  <pre><code>let output = AVCaptureVideoDataOutput()
  output.setSampleBufferDelegate(self, queue: captureQueue)
  captureSession.addOutput(output)
  
  // I did this to get the CVPixelBuffer to be oriented in portrait.
  // I don't know if it's needed and I'm not sure it matters anyway.
  output.connection(with: .video)!.videoOrientation = .portrait
  </code></pre>
  
  <p>So the camera is up and running as always. Here is the code I am using to perform a <code>VNImageRequestHandler</code> for QR codes.</p>
  
  <pre><code>func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
      guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
      let imageRequestHandler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer, orientation: .up, options: [:])
  
      let qrRequest = VNDetectBarcodesRequest { request, error in
          let barcodeObservations = request.results as? [VNBarcodeObservation]
          guard let qrCode = barcodeObservations?.flatMap({ $0.barcodeDescriptor as? CIQRCodeDescriptor }).first else { return }
          if let code = String(data: qrCode.errorCorrectedPayload, encoding: .isoLatin1) {
              debugPrint(code)
          }
      }
  
      qrRequest.symbologies = [.QR]
      try! imageRequestHandler.perform([qrRequest])
  }
  </code></pre>
  
  <p>I am using a QR code that encodes <code>http://www.google.com</code> as a test. The <code>debugPrint</code> line prints out: </p>
  
  <blockquote>
    <p><code>AVGG\u{03}¢ò÷wwrævöövÆRæ6öÐì\u{11}ì</code></p>
  </blockquote>
  
  <p>I have tested this same QR code with the <code>AVCaptureMetadataOutput</code> that has been around for a while and that method decodes the QR code correctly. So my question is, what have I missed to get the output that I am getting?</p>
  
  <p>(Obviously I could just use the <code>AVCaptureMetadataOutput</code> as a solution, because I can see that it works. But that doesn't help me learn how to use the Vision framework.)</p>