diff --git a/DocutainSDKExampleiOSSwift/Extensions.swift b/DocutainSDKExampleiOSSwift/Extensions.swift index 95732d3..0354ae2 100644 --- a/DocutainSDKExampleiOSSwift/Extensions.swift +++ b/DocutainSDKExampleiOSSwift/Extensions.swift @@ -12,6 +12,10 @@ internal extension String{ func localized() -> Self{ return NSLocalizedString(self, comment: "") } + + func localized(defaultValue: String) -> Self{ + return NSLocalizedString(self, tableName: nil, bundle: .main, value: defaultValue, comment: "") + } } internal extension NSObject { diff --git a/DocutainSDKExampleiOSSwift/ViewController.swift b/DocutainSDKExampleiOSSwift/ViewController.swift index a20cd18..6f9a453 100644 --- a/DocutainSDKExampleiOSSwift/ViewController.swift +++ b/DocutainSDKExampleiOSSwift/ViewController.swift @@ -20,7 +20,14 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour var itemType: ItemType } - let listItems : [ListItem] = [ListItem](arrayLiteral: ListItem(title: "title_document_scan".localized(), icon: "DocumentScanner", subtitle: "subtitle_document_scan".localized(), itemType: ItemType.documentScan), ListItem(title: "title_data_extraction".localized(), icon: "DataExtraction", subtitle: "subtitle_data_extraction".localized(), itemType: ItemType.dataExtraction), ListItem(title: "title_text_recognition".localized(), icon: "OCR", subtitle: "subtitle_text_recognition".localized(), itemType: ItemType.textRecognition), ListItem(title: "title_PDF_generating".localized(), icon: "PDF", subtitle: "subtitle_PDF_generating".localized(), itemType: ItemType.pdfGenerating), ListItem(title: "title_settings".localized(), icon: "Settings", subtitle: "subtitle_settings".localized(), itemType: ItemType.settings)) + let listItems : [ListItem] = [ + ListItem(title: "title_document_scan".localized(), icon: "DocumentScanner", subtitle: "subtitle_document_scan".localized(), itemType: ItemType.documentScan), + ListItem(title: "title_data_extraction".localized(), icon: "DataExtraction", subtitle: "subtitle_data_extraction".localized(), itemType: ItemType.dataExtraction), + ListItem(title: "title_text_recognition".localized(), icon: "OCR", subtitle: "subtitle_text_recognition".localized(), itemType: ItemType.textRecognition), + ListItem(title: "title_PDF_generating".localized(), icon: "PDF", subtitle: "subtitle_PDF_generating".localized(), itemType: ItemType.pdfGenerating), + ListItem(title: "title_image_export".localized(defaultValue: "Image Export"), icon: "DocumentScanner", subtitle: "subtitle_image_export".localized(defaultValue: "Export each scanned or imported document page as a JPG image file."), itemType: ItemType.imageExport), + ListItem(title: "title_settings".localized(), icon: "Settings", subtitle: "subtitle_settings".localized(), itemType: ItemType.settings) + ] let cellReuseIdentifier = "cell" let tableView = UITableView() @@ -31,6 +38,7 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour case dataExtraction case textRecognition case pdfGenerating + case imageExport case settings } @@ -150,6 +158,10 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour private func startPDFGenerating(){ showInputOptionAlert() } + + private func startImageExport(){ + showInputOptionAlert() + } private func showInputOptionAlert(){ let alert = UIAlertController(title: "Info", message: "input_option_message".localized(), preferredStyle: .actionSheet) @@ -211,6 +223,68 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour } } } + + private func exportImages(url: URL?){ + DispatchQueue.global(qos: .userInitiated).async { + if let documentUrl = url{ + //if an url is available it means we have imported a file. If so, we need to load it into the SDK first + if(!Document.loadFile(fileUrl: documentUrl)){ + let errorMessage = "Document.loadFile failed with error: \(DocutainSDK.getLastError())" + print(errorMessage) + self.showImageExportError(message: errorMessage) + return + } + } + + let pageCount = Document.pageCount() + if(pageCount == 0){ + let errorMessage = "No document pages available for image export." + print(errorMessage) + self.showImageExportError(message: errorMessage) + return + } + + let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) + let path = paths[0] + var exportedImages = [URL]() + + for page in 1...pageCount{ + let imageUrl = path.appendingPathComponent("DocutainSDK_Page_\(page).jpg") + try? FileManager.default.removeItem(at: imageUrl) + + if let savedImageUrl = Document.writeImage(page: page, fileUrl: imageUrl){ + exportedImages.append(savedImageUrl) + } else{ + let errorMessage = "Writing image file failed for page \(page), last error: \(DocutainSDK.getLastError())" + print(errorMessage) + self.showImageExportError(message: errorMessage) + return + } + } + + DispatchQueue.main.async { + self.presentShareSheet(urls: exportedImages) + } + } + } + + private func presentShareSheet(urls: [URL]){ + let activityViewController = UIActivityViewController(activityItems: urls, applicationActivities: nil) + if let popoverController = activityViewController.popoverPresentationController{ + popoverController.sourceView = self.view + popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0) + popoverController.permittedArrowDirections = [] + } + present(activityViewController, animated: true) + } + + private func showImageExportError(message: String){ + DispatchQueue.main.async { + let alert = UIAlertController(title: "Image export failed", message: message, preferredStyle: .alert) + alert.addAction(UIAlertAction(title: "OK", style: .default)) + self.present(alert, animated: true) + } + } private func openDataResultViewController(url: URL?){ navigationController!.pushViewController(ViewControllerDataResult(url: url), animated: true) @@ -224,6 +298,8 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour switch selectedOption{ case ItemType.pdfGenerating: generatePDF(url: url) + case ItemType.imageExport: + exportImages(url: url) case ItemType.dataExtraction: openDataResultViewController(url:url) case ItemType.textRecognition: @@ -268,6 +344,9 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour case ItemType.pdfGenerating: selectedOption = ItemType.pdfGenerating startPDFGenerating() + case ItemType.imageExport: + selectedOption = ItemType.imageExport + startImageExport() case ItemType.settings: selectedOption = ItemType.none navigationController!.pushViewController(ViewControllerSettings(), animated: true) @@ -282,6 +361,8 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour switch selectedOption{ case ItemType.pdfGenerating: generatePDF(url:nil) + case ItemType.imageExport: + exportImages(url:nil) case ItemType.dataExtraction: openDataResultViewController(url:nil) case ItemType.textRecognition: @@ -329,4 +410,3 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour } } } - diff --git a/DocutainSDKExampleiOSSwift/de.lproj/Localizable.strings b/DocutainSDKExampleiOSSwift/de.lproj/Localizable.strings index 9570aa1..7970ce9 100644 --- a/DocutainSDKExampleiOSSwift/de.lproj/Localizable.strings +++ b/DocutainSDKExampleiOSSwift/de.lproj/Localizable.strings @@ -14,6 +14,8 @@ "subtitle_text_recognition" = "Führen Sie OCR für das zuvor gescannte oder importierte Dokument (PDF oder Bild) aus und erhalten Sie den erkannten Text."; "title_PDF_generating" = "PDF Dokument erstellen"; "subtitle_PDF_generating" = "Generieren Sie aus den gescannten Seiten oder dem importierten Bild ein durchsuchbares PDF-Dokument (mit erkanntem Text)."; +"title_image_export" = "Bildexport"; +"subtitle_image_export" = "Exportieren Sie jede gescannte oder importierte Dokumentseite als JPG-Bilddatei."; "title_settings" = "Einstellungen"; "subtitle_settings" = "Ändern Sie Einstellungen bezüglich Farbe, Scanner und Editieren des Docutain SDK."; "input_option_message" = "Zunächst muss ein Dokument gescannt oder importiert werden."; diff --git a/DocutainSDKExampleiOSSwift/en.lproj/Localizable.strings b/DocutainSDKExampleiOSSwift/en.lproj/Localizable.strings index 36235bc..3a44dba 100644 --- a/DocutainSDKExampleiOSSwift/en.lproj/Localizable.strings +++ b/DocutainSDKExampleiOSSwift/en.lproj/Localizable.strings @@ -14,6 +14,8 @@ "subtitle_text_recognition" = "Run OCR on the previously scanned or imported (PDF or Image) document and get the recognized text."; "title_PDF_generating" = "Generate PDF Document"; "subtitle_PDF_generating" = "Generate a searchable (includes recognized text) PDF document out of the scanned pages or imported image."; +"title_image_export" = "Image Export"; +"subtitle_image_export" = "Export each scanned or imported document page as a JPG image file."; "title_settings" = "Settings"; "subtitle_settings" = "Configure color, scanner and editing of the Docutain SDK."; "input_option_message" = "First, scan or import a document.";