2017-07-02 100 views
3

已通过github链接浏览了Android OCR愿景样本https://codelabs.developers.google.com/codelabs/mobile-vision-ocr/index.html?index=..%2F..%2Findex#0Android-vision OCR; Android-vision

如何自动识别并挑选信用卡号码而不用费力去点击它。 目前receiveDetection方法是

@Override 
public void receiveDetections(Detector.Detections<TextBlock> detections) { 
    mGraphicOverlay.clear(); 
    SparseArray<TextBlock> items = detections.getDetectedItems(); 
    for (int i = 0; i < items.size(); ++i) { 
     TextBlock item = items.valueAt(i); 
     if (item != null && item.getValue() != null) { 
      Log.d("Processor", "Text detected! " + item.getValue()); 
     } 
     OcrGraphic graphic = new OcrGraphic(mGraphicOverlay, item); 
     mGraphicOverlay.add(graphic); 
    } 
} 

@Override 
public void release() { 
    mGraphicOverlay.clear(); 
} 

我想自动识别有效的信用卡号(也能像一个收据号码,账单顺序号的任何东西),因为它扫描并切换到与另一个意图的方法价值为了执行其他活动。

+0

虽然嗨,这不是解决你的问题。但为什么不使用https://www.card.io/? – Pranaysharma

+0

我正尝试创建一个应用程序来协助OCR解决方案 – Job

回答

1

您可以使用正则表达式并使用它来匹配它检测到的每个文本行。如果您的信用卡号码正则表达式匹配,请进行任何您想要的操作。不需要触摸。

你可以试试这个正则表达式(从this question拍摄)

^(?:4[0-9]{12}(?:[0-9]{3})?|[25][1-7][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$ 

在下面的方法

@Override 
    public void receiveDetections(Detector.Detections<TextBlock> detections) { 
     mGraphicOverlay.clear(); 
     SparseArray<TextBlock> items = detections.getDetectedItems(); 
     for (int i = 0; i < items.size(); ++i) { 
      TextBlock item = items.valueAt(i); 
      if (item != null && item.getValue() != null) { 
     List<Line> textComponents = (List<Line>) item.getComponents(); 
            for (Line currentText : textComponents) { 
             String text = currentText.getValue(); 
              if (word.matches(CREDIT_CARD_PATTERN){ 

              do your stuff here... 

             } 
            } 
           } 
      } 

      OcrGraphic graphic = new OcrGraphic(mGraphicOverlay, item); 
      mGraphicOverlay.add(graphic); 
     } 
    }