2016-04-20 10 views
2

私はtesseractを使ってアンドロイドアプリを開発しています。精度は85%以上です。しかし、私は出力にいくつかの制約を置いています。例えば、テキストが見つからない場合や意味のある単語が見つからない場合は、エラーメッセージを表示します。 テキストの妖精を見ましたが、その方法を理解できませんでした。 私は出力が意味のある単語や文章だけではなく、ランダムな文字になりたい... Plzはここ を助けるが、私のOCR部分のコードです:Tesseract OCR出力値

protected void onPhotoTaken() { 
    _taken = true; 

    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inSampleSize = 4; 

    Bitmap bitmap = BitmapFactory.decodeFile(_path, options); 

    try { 
     ExifInterface exif = new ExifInterface(_path); 
     int exifOrientation = exif.getAttributeInt(
       ExifInterface.TAG_ORIENTATION, 
       ExifInterface.ORIENTATION_NORMAL); 

     Log.v(TAG, "Orient: " + exifOrientation); 

     int rotate = 0; 

     switch (exifOrientation) { 
     case ExifInterface.ORIENTATION_ROTATE_90: 
      rotate = 90; 
      break; 
     case ExifInterface.ORIENTATION_ROTATE_180: 
      rotate = 180; 
      break; 
     case ExifInterface.ORIENTATION_ROTATE_270: 
      rotate = 270; 
      break; 
     } 

     Log.v(TAG, "Rotation: " + rotate); 

     if (rotate != 0) { 

      // Getting width & height of the given image. 
      int w = bitmap.getWidth(); 
      int h = bitmap.getHeight(); 

      // Setting pre rotate 
      Matrix mtx = new Matrix(); 
      mtx.preRotate(rotate); 

      // Rotating Bitmap 
      bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false); 
     } 

     // Convert to ARGB_8888, required by tess 
     bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); 

    } catch (IOException e) { 
     Log.e(TAG, "Couldn't correct orientation: " + e.toString()); 
    } 

    // _image.setImageBitmap(bitmap); 

    Log.v(TAG, "Before baseApi"); 

    TessBaseAPI baseApi = new TessBaseAPI(); 
    baseApi.setDebug(true); 
    baseApi.init(DATA_PATH, lang); 
    baseApi.setImage(bitmap); 

    String recognizedText = baseApi.getUTF8Text(); 

    baseApi.end(); 

    // You now have the text in recognizedText var, you can do anything with it. 
    // We will display a stripped out trimmed alpha-numeric version of it (if lang is eng) 
    // so that garbage doesn't make it to the display. 

    Log.v(TAG, "OCRED TEXT: " + recognizedText); 

    if (lang.equalsIgnoreCase("eng")) { 
     recognizedText = recognizedText.replaceAll("[^a-zA-Z0-9]+", " "); 
    } 

    recognizedText = recognizedText.trim(); 

    if (recognizedText.length() != 0) { 
     _field.setText(_field.getText().toString().length() == 0 ? recognizedText : _field.getText() + " " + recognizedText); 
     _field.setSelection(_field.getText().toString().length()); 
    } 

    } 

}

+0

写真に何か書きたいですか? – raj

+0

テキストが見つからない場合や画像が明瞭でない場合は画像を処理していません –

答えて

0

あなたのエラー表示は、他の部分で

を置くことができます
if (recognizedText.length() != 0) { 
     _field.setText(_field.getText().toString().length() == 0 ? recognizedText : _field.getText() + " " + recognizedText); 
     _field.setSelection(_field.getText().toString().length()); 
    }else { 
//Toast 
//System.out.print 
} 
+0

ありがとうございます。残念ながらtesseractは文字列VBG Z8などのテキストが見つからない場合はランダムな出力を与えます。これはこれを防ぐでしょう。 –

関連する問題