2016-12-27 3 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을 설정하십시오. rectOfInterest documentation

시각 메타 데이터에 대한 탐색 영역을 제한 관심의 구형으로 당

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

.

이 속성 값은 각 비디오 프레임에 대해 개체의 관심 영역을 결정하는 CGRect 값입니다.

사각형의 원점은 왼쪽 상단이며 메타 데이터를 제공하는 장치의 공간 의 좌표를 기준으로합니다.

특정 직사각형을 지정하면 특정 유형의 메타 데이터에 대해 검색 성능이 향상 될 수 있습니다 ( ). 경계가 과 일치하지 않는 메타 데이터 객체는 rectOfInterest와 교차하지 않습니다.

이 속성의 기본값은 (0.0, 0.0, 1.0, 1.0)의 사각형입니다.

값은 픽셀이 아닙니다. 자세한 내용은 SO 대답 herehere을보십시오.