Skip to content

Commit

Permalink
fix broken build on *nix caused by recent fixes; improve documentatio…
Browse files Browse the repository at this point in the history
…n; ensure trailing slash behavior on all OS
  • Loading branch information
tballison committed Apr 19, 2018
1 parent 85b2504 commit c68994f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.commons.io.FilenameUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
Expand Down Expand Up @@ -85,7 +86,7 @@ public enum OUTPUT_TYPE {
private int enableImageProcessing = 0;

// Path to ImageMagick program, if not on system path.
private String ImageMagickPath = "";
private String imageMagickPath = "";

// resolution of processed image (in dpi).
private int density = 300;
Expand Down Expand Up @@ -198,14 +199,19 @@ public String getTesseractPath() {
}

/**
* Set the path to the Tesseract executable, needed if it is not on system path.
* Set the path to the Tesseract executable's directory, needed if it is not on system path.
* <p>
* Note that if you set this value, it is highly recommended that you also
* set the path to the 'tessdata' folder using {@link #setTessdataPath}.
* </p>
*/
public void setTesseractPath(String tesseractPath) {
this.tesseractPath = FilenameUtils.normalize(tesseractPath);

tesseractPath = FilenameUtils.normalize(tesseractPath);
if (!tesseractPath.isEmpty() && !tesseractPath.endsWith(File.separator))
tesseractPath += File.separator;

this.tesseractPath = tesseractPath;
}

/**
Expand All @@ -221,7 +227,11 @@ public String getTessdataPath() {
* (such as when Tesseract is built from source), it may be located elsewhere.
*/
public void setTessdataPath(String tessdataPath) {
this.tessdataPath = FilenameUtils.normalize(tessdataPath);
tessdataPath = FilenameUtils.normalize(tessdataPath);
if (!tessdataPath.isEmpty() && !tessdataPath.endsWith(File.separator))
tessdataPath += File.separator;

this.tessdataPath = tessdataPath;
}

/**
Expand Down Expand Up @@ -515,21 +525,25 @@ public void setResize(int resize) {
}

/**
* @return path to ImageMagick file.
* @see #setImageMagickPath(String ImageMagickPath)
* @return path to ImageMagick executable directory.
* @see #setImageMagickPath(String imageMagickPath)
*/
public String getImageMagickPath() {

return ImageMagickPath;
return imageMagickPath;
}

/**
* Set the path to the ImageMagick executable, needed if it is not on system path.
* Set the path to the ImageMagick executable directory, needed if it is not on system path.
*
* @param ImageMagickPath to ImageMagick file.
* @param imageMagickPath to ImageMagick executable directory.
*/
public void setImageMagickPath(String ImageMagickPath) {
this.ImageMagickPath = FilenameUtils.normalize(ImageMagickPath);
public void setImageMagickPath(String imageMagickPath) {
imageMagickPath = FilenameUtils.normalize(imageMagickPath);
if (!imageMagickPath.isEmpty() && !imageMagickPath.endsWith(File.separator))
imageMagickPath += File.separator;

this.imageMagickPath = imageMagickPath;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ public boolean hasTesseract(TesseractOCRConfig config) {
TESSERACT_PRESENT.clear();
}
//check that the parent directory exists
if (! Files.isDirectory(Paths.get(config.getTesseractPath()))) {
if (! config.getTesseractPath().isEmpty() &&
! Files.isDirectory(Paths.get(config.getTesseractPath()))) {
TESSERACT_PRESENT.put(tesseract, false);
return false;
}
Expand All @@ -178,7 +179,8 @@ private boolean hasImageMagick(TesseractOCRConfig config) {
IMAGE_MAGICK_PRESENT.clear();
}
//check that directory exists
if (! Files.isDirectory(Paths.get(config.getImageMagickPath()))) {
if (!config.getImageMagickPath().isEmpty() &&
! Files.isDirectory(Paths.get(config.getImageMagickPath()))) {
IMAGE_MAGICK_PRESENT.put(ImageMagick, false);
return false;
}
Expand Down Expand Up @@ -378,7 +380,7 @@ private void processImage(File scratchFile, TesseractOCRConfig config) throws IO
"-density", Integer.toString(config.getDensity()),
"-depth ", Integer.toString(config.getDepth()),
"-colorspace", config.getColorspace(),
" -filter ", config.getFilter(),
"-filter", config.getFilter(),
"-resize", config.getResize() + "%",
"-rotate", angle,
scratchFile.getAbsolutePath(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,33 @@ public void testBogusPathCheck() {
//allow path that doesn't actually exist
TesseractOCRConfig config = new TesseractOCRConfig();
config.setTesseractPath("blahdeblahblah");
assertEquals("blahdeblahblah", config.getTesseractPath());
assertEquals("blahdeblahblah"+File.separator, config.getTesseractPath());
}

@Test
public void testTrailingSlashInPathBehavior() {

TesseractOCRConfig config = new TesseractOCRConfig();
config.setTesseractPath("blah");
assertEquals("blah"+File.separator, config.getTesseractPath());
config.setTesseractPath("blah"+File.separator);
assertEquals("blah"+File.separator, config.getTesseractPath());
config.setTesseractPath("");
assertEquals("", config.getTesseractPath());

config.setTessdataPath("blahdata");
assertEquals("blahdata"+File.separator, config.getTessdataPath());
config.setTessdataPath("blahdata"+File.separator);
assertEquals("blahdata"+File.separator, config.getTessdataPath());
config.setTessdataPath("");
assertEquals("", config.getTessdataPath());

config.setImageMagickPath("imagemagickpath");
assertEquals("imagemagickpath"+File.separator, config.getImageMagickPath());
config.setImageMagickPath("imagemagickpath"+File.separator);
assertEquals("imagemagickpath"+File.separator, config.getImageMagickPath());
config.setImageMagickPath("");
assertEquals("", config.getImageMagickPath());
}

@Test(expected=IllegalArgumentException.class)
Expand Down

0 comments on commit c68994f

Please sign in to comment.