Created
April 25, 2023 12:38
-
-
Save doganaysahins/88541a4286f5db4c27de2984fa8f7098 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // ContentView.swift | |
| // ScanTextApp | |
| // | |
| // Created by Doganay on 25.04.2023. | |
| // | |
| import UIKit | |
| import SwiftUI | |
| import VisionKit | |
| struct ContentView: View { | |
| @State private var showCameraScannerView = false | |
| @State private var scanResults: String = "" | |
| @FocusState var focusKeyboard : Bool | |
| @State private var keywords: String = "" | |
| var body: some View { | |
| VStack { | |
| TextField("Input here", text: $keywords) | |
| .focused($focusKeyboard) | |
| .toolbar{ | |
| ToolbarItemGroup(placement: .keyboard) { | |
| Button { | |
| self.showCameraScannerView.toggle() | |
| } label: { | |
| Text("Scan") | |
| } | |
| } | |
| } | |
| Button { | |
| self.showCameraScannerView.toggle() | |
| } label: { | |
| Image(systemName: "camera") | |
| .bold() | |
| .padding() | |
| } | |
| .buttonStyle(.borderedProminent) | |
| .padding() | |
| Button { | |
| focusKeyboard.toggle() | |
| } label: { | |
| Text("Focus to keyboard") | |
| } | |
| .buttonStyle(.borderedProminent) | |
| .padding() | |
| } | |
| .sheet(isPresented: $showCameraScannerView) { | |
| CameraScanner(startScanning: $showCameraScannerView, scanResult: $keywords) | |
| .presentationDetents([.medium]) | |
| } | |
| .padding() | |
| } | |
| } | |
| struct ContentView_Previews: PreviewProvider { | |
| static var previews: some View { | |
| ContentView() | |
| } | |
| } | |
| struct CameraScanner: View { | |
| @Binding var startScanning: Bool | |
| @Binding var scanResult: String | |
| @Environment(\.presentationMode) var presentationMode | |
| var body: some View { | |
| NavigationView { | |
| CameraScannerViewController(startScanning: $startScanning, scanResult: $scanResult) | |
| .toolbar { | |
| ToolbarItem(placement: .navigationBarTrailing) { | |
| Button { | |
| self.presentationMode.wrappedValue.dismiss() | |
| } label: { | |
| HStack{ | |
| Text("Done") | |
| .bold() | |
| }.foregroundColor(.green) | |
| } | |
| } | |
| } | |
| .ignoresSafeArea() | |
| } | |
| } | |
| } | |
| //struct CameraScanner_Previews: PreviewProvider { | |
| // static var previews: some View { | |
| // CameraScanner(startScanning: .constant(true), scanResult: .constant("")) | |
| // } | |
| //} | |
| struct CameraScannerViewController: UIViewControllerRepresentable { | |
| @Binding var startScanning: Bool | |
| @Binding var scanResult: String | |
| func makeCoordinator() -> Coordinator { | |
| Coordinator(self) | |
| } | |
| func makeUIViewController(context: Context) -> DataScannerViewController { | |
| let viewController = DataScannerViewController( | |
| recognizedDataTypes: [.text()], | |
| qualityLevel: .balanced, | |
| recognizesMultipleItems: false, | |
| isHighFrameRateTrackingEnabled: false, | |
| isHighlightingEnabled: true) | |
| viewController.delegate = context.coordinator | |
| return viewController | |
| } | |
| func updateUIViewController(_ viewController: DataScannerViewController, context: Context) { | |
| if startScanning { | |
| try? viewController.startScanning() | |
| } else { | |
| viewController.stopScanning() | |
| } | |
| } | |
| class Coordinator: NSObject, DataScannerViewControllerDelegate { | |
| var parent: CameraScannerViewController | |
| init(_ parent: CameraScannerViewController) { | |
| self.parent = parent | |
| } | |
| func dataScanner(_ dataScanner: DataScannerViewController, didTapOn item: RecognizedItem) { | |
| switch item { | |
| case .text(let text): | |
| parent.scanResult = text.transcript | |
| default: | |
| break | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment