• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - AV Foundation 相机扫描箱内

[复制链接]
菜鸟教程小白 发表于 2022-12-11 18:25:15 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

enter image description here

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

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()

    }



Best Answer-推荐答案


尝试为您的 AVCaptureMetaDataOutput 设置 rectOfInterest

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

根据 documentation

rectOfInterest A rectangle of interest for limiting the search area for visual metadata.

The value of this property is a CGRect value that determines the object’s rectangle of interest for each frame of video.

The rectangle's origin is top left and is relative to the coordinate space of the device providing the metadata.

Specifying a rectangle of interest may improve detection performance for certain types of metadata. Metadata objects whose bounds do not intersect with the rectOfInterest will not be returned.

The default value of this property is a rectangle of (0.0, 0.0, 1.0, 1.0).

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

关于ios - AV Foundation 相机扫描箱内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41340123/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap