2017-02-21 8 views
2

私はチュートリアルに従って、Android用Googleのモバイルビジョンを実装しました。私は領収書をスキャンし、数値の合計を見つけるアプリケーションを構築しようとしています。しかし、異なるフォーマットで印刷された異なるレシートをスキャンすると、APIはTextBlocksを任意の方法で検出します。たとえば、1つの領収書で、複数の単語の単語が単一のスペースで区切られている場合、それらは単一のTextBlockにグループ化されます。しかし、2つの単語の単語が空白で区切られている場合、それらは同じ "行"に表示されますが、独立したTextBlockとして区切られます。私がしようとしているのは、APIが領収書の各行全体を単一のエンティティとして認識するようにすることです。これは可能ですか?Android用モバイルビジョンでテキストの全文を読むように強制する方法

+0

まだ解決策がありましたか?もしそうなら、カメラアプリをリアルタイムで使用するのではなく、既存の画像で検出することができましたか? – DaveNOTDavid

答えて

0
public ArrayList<T> getAllGraphicsInRow(float rawY) { 
    synchronized (mLock) { 
     ArrayList<T> row = new ArrayList<>(); 
     // Get the position of this View so the raw location can be offset relative to the view. 
     int[] location = new int[2]; 
     this.getLocationOnScreen(location); 
     for (T graphic : mGraphics) { 
      float rawX = this.getWidth(); 
      for (int i=0; i<rawX; i+=10){ 
       if (graphic.contains(i - location[0], rawY - location[1])) { 
        if(!row.contains(graphic)) { 
         row.add(graphic); 
        } 
       } 
      } 
     } 
     return row; 
    } 
} 

これはGraphicOverlay.javaファイル内にあり、基本的にその行のすべてのグラフィックスを取得します。

public static boolean almostEqual(double a, double b, double eps){ 
    return Math.abs(a-b)<(eps); 
} 

public static boolean pointAlmostEqual(Point a, Point b){ 
    return almostEqual(a.y,b.y,10); 
} 
public static boolean cornerPointAlmostEqual(Point[] rect1, Point[] rect2){ 
    boolean almostEqual=true; 
    for (int i=0; i<rect1.length;i++){ 
      if (!pointAlmostEqual(rect1[i],rect2[i])){ 
       almostEqual=false; 
      } 
     } 
    return almostEqual; 
} 
private boolean onTap(float rawX, float rawY) { 
    String priceRegex = "(\\d+[,.]\\d\\d)"; 
    ArrayList<OcrGraphic> graphics = mGraphicOverlay.getAllGraphicsInRow(rawY); 
    OcrGraphic currentGraphics = mGraphicOverlay.getGraphicAtLocation(rawX,rawY); 
    if (graphics !=null && currentGraphics!=null) { 
     List<? extends Text> currentComponents = currentGraphics.getTextBlock().getComponents(); 
     final Pattern pattern = Pattern.compile(priceRegex); 
     final Pattern pattern1 = Pattern.compile(priceRegex); 

     TextBlock text = null; 
     Log.i("text results", "This many in the row: " + Integer.toString(graphics.size())); 

     ArrayList<Text> combinedComponents = new ArrayList<>(); 
     for (OcrGraphic graphic : graphics) { 
      if (!graphic.equals(currentGraphics)) { 
       text = graphic.getTextBlock(); 
       Log.i("text results", text.getValue()); 
       combinedComponents.addAll(text.getComponents()); 
      } 
     } 

     for (Text currentText : currentComponents) { // goes through components in the row 
      final Matcher matcher = pattern.matcher(currentText.getValue()); // looks for 
      Point[] currentPoint = currentText.getCornerPoints(); 

      for (Text otherCurrentText : combinedComponents) {//Looks for other components that are in the same row 
       final Matcher otherMatcher = pattern1.matcher(otherCurrentText.getValue()); // looks for 
       Point[] innerCurrentPoint = otherCurrentText.getCornerPoints(); 

       if (cornerPointAlmostEqual(currentPoint, innerCurrentPoint)) { 
        if (matcher.find()) { // if you click on the price 
         Log.i("oh yes", "Item: " + otherCurrentText.getValue()); 
         Log.i("oh yes", "Value: " + matcher.group(1)); 
         itemList.add(otherCurrentText.getValue()); 
         priceList.add(Float.valueOf(matcher.group(1))); 
        } 
        if (otherMatcher.find()) { // if you click on the item 
         Log.i("oh yes", "Item: " + currentText.getValue()); 
         Log.i("oh yes", "Value: " + otherMatcher.group(1)); 
         itemList.add(currentText.getValue()); 
         priceList.add(Float.valueOf(otherMatcher.group(1))); 
        }      
        Toast toast = Toast.makeText(this, " Text Captured!" , Toast.LENGTH_SHORT); 
        toast.show(); 
       } 
      } 

     } 
     return true; 
    } 
    return false; 
} 

これはOcrCaptureActivity.javaであるべきであり、それが行にテキストブロックを分割し、成分が全ての価格であり、それに応じてすべての値を出力した場合のラインをチェック同じ行のブロックを見つけます。

almostEqualのeps値は、行内のグラフィックスを確認するための許容差です。

+0

これは、テキスト認識APIのクラスCameraSourcePreviewとGraphicOverlayを使用する必要があるため、既存のイメージとは対照的にカメラアプリケーションをリアルタイムで使用する場合にのみ有効です。 – DaveNOTDavid

関連する問題