0

我想知道如何在不捕获条形码和 QR 码(甚至字符)的情况下识别它。我在许多应用程序中看到,当我们将设备放在其中任何一个(QR/条形码)之上时,应用程序会自动识别它并开始处理。是否有任何用于此的扫描机制?如何做到这一点?这其中涉及哪些机制?

提前致谢。

4

2 回答 2

1
  1) The phone camera will be launched by the library it will autofocus and scans until it finds the decoded info from the image displayed by camera

  2) The info will be parsed by the library and it will give you the result.

解码信息是“其中已解码信息的条形码”

Example for QRCode: The data is present as square 

        for barcode: the data is present as vertical lines 

该库具有用于根据格式检测代码类型和解码的所有逻辑。请阅读更多二维码/条形码库文档或实施它并学习

于 2013-04-19T06:44:17.087 回答
0

您可以使用AVCaptureSession,例如:

let session = AVCaptureSession()
var qrPayload: String?

func startSession() {
    guard !started else { return }

    let output = AVCaptureMetadataOutput()
    output.setMetadataObjectsDelegate(self, queue: .main)

    let device: AVCaptureDevice?

    if #available(iOS 10.0, *) {
        device = AVCaptureDevice
            .DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: .metadataObject, position: .back)
            .devices
            .first
    } else {
        device = AVCaptureDevice.devices().first { $0.position == .back }
    }

    guard
        let camera = device,
        let input = try? AVCaptureDeviceInput(device: camera),
        session.canAddInput(input),
        session.canAddOutput(output)
    else {
        // handle failures here
        return
    }

    session.addInput(input)

    session.addOutput(output)
    output.metadataObjectTypes = [.qr]

    let videoLayer = AVCaptureVideoPreviewLayer(session: session)
    videoLayer.frame = view.bounds
    videoLayer.videoGravity = .resizeAspectFill

    view.layer.addSublayer(videoLayer)

    session.startRunning()
}

并扩展您的视图控制器以符合AVCaptureMetadataOutputObjectsDelegate

extension QRViewController: AVCaptureMetadataOutputObjectsDelegate {
    func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
        guard
            qrPayload == nil,
            let object = metadataObjects.first as? AVMetadataMachineReadableCodeObject,
            let string = object.stringValue
        else { return }

        qrPayload = string
        print(qrPayload)

        // perhaps dismiss this view controller now that you’ve succeeded
    }
}

请注意,我正在测试以确保它qrPayloadnil因为我发现您可以看到metadataOutput(_:didOutput:from:)在视图控制器被解除之前被调用了几次。

于 2019-12-14T22:21:28.783 回答