2017-07-02 117 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); 
     } 
    }