diff --git a/tess-two-test/src/com/googlecode/leptonica/android/test/PixTest.java b/tess-two-test/src/com/googlecode/leptonica/android/test/PixTest.java index 0f2f76928..5470b961a 100644 --- a/tess-two-test/src/com/googlecode/leptonica/android/test/PixTest.java +++ b/tess-two-test/src/com/googlecode/leptonica/android/test/PixTest.java @@ -22,36 +22,84 @@ import junit.framework.TestCase; import android.graphics.Bitmap; -import android.graphics.Color; import android.graphics.Bitmap.CompressFormat; +import android.graphics.Color; import android.test.suitebuilder.annotation.SmallTest; import com.googlecode.leptonica.android.Pix; import com.googlecode.leptonica.android.ReadFile; public class PixTest extends TestCase { + @SmallTest + public void testGetData() throws IOException { + File file = File.createTempFile("testGetData", ".jpg"); + FileOutputStream fileStream = new FileOutputStream(file); + Bitmap bmp = Bitmap.createBitmap(640, 480, Bitmap.Config.RGB_565); + bmp.compress(CompressFormat.JPEG, 85, fileStream); + Pix pix = ReadFile.readFile(file); + + byte[] pixData = pix.getData(); + assertNotNull(pixData); + + bmp.recycle(); + pix.recycle(); + } + + @SmallTest + public void testGetDimensions() { + int width = 640; + int height = 480; + int depth = 32; + Pix pix = new Pix(width, height, depth); + + int[] dimens = pix.getDimensions(); + + assertEquals("Incorrect width value found.", width, dimens[Pix.INDEX_W]); + assertEquals("Incorrect height value found.", height, + dimens[Pix.INDEX_H]); + assertEquals("Incorrect bit-depth value found.", depth, + dimens[Pix.INDEX_D]); + } + + @SmallTest + public void testPixClone() { + Pix pix = new Pix(640, 480, 32); + Pix pixCopy = pix.clone(); + + // The clone should not have the same native pointer. + assertNotSame(pix.getNativePix(), pixCopy.getNativePix()); + + // The clone should share the same backing data. + pix.setPixel(0, 0, Color.RED); + assertEquals(Color.RED, pixCopy.getPixel(0, 0)); + + // Finally, we should be able to recycle both Pix. + pix.recycle(); + pixCopy.recycle(); + } + @SmallTest public void testPixCreate() { testPixCreate(1, 1, 1); testPixCreate(640, 480, 32); } - + private void testPixCreate(int w, int h, int d) { Pix pix = new Pix(w, h, d); - + // Make sure the dimensions were set correctly. assertEquals(w, pix.getWidth()); assertEquals(h, pix.getHeight()); assertEquals(d, pix.getDepth()); - + // Make sure we can recycle the Pix. pix.recycle(); } - + @SmallTest public void testPixPixelOps() { Pix pix = new Pix(640, 480, 32); - + // Set various pixel colors. pix.setPixel(0, 0, Color.RED); pix.setPixel(1, 0, Color.BLUE); @@ -65,42 +113,7 @@ public void testPixPixelOps() { assertEquals(Color.GREEN, pix.getPixel(2, 0)); assertEquals(Color.BLACK, pix.getPixel(3, 0)); assertEquals(Color.WHITE, pix.getPixel(4, 0)); - - pix.recycle(); - } - - @SmallTest - public void testPixClone() { - Pix pix = new Pix(640, 480, 32); - Pix pixCopy = pix.clone(); - - // The clone should not have the same native pointer. - assertNotSame(pix.getNativePix(), pixCopy.getNativePix()); - - // The clone should share the same backing data. - pix.setPixel(0, 0, Color.RED); - assertEquals(Color.RED, pixCopy.getPixel(0, 0)); - - // Finally, we should be able to recycle both Pix. - pix.recycle(); - pixCopy.recycle(); - } - - @SmallTest - public void testGetData() throws IOException { - File file = File.createTempFile("testGetData", "jpg"); - FileOutputStream fileStream = new FileOutputStream(file); - Bitmap bmp = Bitmap.createBitmap(640, 480, Bitmap.Config.RGB_565); - bmp.compress(CompressFormat.JPEG, 85, fileStream); - Pix pix = ReadFile.readFile(file); - - byte[] pixData = pix.getData(); - Pix pixFromBytes = Pix.createFromPix(pixData, 640, 480, 16); - - assertNotNull(pixFromBytes); - fileStream.close(); - bmp.recycle(); pix.recycle(); } } diff --git a/tess-two-test/src/com/googlecode/leptonica/android/test/ReadFileTest.java b/tess-two-test/src/com/googlecode/leptonica/android/test/ReadFileTest.java index 11af38d00..dd20f7072 100644 --- a/tess-two-test/src/com/googlecode/leptonica/android/test/ReadFileTest.java +++ b/tess-two-test/src/com/googlecode/leptonica/android/test/ReadFileTest.java @@ -16,29 +16,27 @@ package com.googlecode.leptonica.android.test; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + +import junit.framework.TestCase; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.Paint.Style; -import android.graphics.Rect; import android.test.suitebuilder.annotation.SmallTest; +import android.util.Log; import com.googlecode.leptonica.android.Pix; import com.googlecode.leptonica.android.ReadFile; -import junit.framework.TestCase; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; - /** - * @author alanv@google.com (Your Name Here) + * @author alanv@google.com (Alan Viverette) */ public class ReadFileTest extends TestCase { + + private static final String TAG = ReadFileTest.class.getSimpleName(); + @SmallTest public void testReadBitmap() { testReadBitmap(1, 1, Bitmap.Config.ARGB_8888); @@ -46,57 +44,64 @@ public void testReadBitmap() { } private void testReadBitmap(int width, int height, Bitmap.Config format) { - Bitmap bmp = Bitmap.createBitmap(width, height, format); - Canvas canvas = new Canvas(bmp); - Paint paint = new Paint(); - - if (width > 1 && height > 1) { - // Paint the left half white - paint.setColor(Color.WHITE); - paint.setStyle(Style.FILL); - canvas.drawRect(new Rect(0, 0, width / 2 , height - 1), paint); - - // Paint the right half black - paint.setColor(Color.BLACK); - paint.setStyle(Style.FILL); - canvas.drawRect(new Rect(width / 2, 0, width - 1, height - 1), paint); - } - + Bitmap bmp = TestUtils.createTestBitmap(640, 480, format); Pix pix = ReadFile.readBitmap(bmp); assertEquals(bmp.getWidth(), pix.getWidth()); assertEquals(bmp.getHeight(), pix.getHeight()); - if (width > 1 && height > 1) { - // Make sure the colors were preserved. - assertEquals(Color.WHITE, pix.getPixel(0, 0)); - assertEquals(Color.BLACK, pix.getPixel(width - 1, height - 1)); - } + float match = TestUtils.compareImages(pix, bmp); + Log.d(TAG, "match=" + match); + assertTrue("Images do not match.", (match >= 0.99f)); bmp.recycle(); pix.recycle(); } @SmallTest - public void testReadFile() throws IOException { - File file = File.createTempFile("testReadFile", "jpg"); + public void testReadFile_bmp() throws IOException { + File file = File.createTempFile("testReadFile", ".bmp"); FileOutputStream fileStream = new FileOutputStream(file); - Bitmap bmp = Bitmap.createBitmap(640, 480, Bitmap.Config.RGB_565); + Bitmap bmp = TestUtils.createTestBitmap(640, 480, Bitmap.Config.ARGB_8888); + bmp.compress(CompressFormat.PNG, 100, fileStream); + Pix pix = ReadFile.readFile(file); + + assertEquals(bmp.getWidth(), pix.getWidth()); + assertEquals(bmp.getHeight(), pix.getHeight()); + + float match = TestUtils.compareImages(pix, bmp); + Log.d(TAG, "match=" + match); + assertTrue("Images do not match.", (match >= 0.99f)); + + fileStream.close(); + bmp.recycle(); + pix.recycle(); + } + + @SmallTest + public void testReadFile_jpg() throws IOException { + File file = File.createTempFile("testReadFile", ".jpg"); + FileOutputStream fileStream = new FileOutputStream(file); + Bitmap bmp = TestUtils.createTestBitmap(640, 480, Bitmap.Config.ARGB_8888); bmp.compress(CompressFormat.JPEG, 85, fileStream); Pix pix = ReadFile.readFile(file); assertEquals(bmp.getWidth(), pix.getWidth()); assertEquals(bmp.getHeight(), pix.getHeight()); + float match = TestUtils.compareImages(pix, bmp); + Log.d(TAG, "match=" + match); + assertTrue("Images do not match. match=" + match, (match >= 0.99f)); + fileStream.close(); bmp.recycle(); pix.recycle(); } @SmallTest - public void testReadMem() throws IOException { + public void testReadMem_jpg() throws IOException { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); - Bitmap bmp = Bitmap.createBitmap(640, 480, Bitmap.Config.RGB_565); + Bitmap bmp = TestUtils.createTestBitmap(640, 480, Bitmap.Config.ARGB_8888); bmp.compress(CompressFormat.JPEG, 85, byteStream); byte[] encodedData = byteStream.toByteArray(); Pix pix = ReadFile.readMem(encodedData); @@ -104,7 +109,9 @@ public void testReadMem() throws IOException { assertEquals(bmp.getWidth(), pix.getWidth()); assertEquals(bmp.getHeight(), pix.getHeight()); - // TODO(alanv): Need some way to test content, ex. Pix.getPixel(int, int) + float match = TestUtils.compareImages(pix, bmp); + Log.d(TAG, "match=" + match); + assertTrue("Images do not match. match=" + match, (match >= 0.99f)); byteStream.close(); bmp.recycle(); diff --git a/tess-two-test/src/com/googlecode/leptonica/android/test/SkewTest.java b/tess-two-test/src/com/googlecode/leptonica/android/test/SkewTest.java index 28885d820..91b57d90d 100644 --- a/tess-two-test/src/com/googlecode/leptonica/android/test/SkewTest.java +++ b/tess-two-test/src/com/googlecode/leptonica/android/test/SkewTest.java @@ -41,7 +41,7 @@ public void testFindSkew() { testFindSkew(SENTENCE, 640, 480, 15.0f); } - private void testFindSkew(String text, int width, int height, float requestedSkew) { + private void testFindSkew(String text, int width, int height, float skew) { Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Paint paint = new Paint(); Canvas canvas = new Canvas(bmp); @@ -53,14 +53,16 @@ private void testFindSkew(String text, int width, int height, float requestedSke paint.setTextSize(32.0f); canvas.drawColor(Color.WHITE); - canvas.rotate(requestedSkew, width / 2, height / 2); + canvas.rotate(skew, width / 2, height / 2); canvas.drawText(SENTENCE, width / 2, height / 2 , paint); Pix pixs = ReadFile.readBitmap(bmp); Pix pixd = GrayQuant.pixThresholdToBinary(pixs, 1); + pixs.recycle(); float measuredSkew = -Skew.findSkew(pixd); + pixd.recycle(); - boolean isInRange = requestedSkew - 1 < measuredSkew && measuredSkew < requestedSkew + 1; + boolean isInRange = skew - 1 < measuredSkew && measuredSkew < skew + 1; assertTrue("Skew has incorrect value.", isInRange); } } diff --git a/tess-two-test/src/com/googlecode/leptonica/android/test/TestUtils.java b/tess-two-test/src/com/googlecode/leptonica/android/test/TestUtils.java index 947a9a0c5..e86952843 100644 --- a/tess-two-test/src/com/googlecode/leptonica/android/test/TestUtils.java +++ b/tess-two-test/src/com/googlecode/leptonica/android/test/TestUtils.java @@ -17,6 +17,14 @@ package com.googlecode.leptonica.android.test; import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Paint.Style; +import android.graphics.Rect; + +import com.googlecode.leptonica.android.Pix; +import com.googlecode.leptonica.android.ReadFile; /** * Utility methods for running Leptonica unit tests. @@ -29,7 +37,35 @@ public static float compareBitmaps(Bitmap a, Bitmap b) { for (int y = 0; y < a.getHeight(); y++) { for (int x = 0; x < a.getWidth(); x++) { - if (a.getPixel(x, y) == a.getPixel(x, y)) { + if (a.getPixel(x, y) == b.getPixel(x, y)) { + found++; + } + } + } + + return found / (float)(a.getWidth() * a.getHeight()); + } + + public static float compareImages(Pix a, Bitmap b) { + int found = 0; + + for (int y = 0; y < a.getHeight(); y++) { + for (int x = 0; x < a.getWidth(); x++) { + if (a.getPixel(x, y) == b.getPixel(x, y)) { + found++; + } + } + } + + return found / (float)(a.getWidth() * a.getHeight()); + } + + public static float comparePix(Pix a, Pix b) { + int found = 0; + + for (int y = 0; y < a.getHeight(); y++) { + for (int x = 0; x < a.getWidth(); x++) { + if (a.getPixel(x, y) == b.getPixel(x, y)) { found++; } } @@ -37,4 +73,28 @@ public static float compareBitmaps(Bitmap a, Bitmap b) { return found / (float)(a.getWidth() * a.getHeight()); } + + public static Bitmap createTestBitmap(int width, int height, Bitmap.Config format) { + Bitmap bmp = Bitmap.createBitmap(width, height, format); + Canvas canvas = new Canvas(bmp); + Paint paint = new Paint(); + + if (width > 1 && height > 1) { + // Paint the left half white + paint.setColor(Color.WHITE); + paint.setStyle(Style.FILL); + canvas.drawRect(new Rect(0, 0, width / 2 , height - 1), paint); + + // Paint the right half black + paint.setColor(Color.BLACK); + paint.setStyle(Style.FILL); + canvas.drawRect(new Rect(width / 2, 0, width - 1, height - 1), paint); + } + return bmp; + } + + public static Pix createTestPix(int width, int height) { + Bitmap bmp = TestUtils.createTestBitmap(width, height, Bitmap.Config.ARGB_8888); + return ReadFile.readBitmap(bmp); + } } \ No newline at end of file diff --git a/tess-two-test/src/com/googlecode/leptonica/android/test/WriteFileTest.java b/tess-two-test/src/com/googlecode/leptonica/android/test/WriteFileTest.java index ad6e25aeb..3caa5b4df 100644 --- a/tess-two-test/src/com/googlecode/leptonica/android/test/WriteFileTest.java +++ b/tess-two-test/src/com/googlecode/leptonica/android/test/WriteFileTest.java @@ -16,7 +16,81 @@ package com.googlecode.leptonica.android.test; +import java.io.File; +import java.io.IOException; + import junit.framework.TestCase; +import android.graphics.Bitmap; +import android.test.suitebuilder.annotation.SmallTest; + +import com.googlecode.leptonica.android.Pix; +import com.googlecode.leptonica.android.ReadFile; +import com.googlecode.leptonica.android.WriteFile; public class WriteFileTest extends TestCase { + @SmallTest + public void testWriteBitmap() { + testWriteBitmap(1, 1); + testWriteBitmap(640, 480); + } + + private void testWriteBitmap(int width, int height) { + Pix pix = TestUtils.createTestPix(width, height); + Bitmap bmp = WriteFile.writeBitmap(pix); + + assertEquals(pix.getWidth(), bmp.getWidth()); + assertEquals(pix.getHeight(), bmp.getHeight()); + + float match = TestUtils.compareImages(pix, bmp); + pix.recycle(); + bmp.recycle(); + + assertTrue("Images do not match.", (match >= 0.99f)); + } + + @SmallTest + public void testWriteBytes8() { + testWriteBytes8(1, 1); + testWriteBytes8(640, 480); + } + + private static void testWriteBytes8(int width, int height) { + Pix pixs = TestUtils.createTestPix(width, height); + byte[] data = WriteFile.writeBytes8(pixs); + Pix pixd = ReadFile.readBytes8(data, width, height); + + assertEquals(pixs.getWidth(), pixd.getWidth()); + assertEquals(pixs.getHeight(), pixd.getHeight()); + + float match = TestUtils.comparePix(pixs, pixd); + pixs.recycle(); + pixd.recycle(); + + assertTrue("Images do not match.", (match >= 0.99f)); + } + + @SmallTest + public void testWriteImpliedFormat_bmp() throws IOException { + Pix pixs = TestUtils.createTestPix(100, 100); + File file = File.createTempFile("testWriteImpliedFormat", ".bmp"); + testWriteImpliedFormat(pixs, file); + } + + private void testWriteImpliedFormat(Pix pixs, File file) { + boolean success = WriteFile.writeImpliedFormat(pixs, file); + + assertTrue("Writing to file failed.", success); + assertTrue("File does not exist.", file.exists()); + assertTrue("File does not contain data.", file.length() > 0); + + Pix pixd = ReadFile.readFile(file); + + assertNotNull("Pix is null", pixd); + + float match = TestUtils.comparePix(pixs, pixd); + pixs.recycle(); + pixd.recycle(); + + assertTrue("Images do not match.", (match >= 0.99f)); + } } diff --git a/tess-two-test/src/com/googlecode/tesseract/android/test/TessBaseAPITest.java b/tess-two-test/src/com/googlecode/tesseract/android/test/TessBaseAPITest.java index 2c2ff03bd..4d673207b 100644 --- a/tess-two-test/src/com/googlecode/tesseract/android/test/TessBaseAPITest.java +++ b/tess-two-test/src/com/googlecode/tesseract/android/test/TessBaseAPITest.java @@ -17,10 +17,13 @@ package com.googlecode.tesseract.android.test; import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; import java.util.List; import junit.framework.TestCase; import android.graphics.Bitmap; +import android.graphics.Bitmap.CompressFormat; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; @@ -30,6 +33,7 @@ import android.test.suitebuilder.annotation.SmallTest; import android.util.Pair; +import com.googlecode.leptonica.android.Pix; import com.googlecode.leptonica.android.Pixa; import com.googlecode.tesseract.android.ResultIterator; import com.googlecode.tesseract.android.TessBaseAPI; @@ -40,44 +44,110 @@ public class TessBaseAPITest extends TestCase { private static final String DEFAULT_LANGUAGE = "eng"; private static final String EXPECTED_FILE = TESSBASE_PATH + "tessdata/" + DEFAULT_LANGUAGE + ".traineddata"; + private static final int DEFAULT_PAGE_SEG_MODE = TessBaseAPI.PageSegMode.PSM_SINGLE_BLOCK; @SmallTest - public void testInit() { + 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> choicesAndConfidences; + iterator.begin(); + do { + choicesAndConfidences = iterator.getChoicesAndConfidence(PageIteratorLevel.RIL_SYMBOL); + assertNotNull("Invalid result.", choicesAndConfidences); + + for (Pair 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)); + + assertNotNull("No ChoiceIterator values found.", choicesAndConfidences); + + // Attempt to shut down the API. + baseApi.end(); + bmp.recycle(); + } + + private static Bitmap getTextImage(String text, int width, int height) { + 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(24.0f); + canvas.drawText(text, width / 2, height / 2, paint); + + return bmp; + } + + @SmallTest + public void testClear() { // First, make sure the eng.traineddata file exists. assertTrue("Make sure that you've copied " + DEFAULT_LANGUAGE + ".traineddata to " + EXPECTED_FILE, new File(EXPECTED_FILE).exists()); + 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); + baseApi.setImage(bmp); + + // Ensure that the getUTF8Text() operation fails after clear() is called. + baseApi.clear(); + String text = baseApi.getUTF8Text(); + + assertNull("Received non-null result after clear().", text); // Attempt to shut down the API. baseApi.end(); + bmp.recycle(); } @SmallTest - public void testSetPageSegMode() { + public void testGetInitLanguagesAsString() { + // First, make sure the eng.traineddata file exists. + assertTrue("Make sure that you've copied " + DEFAULT_LANGUAGE + ".traineddata to " + + EXPECTED_FILE, new File(EXPECTED_FILE).exists()); + // Attempt to initialize the API. final TessBaseAPI baseApi = new TessBaseAPI(); baseApi.init(TESSBASE_PATH, DEFAULT_LANGUAGE); - // Check the default page segmentation mode. - final int defaultPageSegMode = TessBaseAPI.PageSegMode.PSM_SINGLE_BLOCK; - assertEquals("Found unexpected default page segmentation mode.", - baseApi.getPageSegMode(), defaultPageSegMode); - - // Ensure that the page segmentation mode can be changed. - final int newPageSegMode = TessBaseAPI.PageSegMode.PSM_SINGLE_CHAR; - baseApi.setPageSegMode(newPageSegMode); - assertEquals("Found unexpected page segmentation mode.", - baseApi.getPageSegMode(), newPageSegMode); + // Check the last-used language code. + String lang = baseApi.getInitLanguagesAsString(); + assertEquals("Got incorrect init languages value.", lang, DEFAULT_LANGUAGE); // Attempt to shut down the API. baseApi.end(); } @SmallTest - public void testSetImage() { + public void testGetThresholdedImage() { // First, make sure the eng.traineddata file exists. assertTrue("Make sure that you've copied " + DEFAULT_LANGUAGE + ".traineddata to " + EXPECTED_FILE, new File(EXPECTED_FILE).exists()); @@ -90,25 +160,16 @@ public void testSetImage() { final Bitmap bmp = Bitmap.createBitmap(640, 480, Bitmap.Config.ARGB_8888); baseApi.setImage(bmp); + // Check the size of the thresholded image. + Pix pixd = baseApi.getThresholdedImage(); + assertNotNull("Thresholded image is null.", pixd); + assertEquals(bmp.getWidth(), pixd.getWidth()); + assertEquals(bmp.getHeight(), pixd.getHeight()); + // Attempt to shut down the API. baseApi.end(); - } - - private static Bitmap getTextImage(String text, int width, int height) { - 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(24.0f); - canvas.drawText(text, width / 2, height / 2, paint); - - return bmp; + bmp.recycle(); + pixd.recycle(); } @SmallTest @@ -133,11 +194,19 @@ public void testGetUTF8Text() { // Ensure that getHOCRText() produced a result. final String hOcr = baseApi.getHOCRText(0); assertNotNull("HOCR result not found.", hOcr); - + // Ensure getRegions() works. final Pixa regions = baseApi.getRegions(); assertEquals("Found incorrect number of regions.", regions.size(), 1); + // Ensure getTextlines() works. + final Pixa textlines = baseApi.getTextlines(); + assertEquals("Found incorrect number of textlines.", textlines.size(), 1); + + // Ensure getStrips() works. + final Pixa strips = baseApi.getStrips(); + assertEquals("Found incorrect number of strips.", strips.size(), 1); + // Ensure getWords() works. final Pixa words = baseApi.getWords(); assertEquals("Found incorrect number of words.", words.size(), 1); @@ -166,7 +235,7 @@ public void testGetUTF8Text() { 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); @@ -176,6 +245,171 @@ public void testGetUTF8Text() { bmp.recycle(); } + @SmallTest + public void testInit() { + // First, make sure the eng.traineddata file exists. + assertTrue("Make sure that you've copied " + DEFAULT_LANGUAGE + ".traineddata to " + + EXPECTED_FILE, new File(EXPECTED_FILE).exists()); + + // Attempt to initialize the API. + final TessBaseAPI baseApi = new TessBaseAPI(); + baseApi.init(TESSBASE_PATH, DEFAULT_LANGUAGE); + + // Attempt to shut down the API. + baseApi.end(); + } + + @SmallTest + public void testInit_ocrEngineMode() { + // First, make sure the eng.traineddata file exists. + assertTrue("Make sure that you've copied " + DEFAULT_LANGUAGE + ".traineddata to " + + EXPECTED_FILE, new File(EXPECTED_FILE).exists()); + + // Attempt to initialize the API. + final TessBaseAPI baseApi = new TessBaseAPI(); + boolean result = baseApi.init(TESSBASE_PATH, DEFAULT_LANGUAGE, + TessBaseAPI.OEM_TESSERACT_ONLY); + + assertTrue("Init was unsuccessful.", result); + + // Attempt to shut down the API. + baseApi.end(); + } + + @SmallTest + public void testSetImage_bitmap() { + // First, make sure the eng.traineddata file exists. + assertTrue("Make sure that you've copied " + DEFAULT_LANGUAGE + ".traineddata to " + + EXPECTED_FILE, new File(EXPECTED_FILE).exists()); + + // Attempt to initialize the API. + final TessBaseAPI baseApi = new TessBaseAPI(); + baseApi.init(TESSBASE_PATH, DEFAULT_LANGUAGE); + + // Set the image to a Bitmap. + final Bitmap bmp = Bitmap.createBitmap(640, 480, Bitmap.Config.ARGB_8888); + baseApi.setImage(bmp); + + // Attempt to shut down the API. + baseApi.end(); + bmp.recycle(); + } + + @SmallTest + public void testSetImage_file() throws IOException { + // First, make sure the eng.traineddata file exists. + assertTrue("Make sure that you've copied " + DEFAULT_LANGUAGE + ".traineddata to " + + EXPECTED_FILE, new File(EXPECTED_FILE).exists()); + + // Attempt to initialize the API. + final TessBaseAPI baseApi = new TessBaseAPI(); + baseApi.init(TESSBASE_PATH, DEFAULT_LANGUAGE); + + // Create an image file. + File file = File.createTempFile("testSetImage", ".bmp"); + FileOutputStream fileStream = new FileOutputStream(file); + + Bitmap bmp = Bitmap.createBitmap(640, 480, Bitmap.Config.ARGB_8888); + bmp.compress(CompressFormat.JPEG, 85, fileStream); + + // Set the image to a File. + baseApi.setImage(file); + + // Attempt to shut down the API. + baseApi.end(); + bmp.recycle(); + } + + @SmallTest + public void testSetImage_pix() throws IOException { + // First, make sure the eng.traineddata file exists. + assertTrue("Make sure that you've copied " + DEFAULT_LANGUAGE + ".traineddata to " + + EXPECTED_FILE, new File(EXPECTED_FILE).exists()); + + // Attempt to initialize the API. + final TessBaseAPI baseApi = new TessBaseAPI(); + baseApi.init(TESSBASE_PATH, DEFAULT_LANGUAGE); + + // Set the image to a Pix. + Pix pix = new Pix(640, 480, 32); + baseApi.setImage(pix); + + // Attempt to shut down the API. + baseApi.end(); + pix.recycle(); + } + + @SmallTest + public void testSetPageSegMode() { + // Attempt to initialize the API. + final TessBaseAPI baseApi = new TessBaseAPI(); + baseApi.init(TESSBASE_PATH, DEFAULT_LANGUAGE); + + // Check the default page segmentation mode. + assertEquals("Found unexpected default page segmentation mode.", + baseApi.getPageSegMode(), DEFAULT_PAGE_SEG_MODE); + + // Ensure that the page segmentation mode can be changed. + final int newPageSegMode = TessBaseAPI.PageSegMode.PSM_SINGLE_CHAR; + baseApi.setPageSegMode(newPageSegMode); + assertEquals("Found unexpected page segmentation mode.", + baseApi.getPageSegMode(), newPageSegMode); + + // Attempt to shut down the API. + baseApi.end(); + } + + @SmallTest + public void testSetRectangle() { + // First, make sure the eng.traineddata file exists. + assertTrue("Make sure that you've copied " + DEFAULT_LANGUAGE + ".traineddata to " + + EXPECTED_FILE, new File(EXPECTED_FILE).exists()); + + // 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"; @@ -199,38 +433,37 @@ public void testSetVariable() { } @SmallTest - public void testChoiceIterator() { - final String inputText = "hello"; - final Bitmap bmp = TessBaseAPITest.getTextImage(inputText, 640, 480); + public void testWordConfidences() { + // First, make sure the eng.traineddata file exists. + assertTrue("Make sure that you've copied " + DEFAULT_LANGUAGE + ".traineddata to " + + EXPECTED_FILE, new File(EXPECTED_FILE).exists()); + + final String inputText = "one two three"; + final Bitmap bmp = 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); + baseApi.init(TESSBASE_PATH, DEFAULT_LANGUAGE); + baseApi.setPageSegMode(TessBaseAPI.PageSegMode.PSM_SINGLE_BLOCK); - // 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> choicesAndConfidences; - iterator.begin(); - do { - choicesAndConfidences = iterator.getChoicesAndConfidence(PageIteratorLevel.RIL_SYMBOL); - assertNotNull("Invalid result.", choicesAndConfidences); - - for (Pair 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)); - - assertNotNull("No ChoiceIterator values found.", choicesAndConfidences); + String text = baseApi.getUTF8Text(); + + assertNotNull("Recognized text is null.", text); + + // Ensure that a mean confidence value is returned. + int conf = baseApi.meanConfidence(); + boolean validConf = conf > 0 && conf <= 100; + assertTrue("Mean confidence value is incorrect.", validConf); + + // Ensure that word confidence values are returned. + int numWords = text.split("\\s+").length; + int[] wordConf = baseApi.wordConfidences(); + assertEquals("Found the wrong number of word confidence values.", numWords, wordConf.length); + for (int i = 0; i < wordConf.length; i++) { + boolean valid = 0 <= wordConf[i] && wordConf[i] <= 100; + assertTrue("Found an invalid word confidence value.", valid); + } // Attempt to shut down the API. baseApi.end();