【iOS开发】相册选择图片识别条形码
原生识别条形码
VNDetectBarcodesRequest 只要iOS11.0+以上支持,支持iOS11.0-以下请使用其他方案
// Create a barcode detection-request
let barcodeRequest = VNDetectBarcodesRequest(completionHandler: {
request, error in
guard let results = request.results else {
return }
// Loopm through the found results
for result in results {
// Cast the result to a barcode-observation
if let barcode = result as? VNBarcodeObservation {
// Print barcode-values
print("Symbology: \(barcode.symbology.rawValue)")
// Print barcode string
if let payload = barcode.payloadStringValue {
print("Payload: \(payload)")
}
if let desc = barcode.barcodeDescriptor as? CIQRCodeDescriptor {
let content = String(data: desc.errorCorrectedPayload, encoding: .utf8)
// FIXME: This currently returns nil. I did not find any docs on how to encode the data properly so far.
print("Payload: \(String