0

I am trying to use swift and a simple AVCaptureSession along with google's MLKit Barcode Detector to detect PDF417 barcodes. However, it does not detect truncated PDF417 barcodes without the end lines. Is there any swift library that can add this onto the end of the barcode by appending an image layer? I currently also have the corners of the barcode:

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {

    let barcodeScanner = BarcodeScanner.barcodeScanner(options: barcodeOptions)

   // print("Capturing", Date())

    let image = VisionImage(buffer: sampleBuffer)
    image.orientation = imageOrientation(
      deviceOrientation: UIDevice.current.orientation,
      cameraPosition: AVCaptureDevice.Position.front)

    guard let barcodes = try? barcodeScanner.results(in: image) else {return}

    for barcode in barcodes {
     let corners = barcode.cornerPoints

    print(corners)

      //let displayValue = barcode.displayValue
      let rawValue = barcode.rawValue

Corners: enter image description here

Truncated:

enter image description here

Non-Truncated

Non-truncated PDF417

Mike Schmidt
  • 945
  • 3
  • 12
  • 31

1 Answers1

1

One option would be to load the sampleBuffer into a CGContext and draw on top of that.

You can convert the CMSampleBuffer to a CGImage using the method described in this answer.

A CGImage can then be drawn into the CGContext. With the coordinates of the corners of the truncated code, you can compute the corners of the stop pattern by extending the line spanned by the top left and top right corner as well as the bottom left and bottom right corner.

CGContext has methods for drawing lines and paths.

On a CGContext you can use methods like move(to: point), addLine(to: point), closePath(), setFillColor(color), strokePath() and fillPath().

Once you're done with all drawing operations, use makeImage() to retrieve the output image.

You can then create the vision image from an UIImage, which you can get using UIImage(cgImage: contextOutput).

Palle
  • 11,511
  • 2
  • 40
  • 61