Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP ID & Card Vault7// File : SPVault/Features/Scanner/DocumentCameraView.swift8// Module : SPVault9// Repository : https://git.spboucher.ai/sp-id-card-vault10// Project Website : Not specified11// Created : 2026-12-0212// Last Modified : 2026-12-0213//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import SwiftUI42import UIKit43import VisionKit4445/// "Document mode": VisionKit's document camera as an alternative way to46/// capture a side (useful for passports and paper documents). The scanned page47/// is handed to the same SmartScan pipeline as an in-memory image; VisionKit48/// does not save it anywhere.49struct DocumentCameraView: UIViewControllerRepresentable {50 static var isSupported: Bool { VNDocumentCameraViewController.isSupported }5152 var onScan: (CapturedStill) -> Void53 var onCancel: () -> Void5455 func makeCoordinator() -> Coordinator { Coordinator(onScan: onScan, onCancel: onCancel) }5657 func makeUIViewController(context: Context) -> VNDocumentCameraViewController {58 let controller = VNDocumentCameraViewController()59 controller.delegate = context.coordinator60 return controller61 }6263 func updateUIViewController(_ controller: VNDocumentCameraViewController, context: Context) {}6465 final class Coordinator: NSObject, VNDocumentCameraViewControllerDelegate {66 let onScan: (CapturedStill) -> Void67 let onCancel: () -> Void6869 init(onScan: @escaping (CapturedStill) -> Void, onCancel: @escaping () -> Void) {70 self.onScan = onScan71 self.onCancel = onCancel72 }7374 func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) {75 guard scan.pageCount > 0 else { onCancel(); return }76 let page = scan.imageOfPage(at: 0)77 guard let image = page.cgImage else { onCancel(); return }78 let orientation = CGImagePropertyOrientation(page.imageOrientation)79 onScan(CapturedStill(image: ImageAdjustments.limited(ImageAdjustments.upright(image, orientation: orientation), longEdge: 4032)))80 }8182 func documentCameraViewControllerDidCancel(_ controller: VNDocumentCameraViewController) { onCancel() }8384 func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFailWithError error: Error) { onCancel() }85 }86}8788extension CGImagePropertyOrientation {89 init(_ orientation: UIImage.Orientation) {90 switch orientation {91 case .up: self = .up92 case .down: self = .down93 case .left: self = .left94 case .right: self = .right95 case .upMirrored: self = .upMirrored96 case .downMirrored: self = .downMirrored97 case .leftMirrored: self = .leftMirrored98 case .rightMirrored: self = .rightMirrored99 @unknown default: self = .up100 }101 }102}103