This sample code demonstrates how to use DataScannerViewController in your iOS app to easily scan text and barcodes using VisionKit in SwiftUI.
To use DataScannerViewController in your app, follow these steps:
- Clone or download the sample code from this repository.
- Copy the
DocumentScannerView.swift
file into your Xcode project.
To use DocumentScannerView in your app, create an instance of the class and present it from your view controller:
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
Text("Scan a document")
.font(.title)
.padding()
DocumentScannerView()
.navigationBarTitle("")
.navigationBarHidden(true)
}
}
}
}
You can customize the recognized data types, quality level, and other settings to suit your needs.
Once the scanner is presented, you can read the scanned content from the processItem
function:
func processItem(_ item: DataScannerViewController.Item) {
switch item {
case .text(let text):
print("Scanned text: \(text)")
case .barcode(let barcode):
print("Scanned barcode: \(barcode)")
}
}
You can also add customizations to the overlay view to show additional overlays over detected texts or barcodes:
scannerViewController.overlayContainerView.addSubview(customOverlayView)
This sample code requires Xcode 12 or later and a minimum deployment target of iOS 14 or later.
This sample code is available under the MIT license. See the LICENSE file for more information.