2016-12-27 71 views
1

enter image description hereAV基金會相機掃描框

我能夠成功掃描全相機分辨率條形碼內,但我想它應該只在特定的幀(盒被置於中心)掃描條形碼。如果條碼在於該框架之外不應該進行掃描。

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

    } 

回答

0

嘗試爲您AVCaptureMetaDataOutput設置rectOfInterest

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

作爲每documentation

rectOfInterest感興趣用於限制搜索區域 視覺元數據的矩形。

該屬性的值是一個CGRect值,該值確定每個視頻幀的感興趣對象的矩形框 。

矩形的原點是左上角,並且相對於提供元數據的設備的座標空間 。

指定的感興趣的矩形可以改善某些類型的元數據的檢測性能 。元數據對象,它們的邊界不相交 與rectOfInterest將不予退還。

該屬性的缺省值是(0.0,0.0,1.0,1.0 )的矩形。

請注意,該值不是您的像素。看看SO回答herehere獲取更多信息。