-2

I am using Tess4j API for performing OCR and have created a dynamic web project in eclipse. If I create a new java class directly under the Java resources folder, the code is working fine.

    public static void main(String[] args){
        File image = new File("Scan0008.jpg");  
        ITesseract instance = new Tesseract();
        try{
            String result = instance.doOCR(image);
            System.out.println(result);
        }catch(TesseractException e){
            System.err.println(e.getMessage());
        }
    }

However I am getting an exception when I am calling the same code from my Servlets doPost method.

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Validate valObj = new Validate();
            valObj.validate();
    }
    public void validate() {
        File image = new File("Scan0008.jpg");
        ITesseract instance = new Tesseract();
        try {
            String result = instance.doOCR(image);
            System.out.println(result);
        } catch (TesseractException e) {
            System.err.println(e.getMessage());
        }
    }

I have included all the required jars under lib folder of WEB-INF. Have also added the jars in the projects build path. Could anyone please let me know what I am doing wrong.
Exception :
java.lang.IllegalStateException: Input not set
23:33:45.002 [http-bio-8080-exec-5] ERROR net.sourceforge.tess4j.Tesseract - Input not set
java.lang.IllegalStateException: Input not set

Shirin Safaeian
  • 950
  • 6
  • 18
Saurabh Gour
  • 643
  • 2
  • 8
  • 24
  • Please [format your post](http://stackoverflow.com/editing-help). This is a mess and won't get read in its current form. – tnw Apr 22 '16 at 18:10
  • Are you certain the servlet version is finding the specified file? Try adding an output after the `File image = ...` to ensure the file exists and is readable. – KevinO Apr 22 '16 at 18:19
  • @KevinO .. Just added a System.out.println() after the File image = .. line and it outputs on the screen..so i think the file is readable – Saurabh Gour Apr 22 '16 at 18:22
  • 1
    please print image.exists(). if this not exist, it will not throw exception, but exists() method will return false. – Shirin Safaeian Apr 22 '16 at 18:24
  • 1
    @SaurabhGour, `new File(some_file)` does **not** throw an exception if the file is not at the expected location. Just printing something doesn't help. You should **explicitly** check `if (! image.exists()) { display error }`. – KevinO Apr 22 '16 at 18:24
  • @KevinO ..The image was not readable..Now i have given the absolute path but getting another exception java.lang.Error: Invalid memory access – Saurabh Gour Apr 22 '16 at 18:39
  • please update your question or post a new question, and please use formatting. – Shirin Safaeian Apr 22 '16 at 18:44
  • Hi @KevinO..thank you..changing the image path to absolute worked !! – Saurabh Gour Apr 22 '16 at 18:49

1 Answers1

0

I think your current directory is different when you are calling from servlet. the current directory is you tomcat bin folder. so when you are calling like this:

File image = new File("Scan0008.jpg");

your scan0008.jpg must be put in bin folder of tomcat or you must use absolute path of your file.

Shirin Safaeian
  • 950
  • 6
  • 18