【问题标题】:SwiftUI - scanning QR codes in the macOS appSwiftUI - 在 macOS 应用中扫描二维码
【发布时间】: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


    【解决方案1】:

    我也很惊讶 macOS AVFoundation 不支持AVCaptureMetadataOutput,但CoreImage 支持二维码,这对我有用:

    let qrDetector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: nil)!
    
    func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
        guard let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
        
        let ciImage = CIImage(cvImageBuffer: imageBuffer)
        let features = qrDetector.features(in: ciImage)
    
        for feature in features {
            let qrCodeFeature = feature as! CIQRCodeFeature
            print("messageString \(qrCodeFeature.messageString!)")
        }
    }
    

    可能有更有效的方法来做到这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-22
      • 2020-01-31
      • 1970-01-01
      • 2014-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多