Repository Public License
1// ORIX Scan2// File: App/Sources/Capture/ScannerView.swift3// Description: The system document camera (VisionKit): automatic edge detection, multi-page capture, flash, retake, perspective correction.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import ORIXKit8import SwiftUI9import VisionKit1011struct ScannerView: UIViewControllerRepresentable {12 /// False on devices without a camera (and in the Simulator).13 static var isAvailable: Bool { VNDocumentCameraViewController.isSupported }1415 var onFinish: ([Incoming]) -> Void16 var onCancel: () -> Void17 var onError: (Error) -> Void1819 func makeCoordinator() -> Coordinator { Coordinator(self) }2021 func makeUIViewController(context: Context) -> VNDocumentCameraViewController {22 let controller = VNDocumentCameraViewController()23 controller.delegate = context.coordinator24 return controller25 }2627 func updateUIViewController(_ controller: VNDocumentCameraViewController, context: Context) {}2829 final class Coordinator: NSObject, VNDocumentCameraViewControllerDelegate {30 let parent: ScannerView31 init(_ parent: ScannerView) { self.parent = parent }3233 func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) {34 var items: [Incoming] = []35 for index in 0..<scan.pageCount {36 autoreleasepool {37 // High quality JPEG of the capture; this file is the original every later edit starts from.38 if let data = scan.imageOfPage(at: index).jpegData(compressionQuality: 0.95) {39 items.append(.image(data: data, fileExtension: "jpg"))40 }41 }42 }43 parent.onFinish(items)44 }4546 func documentCameraViewControllerDidCancel(_ controller: VNDocumentCameraViewController) { parent.onCancel() }4748 func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFailWithError error: Error) { parent.onError(error) }49 }50}51