我正在开发一款Mac应用程序,它可以从相机中读取QR码,并从中读取文本。不幸的是,CodeScanner包仅适用于iOS,没有叫做的方法:
当然,要使用它,必须包含AVCaptureMetadataOutputObjectsDelegate - 不幸的是,这个协议在macOS上不可用。
有没有办法使用SwiftUI创建macOS二维码扫描器?
我目前已经内置了MacBook相机预览,但我缺少捕获QR码的功能:
func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection)
当然,要使用它,必须包含AVCaptureMetadataOutputObjectsDelegate - 不幸的是,这个协议在macOS上不可用。
有没有办法使用SwiftUI创建macOS二维码扫描器?
我目前已经内置了MacBook相机预览,但我缺少捕获QR码的功能:
final class PlayerContainerView: NSViewRepresentable {
typealias NSViewType = PlayerView
let captureSession: AVCaptureSession
init(captureSession: AVCaptureSession) {
self.captureSession = captureSession
}
func makeNSView(context: Context) -> PlayerView {
return PlayerView(captureSession: captureSession)
}
func updateNSView(_ nsView: PlayerView, context: Context) {}
}
class PlayerView: NSView {
var previewLayer: AVCaptureVideoPreviewLayer?
init(captureSession: AVCaptureSession) {
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
super.init(frame: .zero)
setupLayer()
}
func setupLayer() {
previewLayer?.frame = self.frame
previewLayer?.contentsGravity = .resizeAspectFill
previewLayer?.videoGravity = .resizeAspectFill
previewLayer?.connection?.automaticallyAdjustsVideoMirroring = false
if let mirroringSupported = previewLayer?.connection?.isVideoMirroringSupported {
if mirroringSupported {
previewLayer?.connection?.automaticallyAdjustsVideoMirroring = false
previewLayer?.connection?.isVideoMirrored = true
}
}
layer = previewLayer
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我找不到任何与AVCaptureMetadataOutputObjectsDelegate类似并允许我捕获元数据对象的委托协议。