NCDocumentCamera.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 UIKit
  25. import Photos
  26. import VisionKit
  27. class NCDocumentCamera: NSObject, VNDocumentCameraViewControllerDelegate {
  28. static let shared: NCDocumentCamera = {
  29. let instance = NCDocumentCamera()
  30. return instance
  31. }()
  32. var viewController: UIViewController?
  33. let utilityFileSystem = NCUtilityFileSystem()
  34. func openScannerDocument(viewController: UIViewController?) {
  35. guard VNDocumentCameraViewController.isSupported else { return }
  36. self.viewController = viewController
  37. let controller = VNDocumentCameraViewController()
  38. controller.delegate = self
  39. viewController?.present(controller, animated: true)
  40. }
  41. func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) {
  42. for pageNumber in 0..<scan.pageCount {
  43. let fileName = utilityFileSystem.createFileName("scan.png", fileDate: Date(), fileType: PHAssetMediaType.image, notUseMask: 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 if let controller = self.viewController as? NCMainTabBarController {
  54. if let navigationController = UIStoryboard(name: "NCScan", bundle: nil).instantiateInitialViewController() {
  55. navigationController.modalPresentationStyle = UIModalPresentationStyle.pageSheet
  56. if let viewController = navigationController.topMostViewController() as? NCScan {
  57. viewController.serverUrl = controller.currentServerUrl()
  58. viewController.controller = controller
  59. }
  60. self.viewController?.present(navigationController, animated: true, completion: nil)
  61. }
  62. }
  63. }
  64. }
  65. func documentCameraViewControllerDidCancel(_ controller: VNDocumentCameraViewController) {
  66. controller.dismiss(animated: true, completion: nil)
  67. }
  68. }