Add Rectangles Todo: fix rectangle high

This commit is contained in:
Simon Rieger 2023-12-15 20:58:20 +01:00
parent f4871473c4
commit b4ccf6315b

View file

@ -13,11 +13,23 @@ struct ContentView: View {
@State private var recognizedText: String = ""
@State private var isShowingPopup = false
@State private var matchedLines: [String] = []
@State private var textRectangles: [RectangleData] = []
var body: some View {
VStack {
CameraView(recognizedText: $recognizedText, isShowingPopup: $isShowingPopup, matchedLines: $matchedLines)
ZStack {
CameraView(recognizedText: $recognizedText, isShowingPopup: $isShowingPopup, matchedLines: $matchedLines, textRectangles: $textRectangles)
.edgesIgnoringSafeArea(.all)
// Overlay für die Rechtecke
ForEach(textRectangles) { rectangleData in
Rectangle()
.stroke(Color.green, lineWidth: 2)
.frame(width: rectangleData.rect.width, height: rectangleData.rect.height)
.position(x: rectangleData.rect.midX, y: rectangleData.rect.midY)
.rotationEffect(Angle(degrees: 270))
}
//Text("Recognized Text: \(recognizedText)")
// .padding()
}
.alert(isPresented: $isShowingPopup) {
Alert(
@ -31,10 +43,16 @@ struct ContentView: View {
}
}
struct RectangleData: Identifiable {
var id: UUID
var rect: CGRect
}
struct CameraView: UIViewControllerRepresentable {
@Binding var recognizedText: String
@Binding var isShowingPopup: Bool
@Binding var matchedLines: [String]
@Binding var textRectangles: [RectangleData]
class Coordinator: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {
var parent: CameraView
@ -57,8 +75,15 @@ struct CameraView: UIViewControllerRepresentable {
observation.topCandidates(1).first?.string
}.joined(separator: "\n")
let rectangles = results.map { observation in
let boundingBox = observation.boundingBox
let transformedRect = self.parent.transformRect(boundingBox)
return RectangleData(id: UUID(), rect: transformedRect)
}
DispatchQueue.main.async {
self.parent.recognizedText = text
self.parent.textRectangles = rectangles
print(text)
self.parent.checkForRegexMatch(text)
}
@ -140,4 +165,12 @@ struct CameraView: UIViewControllerRepresentable {
return 0.0
}
func transformRect(_ rect: CGRect) -> CGRect {
let previewSize = UIScreen.main.bounds.size
var transformedRect = VNImageRectForNormalizedRect(rect, Int(previewSize.width), Int(previewSize.height))
transformedRect.origin.x = previewSize.width - transformedRect.origin.x - transformedRect.size.width
return transformedRect
}
}