title: Reading a barcode image without using cocoapods or other external API's
body: <p>I'm trying to use the new Apple Vision API to detect a barcode from an image and return its details. I've successfully detected a QR code and returned a message using the <code>CIDetector</code>. However I can't make this work for 1 dimensional barcodes. Heres an example result:</p>
  
  <pre><code>import UIKit
  import Vision
  
  class BarcodeDetector {
  
      func recognizeBarcode(for source: UIImage,
                              complete: @escaping (UIImage) -&gt; Void) {
          var resultImage = source
          let detectBarcodeRequest = VNDetectBarcodesRequest { (request, error) in
              if error == nil {
                  if let results = request.results as? [VNBarcodeObservation] {
                      print("Number of Barcodes found: \(results.count)")
                      if results.count == 0 { print("\r") }
  
                      var barcodeBoundingRects = [CGRect]()
                      for barcode in results {
                          barcodeBoundingRects.append(barcode.boundingBox)
                          let barcodeType = String(barcode.symbology.rawValue)?.replacingOccurrences(of: "VNBarcodeSymbology", with: "")
                          print("-Barcode Type: \(barcodeType!)")
  
                          if barcodeType == "QR" {
                              let image = CIImage(image: source)
                              image?.cropping(to: barcode.boundingBox)
                              self.qrCodeDescriptor(qrCode: barcode, qrCodeImage: image!)
                          }
                      }
                      resultImage = self.drawOnImage(source: resultImage, barcodeBoundingRects: barcodeBoundingRects)
                  }
              } else {
                  print(error!.localizedDescription)
              }
              complete(resultImage)
          }
          let vnImage = VNImageRequestHandler(cgImage: source.cgImage!, options: [:])
          try? vnImage.perform([detectBarcodeRequest])
      }
  
      private func qrCodeDescriptor(qrCode: VNBarcodeObservation, qrCodeImage: CIImage) {
          if let description = qrCode.barcodeDescriptor as? CIQRCodeDescriptor {
              readQRCode(qrCodeImage: qrCodeImage)
              print(" -Payload: \(description.errorCorrectedPayload)")
              print(" -Mask Pattern: \(description.maskPattern)")
              print(" -Symbol Version: \(description.symbolVersion)\n")
          }
      }
  
      private func readQRCode(qrCodeImage: CIImage) {
          let detector: CIDetector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy: CIDetectorAccuracyHigh])!
          var qrCodeLink = ""
  
          let features = detector.features(in: qrCodeImage)
          for feature in features as! [CIQRCodeFeature] {
              if let messageString = feature.messageString {
                  qrCodeLink += messageString
              }
          }
  
          if qrCodeLink == "" {
              print(" -No Code Message")
          } else {
              print(" -Code Message: \(qrCodeLink)")
          }
      }
  </code></pre>
  
  <p>How can I convert the image into an <code>AVMetadataObject</code> and then read it from there? Or is there a better approach?</p>