// Scan the photo for text using the tess-two API
  public static String scanPhoto(Bitmap bitmap) {

    TessBaseAPI baseApi = new TessBaseAPI();
    baseApi.setDebug(true);
    baseApi.init(DATA_PATH, lang);
    // set the black list
    baseApi.setVariable("tessedit_char_blacklist", "':;,.?/\\}][{!@#$%^&*()-_=+~");
    baseApi.setVariable("save_blob_choices", "T");

    baseApi.setImage(bitmap);

    String recognizedText = baseApi.getUTF8Text();

    // Iterate over the results and print confidence values for debugging purposes
    final ResultIterator iterator = baseApi.getResultIterator();
    String lastUTF8Text;
    float lastConfidence;
    iterator.begin();
    do {
      lastUTF8Text = iterator.getUTF8Text(PageIteratorLevel.RIL_WORD);
      lastConfidence = iterator.confidence(PageIteratorLevel.RIL_WORD);
      if (lastConfidence > 50) {
        Log.d(TAG, String.format("%s => %.2f", lastUTF8Text, lastConfidence));
      }

    } while (iterator.next(PageIteratorLevel.RIL_WORD));

    baseApi.end();

    Log.d(TAG, recognizedText);

    return recognizedText;
  }
  @SmallTest
  public void testSetRectangle() {
    // Attempt to initialize the API.
    final TessBaseAPI baseApi = new TessBaseAPI();
    baseApi.init(TESSBASE_PATH, DEFAULT_LANGUAGE);
    baseApi.setPageSegMode(TessBaseAPI.PageSegMode.PSM_SINGLE_CHAR);

    final int width = 640;
    final int height = 480;
    final Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    final Paint paint = new Paint();
    final Canvas canvas = new Canvas(bmp);

    canvas.drawColor(Color.WHITE);

    paint.setColor(Color.BLACK);
    paint.setStyle(Style.FILL);
    paint.setAntiAlias(true);
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(32.0f);

    // Draw separate text on the left and right halves of the image.
    final String leftInput = "A";
    final String rightInput = "B";
    canvas.drawText(leftInput, width / 4, height / 2, paint);
    canvas.drawText(rightInput, width * 3 / 4, height / 2, paint);

    baseApi.setVariable(TessBaseAPI.VAR_CHAR_WHITELIST, leftInput + rightInput);
    baseApi.setImage(bmp);

    // Ensure the result is correct for a rectangle on the left half of the image.
    Rect left = new Rect(0, 0, width / 2, height);
    baseApi.setRectangle(left);
    String leftResult = baseApi.getUTF8Text();
    assertEquals("Found incorrect text.", leftInput, leftResult);

    // Ensure the result is correct for a rectangle on the right half of the image.
    Rect right = new Rect(width / 2, 0, width, height);
    baseApi.setRectangle(right);
    String rightResult = baseApi.getUTF8Text();
    assertEquals("Found incorrect text.", rightInput, rightResult);

    // Attempt to shut down the API.
    baseApi.end();
    bmp.recycle();
  }
  @SmallTest
  public void testSetVariable() {
    final String inputText = "hello";
    final Bitmap bmp = getTextImage(inputText, 640, 480);

    // Attempt to initialize the API.
    final TessBaseAPI baseApi = new TessBaseAPI();
    baseApi.init(TESSBASE_PATH, DEFAULT_LANGUAGE);
    baseApi.setPageSegMode(TessBaseAPI.PageSegMode.PSM_SINGLE_LINE);

    // Ensure that setting the blacklist variable works.
    final String blacklistedCharacter = inputText.substring(1, 2);
    baseApi.setVariable(TessBaseAPI.VAR_CHAR_BLACKLIST, blacklistedCharacter);
    baseApi.setImage(bmp);
    final String outputText = baseApi.getUTF8Text();
    assertFalse("Found a blacklisted character.", outputText.contains(blacklistedCharacter));

    // Attempt to shut down the API.
    baseApi.end();
    bmp.recycle();
  }
  @SmallTest
  public void testChoiceIterator() {
    final String inputText = "hello";
    final Bitmap bmp = TessBaseAPITest.getTextImage(inputText, 640, 480);

    // Attempt to initialize the API.
    final TessBaseAPI baseApi = new TessBaseAPI();
    baseApi.init(TessBaseAPITest.TESSBASE_PATH, TessBaseAPITest.DEFAULT_LANGUAGE);
    baseApi.setPageSegMode(TessBaseAPI.PageSegMode.PSM_SINGLE_LINE);
    baseApi.setVariable(TessBaseAPI.VAR_SAVE_BLOB_CHOICES, TessBaseAPI.VAR_TRUE);

    // Ensure that text is recognized.
    baseApi.setImage(bmp);
    String recognizedText = baseApi.getUTF8Text();
    assertTrue("No recognized text found.", recognizedText != null && !recognizedText.equals(""));

    // Iterate through the results.
    ResultIterator iterator = baseApi.getResultIterator();
    List<Pair<String, Double>> choicesAndConfidences;
    iterator.begin();
    do {
      choicesAndConfidences = iterator.getChoicesAndConfidence(PageIteratorLevel.RIL_SYMBOL);
      assertNotNull("Invalid result.", choicesAndConfidences);

      for (Pair<String, Double> choiceAndConfidence : choicesAndConfidences) {
        String choice = choiceAndConfidence.first;
        Double conf = choiceAndConfidence.second;
        assertTrue("No choice value found.", choice != null && !choice.equals(""));
        assertTrue("Found an incorrect confidence value.", conf >= 0 && conf <= 100);
      }
    } while (iterator.next(PageIteratorLevel.RIL_SYMBOL));
    iterator.delete();

    assertNotNull("No ChoiceIterator values found.", choicesAndConfidences);

    // Attempt to shut down the API.
    baseApi.end();
    bmp.recycle();
  }
  @SmallTest
  public void testProgressValues_setRectangle() {
    class Notifier implements ProgressNotifier {
      public boolean receivedProgress = false;
      private Rect bounds;

      public void reset(Rect bounds) {
        this.bounds = bounds;
        receivedProgress = false;
      }

      @Override
      public void onProgressValues(ProgressValues progressValues) {
        receivedProgress = true;
        testProgressValues(progressValues, bounds);
      }
    }

    final Notifier notifier = new Notifier();

    final int width = 640;
    final int height = 480;
    final Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    final Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    paint.setStyle(Style.FILL);
    paint.setAntiAlias(true);
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(32.0f);

    // Draw separate text on the left and right halves of the image.
    final Canvas canvas = new Canvas(bmp);
    canvas.drawColor(Color.WHITE);
    final String leftInput = "A";
    final String rightInput = "B";
    canvas.drawText(leftInput, width / 4, height / 2, paint);
    canvas.drawText(rightInput, width * 3 / 4, height / 2, paint);

    // Attempt to initialize the API.
    final TessBaseAPI baseApi = new TessBaseAPI(notifier);
    baseApi.init(TESSBASE_PATH, DEFAULT_LANGUAGE);
    baseApi.setPageSegMode(TessBaseAPI.PageSegMode.PSM_SINGLE_LINE);
    baseApi.setVariable(TessBaseAPI.VAR_CHAR_WHITELIST, leftInput + rightInput);
    baseApi.setImage(bmp);

    // Attempt to restrict recognition to a sub-rectangle of the image.
    final Rect left = new Rect(0, 0, width / 2, height);
    baseApi.setRectangle(left);
    notifier.reset(left);

    // Ensure a progress callback is received.
    baseApi.getHOCRText(0);
    assertTrue(notifier.receivedProgress);

    // Attempt to restrict recognition to a sub-rectangle of the image.
    final Rect right = new Rect(width / 2 + 5, 7, width - 5, height - 7);
    baseApi.setRectangle(right);
    notifier.reset(right);

    // Ensure a progress callback is received.
    baseApi.getHOCRText(0);
    assertTrue(notifier.receivedProgress);

    // Attempt to shut down the API.
    baseApi.end();
    bmp.recycle();
  }