Add Rectangles Todo: fix rectangle high
This commit is contained in:
parent
f4871473c4
commit
b4ccf6315b
1 changed files with 48 additions and 15 deletions
|
@ -13,11 +13,23 @@ struct ContentView: View {
|
||||||
@State private var recognizedText: String = ""
|
@State private var recognizedText: String = ""
|
||||||
@State private var isShowingPopup = false
|
@State private var isShowingPopup = false
|
||||||
@State private var matchedLines: [String] = []
|
@State private var matchedLines: [String] = []
|
||||||
|
@State private var textRectangles: [RectangleData] = []
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
VStack {
|
ZStack {
|
||||||
CameraView(recognizedText: $recognizedText, isShowingPopup: $isShowingPopup, matchedLines: $matchedLines)
|
CameraView(recognizedText: $recognizedText, isShowingPopup: $isShowingPopup, matchedLines: $matchedLines, textRectangles: $textRectangles)
|
||||||
.edgesIgnoringSafeArea(.all)
|
.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(isPresented: $isShowingPopup) {
|
||||||
Alert(
|
Alert(
|
||||||
|
@ -31,10 +43,16 @@ struct ContentView: View {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct RectangleData: Identifiable {
|
||||||
|
var id: UUID
|
||||||
|
var rect: CGRect
|
||||||
|
}
|
||||||
|
|
||||||
struct CameraView: UIViewControllerRepresentable {
|
struct CameraView: UIViewControllerRepresentable {
|
||||||
@Binding var recognizedText: String
|
@Binding var recognizedText: String
|
||||||
@Binding var isShowingPopup: Bool
|
@Binding var isShowingPopup: Bool
|
||||||
@Binding var matchedLines: [String]
|
@Binding var matchedLines: [String]
|
||||||
|
@Binding var textRectangles: [RectangleData]
|
||||||
|
|
||||||
class Coordinator: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {
|
class Coordinator: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {
|
||||||
var parent: CameraView
|
var parent: CameraView
|
||||||
|
@ -57,8 +75,15 @@ struct CameraView: UIViewControllerRepresentable {
|
||||||
observation.topCandidates(1).first?.string
|
observation.topCandidates(1).first?.string
|
||||||
}.joined(separator: "\n")
|
}.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 {
|
DispatchQueue.main.async {
|
||||||
self.parent.recognizedText = text
|
self.parent.recognizedText = text
|
||||||
|
self.parent.textRectangles = rectangles
|
||||||
print(text)
|
print(text)
|
||||||
self.parent.checkForRegexMatch(text)
|
self.parent.checkForRegexMatch(text)
|
||||||
}
|
}
|
||||||
|
@ -140,4 +165,12 @@ struct CameraView: UIViewControllerRepresentable {
|
||||||
|
|
||||||
return 0.0
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue