com.googlecode.tesseract.android.TessBaseAPI Java Examples
The following examples show how to use
com.googlecode.tesseract.android.TessBaseAPI.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CaptureActivity.java From android-mrz-reader with Apache License 2.0 | 7 votes |
/** * Returns a string that represents which OCR engine(s) are currently set to be run. * * @return OCR engine mode */ String getOcrEngineModeName() { String ocrEngineModeName = ""; String[] ocrEngineModes = getResources().getStringArray(R.array.ocrenginemodes); if (ocrEngineMode == TessBaseAPI.OEM_TESSERACT_ONLY) { ocrEngineModeName = ocrEngineModes[0]; } else if (ocrEngineMode == TessBaseAPI.OEM_CUBE_ONLY) { ocrEngineModeName = ocrEngineModes[1]; } else if (ocrEngineMode == TessBaseAPI.OEM_TESSERACT_CUBE_COMBINED) { ocrEngineModeName = ocrEngineModes[2]; } return ocrEngineModeName; }
Example #2
Source File: MainActivity.java From OCR-Test with Apache License 2.0 | 6 votes |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.imageView); OCRTextView = (TextView) findViewById(R.id.OCRTextView); progressDialog = new ProgressDialog(this); progressDialog.setMessage("Processing image..."); // init Image image = BitmapFactory.decodeResource(getResources(), R.drawable.test_image); datapath = getFilesDir()+ "/tesseract/"; //make sure training data has been copied checkFile(new File(datapath + "tessdata/")); //init Tesseract API String language = "eng"; //language = "hin+eng"; mTess = new TessBaseAPI(); mTess.init(datapath, language, OEM_TESSERACT_ONLY); }
Example #3
Source File: BitmapORC.java From Android-OCR-Example with Apache License 2.0 | 6 votes |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_orc_bitmap); findView(); mTextBitmap = getTextImage("你好", 300, 300); image.setImageBitmap(mTextBitmap); baseApi = new TessBaseAPI(); baseApi.init("/mnt/sdcard/tesseract/", "chi_tra"); baseApi.setPageSegMode(TessBaseAPI.PageSegMode.PSM_SINGLE_LINE); baseApi.setImage(mTextBitmap); String outputText = baseApi.getUTF8Text(); tessResults.setText(outputText); }
Example #4
Source File: CameraORC.java From Android-OCR-Example with Apache License 2.0 | 6 votes |
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != RESULT_OK) { return; } if (requestCode == cameraResultCode) { Bundle extras = data.getExtras(); Bitmap bmp = (Bitmap) extras.get("data"); image.setImageBitmap(bmp); TessBaseAPI baseApi = new TessBaseAPI(); baseApi.init("/mnt/sdcard/tesseract/", "chi_tra"); baseApi.setPageSegMode(TessBaseAPI.PageSegMode.PSM_SINGLE_LINE); baseApi.setImage(bmp); String outputText = baseApi.getUTF8Text(); tessResults.setText(outputText); } super.onActivityResult(requestCode, resultCode, data); }
Example #5
Source File: ResultActivity.java From TextOcrExample with Apache License 2.0 | 6 votes |
/** * 获取图片上的文字 * * @param bitmap * @return */ private String getText(Bitmap bitmap) { String retStr = "No result"; try { TessBaseAPI tessBaseAPI = new TessBaseAPI(); tessBaseAPI.init(DATA_PATH, language); // 识别黑名单 // tessBaseAPI.setVariable(TessBaseAPI.VAR_CHAR_BLACKLIST, "!@#$%^&*()_+=÷-[]}{;:'\"\\|~`," + // "./<>?" + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"); bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); tessBaseAPI.setImage(bitmap); retStr = tessBaseAPI.getUTF8Text(); tessBaseAPI.clear(); tessBaseAPI.end(); } catch (Exception e) { Message msg = Message.obtain(); msg.what = 0; msg.obj = e.getMessage(); mHandler.sendMessage(msg); } return retStr; }
Example #6
Source File: TesseractOCR.java From polling-station-app with GNU Lesser General Public License v3.0 | 6 votes |
/** * Initializes Tesseract library using traineddata file. * Should not be called directly, is public for testing. */ public void init() { baseApi = new TessBaseAPI(); baseApi.setDebug(true); String path = Environment.getExternalStorageDirectory() + "/" + Util.FOLDER_DIGITAL_VOTING_PASS + "/"; File trainedDataFile = new File(path, TesseractOCR.FOLDER_TESSERACT_DATA + "/" + trainedData); try { mDeviceStorageAccessLock.acquire(); if (!trainedDataFile.exists()) { Log.i(TAG, "No existing trained data found, copying from assets.."); Util.copyAssetsFile(assetManager.open(trainedData), trainedDataFile); } else { Log.i(TAG, "Existing trained data found"); } mDeviceStorageAccessLock.release(); baseApi.init(path, trainedData.replace(TesseractOCR.TRAINED_DATA_EXTENSION, "")); //extract language code from trained data file } catch (IOException | InterruptedException e) { e.printStackTrace(); //TODO show error to user, coping failed } }
Example #7
Source File: OCRProcessor.java From Camdroid with Apache License 2.0 | 6 votes |
public OCRFrameWorker(OnCameraPreviewListener.FrameDrawer drawer) { super(drawer); this.tessBaseAPI = new TessBaseAPI(); this.tessBaseAPI.setPageSegMode(PageSegMode.PSM_AUTO_OSD); this.tessBaseAPI.setVariable(TessBaseAPI.VAR_ACCURACYVSPEED, String.valueOf(50)); this.tessBaseAPI.init(Environment.getExternalStorageDirectory().getPath(), "eng"); paint = new Paint(); paint.setColor(Color.RED); int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) 11.0, drawer.getDisplayMetrics()); paint.setTextSize(size); bounds = new Rect(); paint.getTextBounds("Q", 0, 1, bounds); lines = drawer.getDisplayMetrics().heightPixels / bounds.height(); }
Example #8
Source File: CaptureActivity.java From android-mrz-reader with Apache License 2.0 | 6 votes |
/** * Method to start or restart recognition after the OCR engine has been initialized, * or after the app regains focus. Sets state related settings and OCR engine parameters, * and requests camera initialization. */ void resumeOCR() { Log.d(TAG, "resumeOCR()"); // This method is called when Tesseract has already been successfully initialized, so set // isEngineReady = true here. isEngineReady = true; isPaused = false; if (handler != null) { handler.resetState(); } if (baseApi != null) { baseApi.setPageSegMode(pageSegmentationMode); baseApi.setVariable(TessBaseAPI.VAR_CHAR_BLACKLIST, characterBlacklist); baseApi.setVariable(TessBaseAPI.VAR_CHAR_WHITELIST, characterWhitelist); } if (hasSurface) { // The activity was paused but not stopped, so the surface still exists. Therefore // surfaceCreated() won't be called, so init the camera here. initCamera(surfaceHolder); } }
Example #9
Source File: TessOCR.java From android-tesseract-ocr with MIT License | 6 votes |
public TessOCR() { // TODO Auto-generated constructor stub mTess = new TessBaseAPI(); String datapath = Environment.getExternalStorageDirectory() + "/tesseract/"; String language = "eng"; File dir = new File(datapath + "tessdata/"); if (!dir.exists()) dir.mkdirs(); mTess.init(datapath, language); }
Example #10
Source File: TessEngine.java From Tesseract-OCR-Scanner with Apache License 2.0 | 6 votes |
public String detectText(Bitmap bitmap) { Log.d(TAG, "Initialization of TessBaseApi"); TessDataManager.initTessTrainedData(MyApplication.sAppContext); TessBaseAPI tessBaseAPI = new TessBaseAPI(); String path = TessDataManager.getTesseractFolder(); Log.d(TAG, "Tess folder: " + path); tessBaseAPI.setDebug(true); tessBaseAPI.init(path, "eng"); // 白名单 tessBaseAPI.setVariable(TessBaseAPI.VAR_CHAR_WHITELIST, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); // 黑名单 tessBaseAPI.setVariable(TessBaseAPI.VAR_CHAR_BLACKLIST, "!@#$%^&*()_+=-[]}{;:'\"\\|~`,./<>?"); tessBaseAPI.setPageSegMode(TessBaseAPI.PageSegMode.PSM_AUTO_OSD); Log.d(TAG, "Ended initialization of TessEngine"); Log.d(TAG, "Running inspection on bitmap"); tessBaseAPI.setImage(bitmap); String inspection = tessBaseAPI.getHOCRText(0); Log.d(TAG, "Confidence values: " + tessBaseAPI.meanConfidence()); tessBaseAPI.end(); System.gc(); return Tools.getTelNum(inspection); }
Example #11
Source File: OcrRecognizeAsyncTask.java From android-mrz-reader with Apache License 2.0 | 5 votes |
OcrRecognizeAsyncTask(CaptureActivity activity, TessBaseAPI baseApi, byte[] data, int width, int height) { this.activity = activity; this.baseApi = baseApi; this.data = data; this.width = width; this.height = height; }
Example #12
Source File: Recognizer.java From text-scanner with MIT License | 5 votes |
private void recognizeText(){ String language = ""; if (Binarization.language == 0) language = "eng"; else language= "spa"; baseApi = new TessBaseAPI(); baseApi.init(DATA_PATH, language,TessBaseAPI.OEM_TESSERACT_ONLY); baseApi.setImage(Binarization.umbralization); textScanned = baseApi.getUTF8Text(); }
Example #13
Source File: TessactOcr.java From RobotHelper with GNU Affero General Public License v3.0 | 5 votes |
/** * @param img //需要识别的图片 * @param lang // 文字语言,默认只支持了eng和chi_sim * @param whitelist //白名单 * @param blacklist //黑名单 * @return String */ public static String img2string(Bitmap img, String lang, String whitelist, String blacklist) { if (img == null) { return ""; } if (!checkInit()) { sleep(500); return img2string(img, lang, whitelist, blacklist); } TessBaseAPI tessBaseAPI = new TessBaseAPI(); // 使用默认语言初始化BaseApi tessBaseAPI.init(SD_PATH + "/", lang); if (whitelist != null && !whitelist.equals("")) { tessBaseAPI.setVariable(TessBaseAPI.VAR_CHAR_WHITELIST, whitelist); // 识别白名单 } if (blacklist != null && !"".equals(blacklist)) { tessBaseAPI.setVariable(TessBaseAPI.VAR_CHAR_BLACKLIST, blacklist); //识别黑名单 } tessBaseAPI.setImage(img); // 获取返回值 String recognizedText = tessBaseAPI.getUTF8Text(); tessBaseAPI.end(); return recognizedText; }
Example #14
Source File: CaptureActivity.java From android-mrz-reader with Apache License 2.0 | 5 votes |
/** * Gets values from shared preferences and sets the corresponding data members in this activity. */ private void retrievePreferences() { prefs = PreferenceManager.getDefaultSharedPreferences(this); // Retrieve from preferences, and set in this Activity, the language preferences PreferenceManager.setDefaultValues(this, R.xml.preferences, false); setSourceLanguage(prefs.getString(PreferencesActivity.KEY_SOURCE_LANGUAGE_PREFERENCE, CaptureActivity.DEFAULT_SOURCE_LANGUAGE_CODE)); setTargetLanguage(prefs.getString(PreferencesActivity.KEY_TARGET_LANGUAGE_PREFERENCE, CaptureActivity.DEFAULT_TARGET_LANGUAGE_CODE)); isTranslationActive = prefs.getBoolean(PreferencesActivity.KEY_TOGGLE_TRANSLATION, false); // Retrieve from preferences, and set in this Activity, the capture mode preference if (prefs.getBoolean(PreferencesActivity.KEY_CONTINUOUS_PREVIEW, CaptureActivity.DEFAULT_TOGGLE_CONTINUOUS)) { isContinuousModeActive = true; } else { isContinuousModeActive = false; } // Retrieve from preferences, and set in this Activity, the page segmentation mode preference String[] pageSegmentationModes = getResources().getStringArray(R.array.pagesegmentationmodes); String pageSegmentationModeName = prefs.getString(PreferencesActivity.KEY_PAGE_SEGMENTATION_MODE, pageSegmentationModes[0]); if (pageSegmentationModeName.equals(pageSegmentationModes[0])) { pageSegmentationMode = TessBaseAPI.PageSegMode.PSM_AUTO_OSD; } else if (pageSegmentationModeName.equals(pageSegmentationModes[1])) { pageSegmentationMode = TessBaseAPI.PageSegMode.PSM_AUTO; } else if (pageSegmentationModeName.equals(pageSegmentationModes[2])) { pageSegmentationMode = TessBaseAPI.PageSegMode.PSM_SINGLE_BLOCK; } else if (pageSegmentationModeName.equals(pageSegmentationModes[3])) { pageSegmentationMode = TessBaseAPI.PageSegMode.PSM_SINGLE_CHAR; } else if (pageSegmentationModeName.equals(pageSegmentationModes[4])) { pageSegmentationMode = TessBaseAPI.PageSegMode.PSM_SINGLE_COLUMN; } else if (pageSegmentationModeName.equals(pageSegmentationModes[5])) { pageSegmentationMode = TessBaseAPI.PageSegMode.PSM_SINGLE_LINE; } else if (pageSegmentationModeName.equals(pageSegmentationModes[6])) { pageSegmentationMode = TessBaseAPI.PageSegMode.PSM_SINGLE_WORD; } else if (pageSegmentationModeName.equals(pageSegmentationModes[7])) { pageSegmentationMode = TessBaseAPI.PageSegMode.PSM_SINGLE_BLOCK_VERT_TEXT; } else if (pageSegmentationModeName.equals(pageSegmentationModes[8])) { pageSegmentationMode = TessBaseAPI.PageSegMode.PSM_SPARSE_TEXT; } // Retrieve from preferences, and set in this Activity, the OCR engine mode String[] ocrEngineModes = getResources().getStringArray(R.array.ocrenginemodes); String ocrEngineModeName = prefs.getString(PreferencesActivity.KEY_OCR_ENGINE_MODE, ocrEngineModes[0]); if (ocrEngineModeName.equals(ocrEngineModes[0])) { ocrEngineMode = TessBaseAPI.OEM_TESSERACT_ONLY; } else if (ocrEngineModeName.equals(ocrEngineModes[1])) { ocrEngineMode = TessBaseAPI.OEM_CUBE_ONLY; } else if (ocrEngineModeName.equals(ocrEngineModes[2])) { ocrEngineMode = TessBaseAPI.OEM_TESSERACT_CUBE_COMBINED; } // Retrieve from preferences, and set in this Activity, the character blacklist and whitelist characterBlacklist = OcrCharacterHelper.getBlacklist(prefs, sourceLanguageCodeOcr); characterWhitelist = OcrCharacterHelper.getWhitelist(prefs, sourceLanguageCodeOcr); prefs.registerOnSharedPreferenceChangeListener(listener); }
Example #15
Source File: CaptureActivity.java From android-mrz-reader with Apache License 2.0 | 4 votes |
TessBaseAPI getBaseApi() { return baseApi; }
Example #16
Source File: TesseractImageTextReader4.java From loco-answers with GNU General Public License v3.0 | 4 votes |
public static TesseractImageTextReader4 geInstance(String path, String language) { api = new TessBaseAPI(); api.init(path, language); api.setPageSegMode(TessBaseAPI.PageSegMode.PSM_AUTO_OSD); return new TesseractImageTextReader4(); }
Example #17
Source File: TesseractImageTextReader.java From loco-answers with GNU General Public License v3.0 | 4 votes |
public static TesseractImageTextReader geInstance(String path,String language) { api = new TessBaseAPI(); api.init(path, language); api.setPageSegMode(TessBaseAPI.PageSegMode.PSM_AUTO_OSD); return new TesseractImageTextReader(); }
Example #18
Source File: CropActivity.java From reader with MIT License | 4 votes |
public String doOcr(Bitmap bitmap, String language) { TessBaseAPI baseApi = new TessBaseAPI(); baseApi.init(getSDPath(), language); bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); baseApi.setImage(bitmap); String text = baseApi.getUTF8Text(); baseApi.clear(); baseApi.end(); return text; }
Example #19
Source File: CropImage.java From reader with MIT License | 4 votes |
public String doOcr(Bitmap bitmap, String language) { TessBaseAPI baseApi = new TessBaseAPI(); baseApi.init(getSDPath(), language); bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); baseApi.setImage(bitmap); String text = baseApi.getUTF8Text(); baseApi.clear(); baseApi.end(); return text; }
Example #20
Source File: MainActivity.java From reader with MIT License | 4 votes |
/** * ����ͼƬʶ�� * * @param bitmap * ��ʶ��ͼƬ * @param language * ʶ������ * @return ʶ�����ַ��� */ public String doOcr(Bitmap bitmap, String language) { TessBaseAPI baseApi = new TessBaseAPI(); baseApi.init(getSDPath(), language); // ����Ӵ��У�tess-twoҪ��BMP����Ϊ������ bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); baseApi.setImage(bitmap); String text = baseApi.getUTF8Text(); baseApi.clear(); baseApi.end(); return text; }
Example #21
Source File: OcrInitAsyncTask.java From android-mrz-reader with Apache License 2.0 | 3 votes |
/** * AsyncTask to asynchronously download data and initialize Tesseract. * * @param activity * The calling activity * @param baseApi * API to the OCR engine * @param dialog * Dialog box with thermometer progress indicator * @param indeterminateDialog * Dialog box with indeterminate progress indicator * @param languageCode * ISO 639-2 OCR language code * @param languageName * Name of the OCR language, for example, "English" * @param ocrEngineMode * Whether to use Tesseract, Cube, or both */ OcrInitAsyncTask(CaptureActivity activity, TessBaseAPI baseApi, ProgressDialog dialog, ProgressDialog indeterminateDialog, String languageCode, String languageName, int ocrEngineMode) { this.activity = activity; this.context = activity.getBaseContext(); this.baseApi = baseApi; this.dialog = dialog; this.indeterminateDialog = indeterminateDialog; this.languageCode = languageCode; this.languageName = languageName; this.ocrEngineMode = ocrEngineMode; }