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. let appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
  35. func openScannerDocument(viewController: UIViewController?) {
  36. guard VNDocumentCameraViewController.isSupported else { return }
  37. self.viewController = viewController
  38. let controller = VNDocumentCameraViewController()
  39. controller.delegate = self
  40. viewController?.present(controller, animated: true)
  41. }
  42. func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) {
  43. for pageNumber in 0..<scan.pageCount {
  44. let fileName = utilityFileSystem.createFileName("scan.png", fileDate: Date(), fileType: PHAssetMediaType.image, notUseMask: true)
  45. let fileNamePath = utilityFileSystem.directoryScan + "/" + fileName
  46. let image = scan.imageOfPage(at: pageNumber)
  47. do {
  48. try image.pngData()?.write(to: NSURL.fileURL(withPath: fileNamePath))
  49. } catch { }
  50. }
  51. controller.dismiss(animated: true) {
  52. if let viewController = self.viewController as? NCScan {
  53. viewController.loadImage()
  54. } else if let controller = self.viewController as? NCMainTabBarController {
  55. if let navigationController = UIStoryboard(name: "NCScan", bundle: nil).instantiateInitialViewController() {
  56. navigationController.modalPresentationStyle = UIModalPresentationStyle.pageSheet
  57. if let viewController = navigationController.topMostViewController() as? NCScan {
  58. viewController.serverUrl = controller.currentServerUrl()
  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. }