菜鸟教程小白 发表于 2022-12-12 12:16:29

ios - Swift 3 在 1 行中检测多个条码


                                            <p><p>您好,我正在使用 <code>swift3</code> 和 <code>AVFoundation</code> 来检测账单的条形码。我的账单在 1 行中最多有 3 个条形码。怎样才能将全部条码合并为一串,并在检测到该行中的所有条码并合并为一串后才停止操作?</p>

<p><strong>这是我的账单样本</strong> </p>

<p> <a href="/image/WggAx.png" rel="noreferrer noopener nofollow"><img src="/image/WggAx.png" alt="enter image description here"/></a> </p>

<p>底部有3个条形码</p>

<p><strong>这是我的 AVCaptureMetadataOutputObjectsDelegate 函数</strong></p>

<pre><code>func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: !, from connection: AVCaptureConnection!) {
      // This is the delegate&#39;smethod that is called when a code is readed
      for metadata in metadataObjects {
            let readableObject = metadata as! AVMetadataMachineReadableCodeObject
            let code = readableObject.stringValue


            self.dismiss(animated: true, completion: nil)
            self.delegate?.barcodeReaded(barcode: code!)
            print(code!)
            print(readableObject.type)
      }
    }
</code></pre>

<p>我们如何修改上面的来扫描所有3个条码并将它们组合起来,当所有条码都被解码时通知<code>Viewcontroller</code>?非常感谢任何帮助。谢谢</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>不要停止扫描,除非检测到 3 个条码然后组合代码:</p>

<pre><code>func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: !, from connection: AVCaptureConnection!) {
    // This is the delegate&#39;smethod that is called when a code is headed

    guard metadataObjects.count == 3 else { return }

    var finalString: String = &#34;&#34;

    for metadata in metadataObjects {
      let readableObject = metadata as! AVMetadataMachineReadableCodeObject
      let code = readableObject.stringValue

      finalString.append(code!)
      print(code!)
      print(readableObject.type)
    }

    self.dismiss(animated: true, completion: nil)
    self.delegate?.barcodeReaded(barcode: finalString)
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Swift 3 在 1 行中检测多个条码,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40398896/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40398896/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Swift 3 在 1 行中检测多个条码