// 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; }
public void ocr_main() { Log.v(TAG, "entering tess"); // Getting uri of image/cropped image try { myimage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), image_uri); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // Log.v(TAG, "bitmap " + myimage.getByteCount()); // Bitmap argb = myimage; // argb = argb.copy(Bitmap.Config.ARGB_8888, true); // Log.v(TAG, "bitmap after argb:" + argb.getByteCount()); // BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inSampleSize = 2; myimage = BitmapFactory.decodeFile(filepath, opt); // Log.v(TAG, "bitmap after comp:" + myimage.getByteCount()); // TessBase starts TessBaseAPI baseApi = new TessBaseAPI(); baseApi.setDebug(true); baseApi.init(DATA_PATH, lang); Log.v(TAG, "Before baseApi"); baseApi.setImage(myimage); Log.v(TAG, "Before baseApi2"); String recognizedText = baseApi.getUTF8Text(); Log.v(TAG, "Before baseApi3"); baseApi.end(); Log.v(TAG, "OCRED TEXT: " + recognizedText); if (lang.equalsIgnoreCase("eng")) { recognizedText = recognizedText.replaceAll("[^a-zA-Z0-9]+", " "); } // recognizedText is the final OCRed text recognizedText = recognizedText.trim(); // String ocrtext = "And...BAM! OCRed: " + recognizedText; // Toast toast = Toast.makeText(this.getApplicationContext(), ocrtext, // Toast.LENGTH_LONG); // toast.show(); // deleting temporary crop file created File file = new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "temp.bmp"); boolean deleted = file.delete(); Log.i(TAG, "File deleted: " + deleted); Intent intent = new Intent(this, ResultActivity.class); intent.putExtra("ocrText", recognizedText); startActivity(intent); }
public String getTextContent() { Log.d("whathaeljf", "The second image path is " + imagePath); Bitmap bitmap = BitmapFactory.decodeFile(imagePath); /* Matrix matrix = new Matrix(); matrix.postRotate(180); Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); */ TessBaseAPI baseApi = new TessBaseAPI(); baseApi.setDebug(true); baseApi.init(DATA_PATH, lang); baseApi.setImage(bitmap); String recognizedText = baseApi.getUTF8Text(); return recognizedText; }
/*Begin citation: http://gaut.am/making-an-ocr-android-app-using-tesseract/ gets the image from the external storage directory and uses the tess-two library to process the image and return the recognized text */ public String getOCRText() { Bitmap bitmap = BitmapFactory.decodeFile(DIR + "ocrImage.jpg"); TessBaseAPI baseApi = new TessBaseAPI(); baseApi.setDebug(true); baseApi.init(DIR, "eng"); baseApi.setImage(bitmap); String recognizedText = baseApi.getUTF8Text(); baseApi.end(); if ("eng".equalsIgnoreCase("eng")) { recognizedText = recognizedText.replaceAll("[^a-zA-Z0-9 ]+", " "); } Log.d(DolchListActivity.TAG, recognizedText); return recognizedText.trim(); }