【发布时间】:2021-08-24 12:09:27
【问题描述】:
我正在开发一个 Mac 应用程序,该应用程序将从相机读取 QR 码,然后从中读取文本。
不幸的是,CodeScanner 包仅适用于 iOS,并且没有调用方法:
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 一样工作并允许我捕获元数据对象的委托协议。
【问题讨论】:
标签: swift macos swiftui avfoundation qr-code