【问题标题】:AV Foundation camera scan within boxAV Foundation 相机扫描箱内
【发布时间】:2017-05-11 10:37:15
【问题描述】:

我能够以全相机分辨率成功扫描条码,但我希望它只能扫描特定框架内的条码(位于中心的框)。如果条形码位于该框架之外,则不应扫描。

var session         : AVCaptureSession = AVCaptureSession()
    var previewLayer    : AVCaptureVideoPreviewLayer!
    var highlightView   : UIView = UIView()

func opencamera()
    {
      if session.running
        {

        }
        else
        {
            session = AVCaptureSession()

            // Set the captureDevice.

            let videoCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

            session.sessionPreset = AVCaptureSessionPresetInputPriority
            // Create input object.
            let videoInput: AVCaptureDeviceInput?

            do {
                videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
            } catch {
                return
            }

            // Add input to the session.
            if (session.canAddInput(videoInput)) {
                session.addInput(videoInput)
            } else {
                scanningNotPossible()
            }
            let metadataOutput = AVCaptureMetadataOutput()

            // Add output to the session.
            if (session.canAddOutput(metadataOutput)) {
                session.addOutput(metadataOutput)

                // Send captured data to the delegate object via a serial queue.
                metadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())

                // Set barcode type for which to scan: EAN-13.
                //metadataOutput.metadataObjectTypes = metadataOutput.availableMetadataObjectTypes
                metadataOutput.metadataObjectTypes = [AVMetadataObjectTypeUPCECode,
                                                      AVMetadataObjectTypeCode39Code,
                                                      AVMetadataObjectTypeCode39Mod43Code,
                                                      AVMetadataObjectTypeEAN13Code,
                                                      AVMetadataObjectTypeEAN8Code,
                                                      AVMetadataObjectTypeCode93Code,
                                                      AVMetadataObjectTypeCode128Code,
                                                      AVMetadataObjectTypePDF417Code,

                ]

            } else {
                scanningNotPossible()
            }
            previewLayer = AVCaptureVideoPreviewLayer(session: session);
            previewLayer.frame = self.view.frame

            previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
            previewLayer?.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds))
                           image.layer.addSublayer(previewLayer);
                            session.startRunning()
        }
    }
    func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {

        //var highlightViewRect = CGRectZero

        // var barCodeObject : AVMetadataObject!

        var detectionString : String!

        let barCodeTypes = [AVMetadataObjectTypeUPCECode,
                            AVMetadataObjectTypeCode39Code,
                            AVMetadataObjectTypeCode39Mod43Code,
                            AVMetadataObjectTypeEAN13Code,
                            AVMetadataObjectTypeEAN8Code,
                            AVMetadataObjectTypePDF417Code,
                            ]


        // The scanner is capable of capturing multiple 2-dimensional barcodes in one scan.
        for metadata in metadataObjects {

            for barcodeType in barCodeTypes {


                let decodedData:  AVMetadataMachineReadableCodeObject =  metadata as! AVMetadataMachineReadableCodeObject
                if metadata.type == barcodeType {

                                           detectionString = (metadata as! AVMetadataMachineReadableCodeObject).stringValue
                    if(metadata.type  == AVMetadataObjectTypeEAN13Code){
                        if (detectionString.hasPrefix("0") && detectionString.characters.count > 1){
                            detectionString = String(detectionString.characters.dropFirst())
                        }

                    }
                    AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
                    self.session.stopRunning()

                    break
                }



            }
        }

        print(detectionString)

        self.previewLayer.removeFromSuperlayer()
        self.highlightView.removeFromSuperview()

    }

【问题讨论】:

  • 这段代码中的“highlightView”是什么?

标签: ios swift avfoundation barcode-scanner


【解决方案1】:

尝试为您的AVCaptureMetaDataOutput 设置rectOfInterest

self.metadataOutput.rectOfInterest = [self.previewLayer metadataOutputRectOfInterestForRect:myRectOfInterest];

根据documentation

rectOfInterest 用于限制搜索区域的感兴趣矩形 用于视觉元数据。

这个属性的值是一个 CGRect 值,它决定了 每帧视频的对象感兴趣的矩形。

矩形的原点在左上角并且相对于坐标 提供元数据的设备的空间。

指定感兴趣的矩形可能会提高检测性能 对于某些类型的元数据。边界不存在的元数据对象 不会返回与 rectOfInterest 相交。

此属性的默认值为 (0.0, 0.0, 1.0, 1.0)。

请注意,该值不是您的像素。查看 SO 答案 herehere 了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多