NCDocumentCamera.swift 3.2 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. func openScannerDocument(viewController: UIViewController) {
  33. self.viewController = viewController
  34. guard VNDocumentCameraViewController.isSupported else { return }
  35. let controller = VNDocumentCameraViewController()
  36. controller.delegate = self
  37. self.viewController?.present(controller, animated: true)
  38. }
  39. func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) {
  40. for pageNumber in 0..<scan.pageCount {
  41. let fileName = CCUtility.createFileName("scan.png",
  42. fileDate: Date(),
  43. fileType: PHAssetMediaType.image,
  44. keyFileName: NCGlobal.shared.keyFileNameMask,
  45. keyFileNameType: NCGlobal.shared.keyFileNameType,
  46. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  47. forcedNewFileName: true)!
  48. let fileNamePath = utilityFileSystem.directoryScan + "/" + fileName
  49. let image = scan.imageOfPage(at: pageNumber)
  50. do {
  51. try image.pngData()?.write(to: NSURL.fileURL(withPath: fileNamePath))
  52. } catch { }
  53. }
  54. controller.dismiss(animated: true) {
  55. if let viewController = self.viewController as? NCScan {
  56. viewController.loadImage()
  57. } else {
  58. let storyboard = UIStoryboard(name: "NCScan", bundle: nil)
  59. let controller = storyboard.instantiateInitialViewController()!
  60. controller.modalPresentationStyle = UIModalPresentationStyle.pageSheet
  61. self.viewController?.present(controller, animated: true, completion: nil)
  62. }
  63. }
  64. }
  65. func documentCameraViewControllerDidCancel(_ controller: VNDocumentCameraViewController) {
  66. controller.dismiss(animated: true, completion: nil)
  67. }
  68. }