Add Alert Popup
This commit is contained in:
parent
58ea7b8f48
commit
f4871473c4
2 changed files with 76 additions and 23 deletions
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<Bucket
|
||||||
|
uuid = "DBED4980-D697-4718-A1D1-091F6419F495"
|
||||||
|
type = "1"
|
||||||
|
version = "2.0">
|
||||||
|
</Bucket>
|
|
@ -4,54 +4,67 @@
|
||||||
//
|
//
|
||||||
// Created by Simon Rieger on 15.12.23.
|
// Created by Simon Rieger on 15.12.23.
|
||||||
//
|
//
|
||||||
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
import AVFoundation
|
import AVFoundation
|
||||||
import Vision
|
import Vision
|
||||||
|
|
||||||
struct ContentView: View {
|
struct ContentView: View {
|
||||||
@State private var recognizedText: String = ""
|
@State private var recognizedText: String = ""
|
||||||
|
@State private var isShowingPopup = false
|
||||||
|
@State private var matchedLines: [String] = []
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
VStack {
|
VStack {
|
||||||
CameraView(recognizedText: $recognizedText)
|
CameraView(recognizedText: $recognizedText, isShowingPopup: $isShowingPopup, matchedLines: $matchedLines)
|
||||||
.edgesIgnoringSafeArea(.all)
|
.edgesIgnoringSafeArea(.all)
|
||||||
|
}
|
||||||
Text("Recognized Text: \(recognizedText)")
|
.alert(isPresented: $isShowingPopup) {
|
||||||
.padding()
|
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 {
|
struct CameraView: UIViewControllerRepresentable {
|
||||||
@Binding var recognizedText: String
|
@Binding var recognizedText: String
|
||||||
|
@Binding var isShowingPopup: Bool
|
||||||
|
@Binding var matchedLines: [String]
|
||||||
|
|
||||||
class Coordinator: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {
|
class Coordinator: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {
|
||||||
var parent: CameraView
|
var parent: CameraView
|
||||||
|
|
||||||
init(parent: CameraView) {
|
init(parent: CameraView) {
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
}
|
}
|
||||||
|
|
||||||
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
|
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
|
||||||
guard let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
|
guard let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
|
||||||
|
|
||||||
let request = VNRecognizeTextRequest { (request, error) in
|
let request = VNRecognizeTextRequest { (request, error) in
|
||||||
if let error = error {
|
if let error = error {
|
||||||
print("Error recognizing text: \(error)")
|
print("Error recognizing text: \(error)")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if let results = request.results as? [VNRecognizedTextObservation] {
|
if let results = request.results as? [VNRecognizedTextObservation] {
|
||||||
let text = results.compactMap { observation in
|
let text = results.compactMap { observation in
|
||||||
observation.topCandidates(1).first?.string
|
observation.topCandidates(1).first?.string
|
||||||
}.joined(separator: "\n")
|
}.joined(separator: "\n")
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.parent.recognizedText = text
|
self.parent.recognizedText = text
|
||||||
|
print(text)
|
||||||
|
self.parent.checkForRegexMatch(text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let handler = VNImageRequestHandler(cvPixelBuffer: imageBuffer, options: [:])
|
let handler = VNImageRequestHandler(cvPixelBuffer: imageBuffer, options: [:])
|
||||||
do {
|
do {
|
||||||
try handler.perform([request])
|
try handler.perform([request])
|
||||||
|
@ -60,15 +73,15 @@ struct CameraView: UIViewControllerRepresentable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeCoordinator() -> Coordinator {
|
func makeCoordinator() -> Coordinator {
|
||||||
return Coordinator(parent: self)
|
return Coordinator(parent: self)
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeUIViewController(context: Context) -> UIViewController {
|
func makeUIViewController(context: Context) -> UIViewController {
|
||||||
let viewController = UIViewController()
|
let viewController = UIViewController()
|
||||||
let captureSession = AVCaptureSession()
|
let captureSession = AVCaptureSession()
|
||||||
|
|
||||||
guard let camera = AVCaptureDevice.default(for: .video) else { return viewController }
|
guard let camera = AVCaptureDevice.default(for: .video) else { return viewController }
|
||||||
do {
|
do {
|
||||||
let input = try AVCaptureDeviceInput(device: camera)
|
let input = try AVCaptureDeviceInput(device: camera)
|
||||||
|
@ -77,20 +90,54 @@ struct CameraView: UIViewControllerRepresentable {
|
||||||
print("Error setting up camera input: \(error)")
|
print("Error setting up camera input: \(error)")
|
||||||
return viewController
|
return viewController
|
||||||
}
|
}
|
||||||
|
|
||||||
let output = AVCaptureVideoDataOutput()
|
let output = AVCaptureVideoDataOutput()
|
||||||
output.setSampleBufferDelegate(context.coordinator, queue: DispatchQueue(label: "cameraQueue"))
|
output.setSampleBufferDelegate(context.coordinator, queue: DispatchQueue(label: "cameraQueue"))
|
||||||
captureSession.addOutput(output)
|
captureSession.addOutput(output)
|
||||||
|
|
||||||
let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
|
let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
|
||||||
previewLayer.frame = viewController.view.layer.bounds
|
previewLayer.frame = viewController.view.layer.bounds
|
||||||
previewLayer.videoGravity = .resizeAspectFill
|
previewLayer.videoGravity = .resizeAspectFill
|
||||||
viewController.view.layer.addSublayer(previewLayer)
|
viewController.view.layer.addSublayer(previewLayer)
|
||||||
|
|
||||||
captureSession.startRunning()
|
captureSession.startRunning()
|
||||||
|
|
||||||
return viewController
|
return viewController
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue