The following piece of Swift code is using the new iOS11 Vision framework to analyze an image and find QR codes within it.
let barcodeRequest = VNDetectBarcodesRequest(completionHandler {(request, error) in
for result in request.results! {
if let barcode = result as? VNBarcodeObservation {
if let desc = barcode.barcodeDescriptor as? CIQRCodeDescriptor {
let content = String(data: desc.errorCorrectedPayload, encoding: .isoLatin1)
print(content) //Prints garbage
}
}
}
}
let image = //some image with QR code...
let handler = VNImageRequestHandler(cgImage: image, options: [.properties : ""])
try handler.perform([barcodeRequest])
However, the problem is that the desc.errorCorrectedPayload
returns the raw encoded data as it has been read from the QR code.
In order to get a printable content string from the descriptor one must decode this raw data (e.g. determine the mode from the first 4 bits).
It gets even more interesting because Apple already has code for decoding raw data in the AVFoundation. The AVMetadataMachineReadableCodeObject
class already has the .stringValue
field which returns the decoded string.
Is it possible to access this decoding code and use it in Vision framework too?
It seems that now you can get a decoded string from a barcode using new payloadStringValue property of VNBarcodeObservation
introduced in iOS 11 beta 5.
if let payload = barcodeObservation.payloadStringValue {
print("payload is \(payload)")
}