NCDocumentCamera.swift 3.1 KB

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