Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/main/java/net/sourceforge/tess4j/ITesseract.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ default String doOCR(BufferedImage bi, String filename, List<Rectangle> rects) t
* Sets tessdata path.
*
* @param datapath the tessdata path to set
* @throws IllegalArgumentException if the given datapath is not an existing directory
*/
void setDatapath(String datapath);

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/net/sourceforge/tess4j/Tesseract.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public Tesseract() {
datapath = "./";
}
}
File datapathFile = new File(datapath);
if (!datapathFile.exists() || !datapathFile.isDirectory()) {
throw new IllegalArgumentException("Given datapath " + datapath + " is not an existing directory.");
}
}

/**
Expand Down Expand Up @@ -114,6 +118,10 @@ protected TessBaseAPI getHandle() {
*/
@Override
public void setDatapath(String datapath) {
File datapathFile = new File(datapath);
if (!datapathFile.exists() || !datapathFile.isDirectory()) {
throw new IllegalArgumentException("Given datapath " + datapath + " is not an existing directory.");
}
this.datapath = datapath;
}

Expand Down
7 changes: 7 additions & 0 deletions src/test/java/net/sourceforge/tess4j/TesseractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,11 @@ public void testGetOSD() {
assertEquals("Latin", result.getScriptName());
assertTrue(result.getScriptConf() > 0);
}

@Test
public void testSetDatapath() {
logger.info("testSetDatapath");
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> instance.setDatapath("non-existent-datapath"));
assertEquals("Given datapath non-existent-datapath is not an existing directory.", e.getMessage());
}
}