[iOS Live Text ] 使用iOS16的LiveText 用swiftUI写一个离线产品包装扫描工具
发表于更新于
广州
开发swiftuiiOS[iOS Live Text ] 使用iOS16的LiveText 用swiftUI写一个离线产品包装扫描工具
❄️2winter把之前写的小工具记录一下。
❤️ ,工具使用SwiftUI为主,由于LiveText还没有提供SwiftUI的API,所以使用switfui包装了一下Uikit的内容。
注意:仅支持iOS16系统以及A12芯片以上设备。
分享一下主要实现代码:
包装的live-text swiftUI组件:文本识别取景器,传入需要绑定的变量,识别文本后,将值传回swiftui页面。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
import Foundation import SwiftUI import UIKit import VisionKit
struct LiveScaner: UIViewControllerRepresentable { @Binding var startScanning: Bool @Binding var scanText: String @Binding var searchList:Array<CategoriesItem> func makeUIViewController(context: Context) -> DataScannerViewController { let controller = DataScannerViewController( recognizedDataTypes: [.text()], qualityLevel: .accurate, recognizesMultipleItems: false, isHighFrameRateTrackingEnabled: true, isPinchToZoomEnabled: true, isHighlightingEnabled: true ) controller.delegate = context.coordinator return controller } func updateUIViewController(_ uiViewController: DataScannerViewController, context: Context) { if startScanning { try? uiViewController.startScanning() } else { uiViewController.stopScanning() } } func makeCoordinator() -> Coordinator { Coordinator(self) } class Coordinator: NSObject, DataScannerViewControllerDelegate { func findhks(waitSearch:String)->Array<CategoriesItem>{ print("list:") print(waitSearch.replacing(/[、\s,。:()]/, with: "")) let findRes:Array<CategoriesItem> = CateList.filter{ fl in print( fl.name,waitSearch.score(word: fl.name)) return waitSearch.score(word: fl.name.replacing(/[、\s,。:()]/, with: "")) > 0 } return findRes } var parent: LiveScaner init(_ parent: LiveScaner) { self.parent = parent } func dataScanner(_ dataScanner: DataScannerViewController, didTapOn item: RecognizedItem) { switch item { case .text(let text): parent.searchList = findhks(waitSearch: text.transcript) parent.scanText = text.transcript default: break } } } }
|
swiftUI使用
1 2
| LiveScaner(startScanning: $startScanning, scanText: $scanText,searchList:$searchList) .frame(height: 120)
|
APP效果:App Store
❄️2winter
ReactNative FullStack Developer
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 ❄️2winter!