From 56a8f07a9f9dfafe64061024b26a9af9330ff5e3 Mon Sep 17 00:00:00 2001 From: Simon Rieger Date: Fri, 15 Dec 2023 17:20:09 +0100 Subject: [PATCH] Add Camera Support and support for use fotos --- .../project.pbxproj | 2 + intelliScan-analytic-engine/ContentView.swift | 112 +++++++++++++++++- 2 files changed, 109 insertions(+), 5 deletions(-) diff --git a/intelliScan-analytic-engine.xcodeproj/project.pbxproj b/intelliScan-analytic-engine.xcodeproj/project.pbxproj index 01a3bd3..f9cf7e4 100644 --- a/intelliScan-analytic-engine.xcodeproj/project.pbxproj +++ b/intelliScan-analytic-engine.xcodeproj/project.pbxproj @@ -424,6 +424,7 @@ ENABLE_HARDENED_RUNTIME = YES; ENABLE_PREVIEWS = YES; GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_KEY_NSCameraUsageDescription = "Wir benötigen Zugriff auf Ihre Kamera, um Bilder für die Texterkennung aufzunehmen."; "INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES; "INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphonesimulator*]" = YES; "INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents[sdk=iphoneos*]" = YES; @@ -462,6 +463,7 @@ ENABLE_HARDENED_RUNTIME = YES; ENABLE_PREVIEWS = YES; GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_KEY_NSCameraUsageDescription = "Wir benötigen Zugriff auf Ihre Kamera, um Bilder für die Texterkennung aufzunehmen."; "INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES; "INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphonesimulator*]" = YES; "INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents[sdk=iphoneos*]" = YES; diff --git a/intelliScan-analytic-engine/ContentView.swift b/intelliScan-analytic-engine/ContentView.swift index 7b218ea..bdb51e6 100644 --- a/intelliScan-analytic-engine/ContentView.swift +++ b/intelliScan-analytic-engine/ContentView.swift @@ -6,19 +6,121 @@ // import SwiftUI +import Vision struct ContentView: View { + @State private var recognizedText = "" + @State private var showingImagePicker = false + @State private var showingCamera = false + @State private var selectedImage: UIImage? + var body: some View { VStack { - Image(systemName: "globe") - .imageScale(.large) - .foregroundStyle(.tint) - Text("Hello, world!") + Button("Bild auswählen") { + self.showingImagePicker.toggle() + } + .sheet(isPresented: $showingImagePicker, onDismiss: recognizeText) { + ImagePicker(selectedImage: self.$selectedImage, sourceType: .photoLibrary) + } + + Button("Kamera verwenden") { + self.showingCamera.toggle() + } + .sheet(isPresented: $showingCamera, onDismiss: recognizeText) { + ImagePicker(selectedImage: self.$selectedImage, sourceType: .camera) + } + + if let selectedImage = selectedImage { + Image(uiImage: selectedImage) + .resizable() + .scaledToFit() + .frame(width: 200, height: 200) + .padding() + + Text("Erkannter Text:") + Text(recognizedText) + .padding() + } + } + } + + func recognizeText() { + guard let selectedImage = selectedImage, let cgImage = selectedImage.cgImage else { + return + } + + let request = VNRecognizeTextRequest(completionHandler: { (request, error) in + guard let observations = request.results as? [VNRecognizedTextObservation] else { return } + + var recognizedText = "" + for observation in observations { + guard let topCandidate = observation.topCandidates(1).first else { continue } + recognizedText += topCandidate.string + "\n" + } + + self.recognizedText = recognizedText + }) + + let requestHandler = VNImageRequestHandler(cgImage: cgImage, options: [:]) + + do { + try requestHandler.perform([request]) + } catch { + print("Error performing OCR: \(error)") } - .padding() } } +struct ContentView_Previews: PreviewProvider { + static var previews: some View { + ContentView() + } +} + +struct ImagePicker: UIViewControllerRepresentable { + @Binding var selectedImage: UIImage? + var sourceType: UIImagePickerController.SourceType + + class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate { + let parent: ImagePicker + + init(parent: ImagePicker) { + self.parent = parent + } + + func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { + if let uiImage = info[.originalImage] as? UIImage { + parent.selectedImage = uiImage + } + + parent.presentationMode.wrappedValue.dismiss() + } + } + + var imagePickerController: UIImagePickerController + @Environment(\.presentationMode) var presentationMode + + init(selectedImage: Binding, sourceType: UIImagePickerController.SourceType) { + _selectedImage = selectedImage + self.sourceType = sourceType + imagePickerController = UIImagePickerController() + imagePickerController.sourceType = sourceType + imagePickerController.allowsEditing = false + } + + func makeUIViewController(context: Context) -> UIImagePickerController { + imagePickerController.delegate = context.coordinator + return imagePickerController + } + + func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {} + + func makeCoordinator() -> Coordinator { + return Coordinator(parent: self) + } +} + + #Preview { ContentView() }