Skip to content

Commit

Permalink
Add another C++ example applied to the Java API
Browse files Browse the repository at this point in the history
  • Loading branch information
ajeans committed Jan 17, 2019
1 parent 8e1e7b0 commit 62ff608
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tesseract/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,47 @@ public class BasicExample {
}
}
```

### The `src/main/java/GetComponentImagesExample.java` source file
```java
import org.bytedeco.javacpp.*;
import static org.bytedeco.javacpp.lept.*;
import static org.bytedeco.javacpp.tesseract.*;

public class GetComponentImagesExample {
public static void main(String[] args) {
BytePointer outText;

TessBaseAPI api = new TessBaseAPI();
// Initialize tesseract-ocr with English, without specifying tessdata path
if (api.Init(null, "eng") != 0) {
System.err.println("Could not initialize tesseract.");
System.exit(1);
}

// Open input image with leptonica library
PIX image = pixRead("/usr/src/tesseract/testing/phototest.tif");
api.SetImage(image);

int[] blockIds = {};
BOXA boxes = api.GetComponentImages(RIL_TEXTLINE, true, null, blockIds);

for (int i = 0; i < boxes.n(); i++) {
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);
}
}
```

0 comments on commit 62ff608

Please sign in to comment.