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() }