diff --git a/image-processing/pom.xml b/image-processing/pom.xml index 09dd29556e..806cccf351 100644 --- a/image-processing/pom.xml +++ b/image-processing/pom.xml @@ -50,12 +50,24 @@ imageio-bmp ${imageio.version} + + net.sourceforge.tess4j + tess4j + ${tess4j.version} + + + org.bytedeco + tesseract-platform + ${tesseract-platform.version} + - + 1.3.5 1.51h 3.3.2 + 4.5.1 + 4.1.0-1.5.2 \ No newline at end of file diff --git a/image-processing/src/main/java/com/baeldung/tesseract/Tess4JExample.java b/image-processing/src/main/java/com/baeldung/tesseract/Tess4JExample.java new file mode 100644 index 0000000000..c647de2372 --- /dev/null +++ b/image-processing/src/main/java/com/baeldung/tesseract/Tess4JExample.java @@ -0,0 +1,28 @@ +package com.baeldung.tesseract; + +import java.awt.Rectangle; +import java.io.File; + +import net.sourceforge.tess4j.Tesseract; +import net.sourceforge.tess4j.TesseractException; + +public class Tess4JExample { + + public static void main(String[] args) { + String result = null; + try { + File image = new File("src/main/resources/images/baeldung.png"); + Tesseract tesseract = new Tesseract(); + tesseract.setLanguage("spa"); + tesseract.setPageSegMode(1); + tesseract.setOcrEngineMode(1); + tesseract.setHocr(true); + tesseract.setDatapath("src/main/resources/tessdata"); + result = tesseract.doOCR(image, new Rectangle(1200, 200)); + } catch (TesseractException e) { + e.printStackTrace(); + } + System.out.println(result); + } + +} diff --git a/image-processing/src/main/java/com/baeldung/tesseract/TesseractPlatformExample.java b/image-processing/src/main/java/com/baeldung/tesseract/TesseractPlatformExample.java new file mode 100644 index 0000000000..61a2e39f2a --- /dev/null +++ b/image-processing/src/main/java/com/baeldung/tesseract/TesseractPlatformExample.java @@ -0,0 +1,26 @@ +package com.baeldung.tesseract; + +import org.bytedeco.javacpp.BytePointer; +import org.bytedeco.leptonica.PIX; +import org.bytedeco.tesseract.TessBaseAPI; + +public class TesseractPlatformExample { + + @SuppressWarnings("resource") + public static void main(String[] args) { + try { + TessBaseAPI tessApi = new TessBaseAPI(); + tessApi.Init("src/main/resources/tessdata", "eng", 3); + tessApi.SetPageSegMode(1); + PIX image = org.bytedeco.leptonica.global.lept.pixRead("src/main/resources/images/baeldung.png"); + tessApi.SetImage(image); + + BytePointer outText = tessApi.GetUTF8Text(); + System.out.println(outText.getString()); + tessApi.End(); + } catch(Exception e) { + e.printStackTrace(); + } + } + +} diff --git a/image-processing/src/main/resources/images/baeldung.png b/image-processing/src/main/resources/images/baeldung.png new file mode 100644 index 0000000000..86155fa3ec Binary files /dev/null and b/image-processing/src/main/resources/images/baeldung.png differ diff --git a/image-processing/src/main/resources/images/multiLanguageText.png b/image-processing/src/main/resources/images/multiLanguageText.png new file mode 100644 index 0000000000..af15783eb8 Binary files /dev/null and b/image-processing/src/main/resources/images/multiLanguageText.png differ diff --git a/image-processing/src/main/resources/images/output.txt b/image-processing/src/main/resources/images/output.txt new file mode 100644 index 0000000000..8f5ad715a0 --- /dev/null +++ b/image-processing/src/main/resources/images/output.txt @@ -0,0 +1,9 @@ +Der ,.schnelle” braune Fuchs springt +iiber den faulen Hund. Le renard brun +«rapide» saute par-dessus le chien +paresseux. La volpe marrone rapida +salta sopra il cane pigro. El zorro +marron rapido salta sobre el perro +perezoso. A raposa marrom rapida +salta sobre 0 cao preguicoso. + \ No newline at end of file