Add Alert Popup

This commit is contained in:
Simon Rieger 2023-12-15 19:27:29 +01:00
parent 58ea7b8f48
commit f4871473c4
2 changed files with 76 additions and 23 deletions

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
uuid = "DBED4980-D697-4718-A1D1-091F6419F495"
type = "1"
version = "2.0">
</Bucket>

View file

@ -4,26 +4,37 @@
//
// Created by Simon Rieger on 15.12.23.
//
import SwiftUI
import AVFoundation
import Vision
struct ContentView: View {
@State private var recognizedText: String = ""
@State private var isShowingPopup = false
@State private var matchedLines: [String] = []
var body: some View {
VStack {
CameraView(recognizedText: $recognizedText)
.edgesIgnoringSafeArea(.all)
Text("Recognized Text: \(recognizedText)")
.padding()
VStack {
CameraView(recognizedText: $recognizedText, isShowingPopup: $isShowingPopup, matchedLines: $matchedLines)
.edgesIgnoringSafeArea(.all)
}
.alert(isPresented: $isShowingPopup) {
Alert(
title: Text("Text Match Found"),
message: Text("Found matching text: \(matchedLines.joined(separator: "\n"))"),
dismissButton: .default(Text("OK")) {
// Hier kannst du zusätzlichen Code für das Schließen des Popups hinzufügen
}
)
}
}
}
}
struct CameraView: UIViewControllerRepresentable {
@Binding var recognizedText: String
@Binding var isShowingPopup: Bool
@Binding var matchedLines: [String]
class Coordinator: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {
var parent: CameraView
@ -48,6 +59,8 @@ struct CameraView: UIViewControllerRepresentable {
DispatchQueue.main.async {
self.parent.recognizedText = text
print(text)
self.parent.checkForRegexMatch(text)
}
}
}
@ -93,4 +106,38 @@ struct CameraView: UIViewControllerRepresentable {
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
func checkForRegexMatch(_ text: String) {
// Hier kannst du dein eigenes Regex-Muster einfügen
let regexPattern = "[\\-öÖäÄüÜß.a-zA-Z\\s]{8,50}[\\d]{1,3}"
let lines = text.components(separatedBy: .newlines)
matchedLines = lines.filter { line in
if let matchPercentage = percentageMatch(text: line, pattern: regexPattern), matchPercentage >= 60.0 {
print("Found matching text: \(line) with \(matchPercentage)% confidence")
return true
}
return false
}
isShowingPopup = !matchedLines.isEmpty
}
func percentageMatch(text: String, pattern: String) -> Double? {
guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else {
return nil
}
let range = NSRange(location: 0, length: text.utf16.count)
if let match = regex.firstMatch(in: text, options: [], range: range) {
let matchLength = match.range.length
let textLength = text.utf16.count
let matchPercentage = Double(matchLength) / Double(textLength) * 100.0
print("\(text) Match Percentage: \(matchPercentage)%")
return matchPercentage
}
return 0.0
}
}