NCDocumentCamera.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import VisionKit
  25. class NCDocumentCamera: NSObject, VNDocumentCameraViewControllerDelegate {
  26. static let shared: NCDocumentCamera = {
  27. let instance = NCDocumentCamera()
  28. return instance
  29. }()
  30. var viewController: UIViewController?
  31. let utilityFileSystem = NCUtilityFileSystem()
  32. let appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
  33. func openScannerDocument(viewController: UIViewController?) {
  34. guard VNDocumentCameraViewController.isSupported else { return }
  35. self.viewController = viewController
  36. let controller = VNDocumentCameraViewController()
  37. controller.delegate = self
  38. viewController?.present(controller, animated: true)
  39. }
  40. func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) {
  41. for pageNumber in 0..<scan.pageCount {
  42. let fileName = CCUtility.createFileName("scan.png",
  43. fileDate: Date(),
  44. fileType: PHAssetMediaType.image,
  45. keyFileName: NCGlobal.shared.keyFileNameMask,
  46. keyFileNameType: NCGlobal.shared.keyFileNameType,
  47. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  48. forcedNewFileName: true)!
  49. let fileNamePath = utilityFileSystem.directoryScan + "/" + fileName
  50. let image = scan.imageOfPage(at: pageNumber)
  51. do {
  52. try image.pngData()?.write(to: NSURL.fileURL(withPath: fileNamePath))
  53. } catch { }
  54. }
  55. controller.dismiss(animated: true) {
  56. if let viewController = self.viewController as? NCScan {
  57. viewController.loadImage()
  58. } else if let mainTabBarController = self.viewController as? NCMainTabBarController {
  59. if let navigationController = UIStoryboard(name: "NCScan", bundle: nil).instantiateInitialViewController() {
  60. navigationController.modalPresentationStyle = UIModalPresentationStyle.pageSheet
  61. if let viewController = navigationController.topMostViewController() as? NCScan {
  62. viewController.serverUrl = mainTabBarController.currentServerUrl()
  63. }
  64. self.viewController?.present(navigationController, animated: true, completion: nil)
  65. }
  66. }
  67. }
  68. }
  69. func documentCameraViewControllerDidCancel(_ controller: VNDocumentCameraViewController) {
  70. controller.dismiss(animated: true, completion: nil)
  71. }
  72. }