Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add another C++ example applied to the Java API #673

Merged
merged 6 commits into from
Jan 19, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions tesseract/samples/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>tesseract-samples</artifactId>
<version>4.0.0-1.4.4</version>
<name>JavaCPP Presets Samples for Tesseract</name>

<properties>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.7</maven.compiler.source>
</properties>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>tesseract</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

package org.bytedeco.javacpp.samples.tesseract;

import org.bytedeco.javacpp.*;
import static org.bytedeco.javacpp.lept.*;
import static org.bytedeco.javacpp.tesseract.*;

/**
* To run this program, you need to configure:
* <ul>
* <li>A java property pointing to the JNI libraries
* -Djava.library.path="~/javacpp-presets/tesseract/target/classes/org/bytedeco/javacpp/linux-x86_64/:~/javacpp-presets/leptonica/target/classes/org/bytedeco/javacpp/linux-x86_64"</li>
saudet marked this conversation as resolved.
Show resolved Hide resolved
* <li>An environment variable pointing to the dictionaries installed on the system
* TESSDATA_PREFIX=/usr/share/tesseract-ocr/4.00</li>
* </ul>
*/
public class GetComponentImagesExample {
public static void main(String[] args) {
BytePointer outText;

TessBaseAPI api = new TessBaseAPI();
// Initialize tesseract-ocr with English, intializing tessdata path with the standard ENV variable
if (api.Init(System.getenv("TESSDATA_PREFIX") + "/tessdata", "eng") != 0) {
System.err.println("Could not initialize tesseract.");
System.exit(1);
}

// Open input image with leptonica library
PIX image = pixRead("src/main/resources/org/bytedeco/javacpp/samples/tesseract/Wikipedia-Computer_modern_sample.png");
api.SetImage(image);

// Lookup all component images
int[] blockIds = {};
BOXA boxes = api.GetComponentImages(RIL_TEXTLINE, true, null, blockIds);

for (int i = 0; i < boxes.n(); i++) {
// For each image box, OCR within its area
BOX box = boxes.box(i);
api.SetRectangle(box.x(), box.y(), box.w(), box.h());
outText = api.GetUTF8Text();
String ocrResult = outText.getString();
int conf = api.MeanTextConf();

String boxInformation = String.format("Box[%d]: x=%d, y=%d, w=%d, h=%d, confidence: %d, text: %s", i, box.x(), box.y(), box.w(), box.h(), conf, ocrResult);
System.out.println(boxInformation);

outText.deallocate();
}

// Destroy used object and release memory
api.End();
pixDestroy(image);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.