NCDocumentCamera.swift 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //
  2. // NCDocumentCamera.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 03/01/23.
  6. // Copyright © 2023 Marino Faggiana. All rights reserved.
  7. //
  8. import Foundation
  9. import VisionKit
  10. class NCDocumentCamera: NSObject, VNDocumentCameraViewControllerDelegate {
  11. static let shared: NCDocumentCamera = {
  12. let instance = NCDocumentCamera()
  13. return instance
  14. }()
  15. var viewController: UIViewController?
  16. func openScannerDocument(viewController: UIViewController) {
  17. self.viewController = viewController
  18. guard VNDocumentCameraViewController.isSupported else { return }
  19. let controller = VNDocumentCameraViewController()
  20. controller.delegate = self
  21. self.viewController?.present(controller, animated: true)
  22. }
  23. func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) {
  24. for pageNumber in 0..<scan.pageCount {
  25. let fileName = CCUtility.createFileName("scan.png",
  26. fileDate: Date(),
  27. fileType: PHAssetMediaType.image,
  28. keyFileName: NCGlobal.shared.keyFileNameMask,
  29. keyFileNameType: NCGlobal.shared.keyFileNameType,
  30. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  31. forcedNewFileName: true)!
  32. let fileNamePath = CCUtility.getDirectoryScan() + "/" + fileName
  33. let image = scan.imageOfPage(at: pageNumber)
  34. do {
  35. try image.pngData()?.write(to: NSURL.fileURL(withPath: fileNamePath))
  36. } catch { }
  37. }
  38. controller.dismiss(animated: true) {
  39. if let viewController = self.viewController as? NCScan {
  40. viewController.loadImage()
  41. } else {
  42. let storyboard = UIStoryboard(name: "NCScan", bundle: nil)
  43. let controller = storyboard.instantiateInitialViewController()!
  44. controller.modalPresentationStyle = UIModalPresentationStyle.pageSheet
  45. self.viewController?.present(controller, animated: true, completion: nil)
  46. }
  47. }
  48. }
  49. func documentCameraViewControllerDidCancel(_ controller: VNDocumentCameraViewController) {
  50. controller.dismiss(animated: true, completion: nil)
  51. }
  52. }