SPB Git

Repository Public

DocumentScanner.swift 5.6 KB 126 lines swift

1// SP Notes iOS2// File: Features/Attachments/DocumentScanner.swift3// Description: Document scanner (VisionKit) returning the scanned pages as JPEG temporary files, and on-device text recognition4//              (Vision) used for scans, pictures and handwriting.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import SwiftUI9import UIKit10import Vision11import VisionKit1213/// The system document camera. Check `isSupported` first (false on the simulator and on devices without a suitable camera).14struct DocumentScanner: UIViewControllerRepresentable {15    static var isSupported: Bool { VNDocumentCameraViewController.isSupported }1617    /// The scanned pages in order, as JPEG temporary files named `<title> - <n>.jpg`. Empty when the user cancelled.18    var onFinish: @MainActor ([URL]) -> Void19    var onError: @MainActor (Error) -> Void = { _ in }2021    func makeCoordinator() -> Coordinator { Coordinator(onFinish: onFinish, onError: onError) }2223    func makeUIViewController(context: Context) -> VNDocumentCameraViewController {24        let controller = VNDocumentCameraViewController()25        controller.delegate = context.coordinator26        return controller27    }2829    func updateUIViewController(_ controller: VNDocumentCameraViewController, context: Context) {30        context.coordinator.onFinish = onFinish31        context.coordinator.onError = onError32    }3334    @MainActor35    final class Coordinator: NSObject, @preconcurrency VNDocumentCameraViewControllerDelegate {36        var onFinish: @MainActor ([URL]) -> Void37        var onError: @MainActor (Error) -> Void38        init(onFinish: @escaping @MainActor ([URL]) -> Void, onError: @escaping @MainActor (Error) -> Void) {39            self.onFinish = onFinish; self.onError = onError40        }4142        func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) {43            let title = scan.title.isEmpty ? String(localized: "Numérisation") : scan.title44            var urls: [URL] = []45            for index in 0..<scan.pageCount {46                // One page at a time so the bitmaps of a long scan are not all alive together.47                autoreleasepool {48                    let name = scan.pageCount == 1 ? "\(title).jpg" : "\(title) - \(index + 1).jpg"49                    if let data = scan.imageOfPage(at: index).jpegData(compressionQuality: 0.85), let url = try? MediaTemp.write(data, named: name) { urls.append(url) }50                }51            }52            onFinish(urls)53        }5455        func documentCameraViewControllerDidCancel(_ controller: VNDocumentCameraViewController) { onFinish([]) }5657        func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFailWithError error: Error) { onError(error) }58    }59}6061/// On-device text recognition. Runs off the main thread; never throws: an image without readable text gives an empty string.62enum TextRecognizer {63    static let preferredLanguages = ["fr-CA", "fr-FR", "en-US"]6465    /// The recognized text of a picture, lines joined by line feeds, top to bottom.66    static func recognizeText(in image: UIImage) async -> String {67        guard let cgImage = image.cgImage ?? Self.rasterize(image) else { return "" }68        let orientation = CGImagePropertyOrientation(image.imageOrientation)69        return await Task.detached(priority: .userInitiated) { perform(VNImageRequestHandler(cgImage: cgImage, orientation: orientation)) }.value70    }7172    /// The recognized text of an image file (decoded by Vision, with its EXIF orientation).73    static func recognizeText(at url: URL) async -> String {74        await Task.detached(priority: .userInitiated) { perform(VNImageRequestHandler(url: url)) }.value75    }7677    /// Several pages (a scan): the texts in order, separated by blank lines.78    static func recognizeText(atPages urls: [URL]) async -> String {79        var parts: [String] = []80        for url in urls {81            let text = await recognizeText(at: url)82            if !text.isEmpty { parts.append(text) }83        }84        return parts.joined(separator: "\n\n")85    }8687    private static func perform(_ handler: VNImageRequestHandler) -> String {88        let request = VNRecognizeTextRequest()89        request.recognitionLevel = .accurate90        request.usesLanguageCorrection = true91        if let supported = try? request.supportedRecognitionLanguages() {92            let languages = preferredLanguages.filter(supported.contains)93            if !languages.isEmpty { request.recognitionLanguages = languages }94        }95        do { try handler.perform([request]) } catch { return "" }96        return (request.results ?? []).compactMap { $0.topCandidates(1).first?.string }.joined(separator: "\n")97    }9899    private static func rasterize(_ image: UIImage) -> CGImage? {100        guard image.size.width > 0, image.size.height > 0 else { return nil }101        let format = UIGraphicsImageRendererFormat()102        format.scale = image.scale103        format.opaque = true104        return UIGraphicsImageRenderer(size: image.size, format: format).image { context in105            UIColor.white.setFill(); context.fill(CGRect(origin: .zero, size: image.size))106            image.draw(at: .zero)107        }.cgImage108    }109}110111private extension CGImagePropertyOrientation {112    init(_ orientation: UIImage.Orientation) {113        switch orientation {114        case .up: self = .up115        case .down: self = .down116        case .left: self = .left117        case .right: self = .right118        case .upMirrored: self = .upMirrored119        case .downMirrored: self = .downMirrored120        case .leftMirrored: self = .leftMirrored121        case .rightMirrored: self = .rightMirrored122        @unknown default: self = .up123        }124    }125}126
Files main
Menu

Theme