https://github.com/hansemannn/ios11-qr-code-example
Example showing how to use the QR-code detection API (VNDetectBarcodesRequest) in iOS 11.
https://github.com/hansemannn/ios11-qr-code-example
ios11 qrcode vision
Last synced: 16 days ago
JSON representation
Example showing how to use the QR-code detection API (VNDetectBarcodesRequest) in iOS 11.
- Host: GitHub
- URL: https://github.com/hansemannn/ios11-qr-code-example
- Owner: hansemannn
- Created: 2017-06-09T17:44:58.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-01-13T18:57:23.000Z (over 7 years ago)
- Last Synced: 2025-03-29T01:12:32.509Z (about 1 month ago)
- Topics: ios11, qrcode, vision
- Language: Swift
- Size: 35.2 KB
- Stars: 86
- Watchers: 4
- Forks: 15
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 🔲 iOS11 QR-Code Example
A quick example showing how to use the `Vision` system-framework in iOS 11 and Swift 4.## Prerequisites
* Xcode 9 and later## Getting Started
First, import the `Vision` framework.
```swift
import Vision
```
Next, create a barcode-request that will call the completion-handler asynchronously when it detects a code:
```swift
// Create a barcode detection-request
let barcodeRequest = VNDetectBarcodesRequest(completionHandler: { request, error inguard 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)")
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(describing: content))")
print("Error-Correction-Level: \(desc.errorCorrectionLevel)")
print("Symbol-Version: \(desc.symbolVersion)")
}
}
}
})
```
Finally, call the image-request-handler with the previously create barcode-request:
```swift
// Create an image handler and use the CGImage your UIImage instance.
guard let image = myImage.cgImage else { return }
let handler = VNImageRequestHandler(cgImage: image, options: [:])// Perform the barcode-request. This will call the completion-handler of the barcode-request.
guard let _ = try? handler.perform([barcodeRequest]) else {
return print("Could not perform barcode-request!")
}
```
That's it! Run the app on the simulator / device and detect QR-codes.## Author
Hans Knöchel ([@hansemannnn](https://twitter.com/hansemannnn))