@Override protected void finalize() throws Throwable { try { if (!mRecycled) { Log.w(TAG, "Pixa was not terminated using recycle()"); recycle(); } } finally { super.finalize(); } }
private void testGetUTF8Text(String language, int ocrEngineMode) { 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, language, ocrEngineMode); baseApi.setPageSegMode(TessBaseAPI.PageSegMode.PSM_SINGLE_LINE); baseApi.setImage(bmp); // Ensure that the result is correct. final String outputText = baseApi.getUTF8Text(); assertEquals("\"" + outputText + "\" != \"" + inputText + "\"", inputText, outputText); // Ensure getRegions() works. final Pixa regions = baseApi.getRegions(); assertEquals("Found incorrect number of regions.", regions.size(), 1); regions.recycle(); // Ensure getTextlines() works. final Pixa textlines = baseApi.getTextlines(); assertEquals("Found incorrect number of textlines.", textlines.size(), 1); textlines.recycle(); // Ensure getStrips() works. final Pixa strips = baseApi.getStrips(); assertEquals("Found incorrect number of strips.", strips.size(), 1); strips.recycle(); // Ensure getWords() works. final Pixa words = baseApi.getWords(); assertEquals("Found incorrect number of words.", words.size(), 1); words.recycle(); // Ensure getConnectedComponents() works. final Pixa connectedComponents = baseApi.getConnectedComponents(); assertTrue("Connected components not found.", connectedComponents.size() > 0); connectedComponents.recycle(); // Iterate through the results. final ResultIterator iterator = baseApi.getResultIterator(); String lastUTF8Text; float lastConfidence; int[] lastBoundingBox; Rect lastBoundingRect; int count = 0; iterator.begin(); do { lastUTF8Text = iterator.getUTF8Text(PageIteratorLevel.RIL_WORD); lastConfidence = iterator.confidence(PageIteratorLevel.RIL_WORD); lastBoundingBox = iterator.getBoundingBox(PageIteratorLevel.RIL_WORD); lastBoundingRect = iterator.getBoundingRect(PageIteratorLevel.RIL_WORD); count++; } while (iterator.next(PageIteratorLevel.RIL_WORD)); iterator.delete(); assertEquals("Found incorrect number of results.", count, 1); assertEquals("Found an incorrect result.", lastUTF8Text, outputText); assertTrue("Result was not high-confidence.", lastConfidence > 80); assertTrue("Result bounding box not found.", lastBoundingBox[2] > 0 && lastBoundingBox[3] > 0); boolean validBoundingRect = lastBoundingRect.left < lastBoundingRect.right && lastBoundingRect.top < lastBoundingRect.bottom; assertTrue("Result bounding box Rect is incorrect.", validBoundingRect); // Attempt to shut down the API. baseApi.end(); bmp.recycle(); }