Press enter to see results or esc to cancel.


Simple java program code to convert Image to Text


You can convert any image file to text by Optical Character Recognition using the below java program.
The API we are using here is the Tesseract OCR which is free licensed.

Before running the below program you have to download and install Tesseract in your PC. Visit the below link to get the installation file,
http://chillyfacts.com/convert-image-to-text-using-cmd-prompt/

This project is internally calling CMD command prompt commands to execute the OCR.
1. Project Structure in Eclipse IDE.

2.my_main.java


package com.chillyfacts.com;
import java.io.PrintWriter;
public class my_main {
public static void main(String[] args) {
 String input_file="D:\\testfiles\\33.png";
 String output_file="D:\\testfiles\\33";
 String tesseract_install_path="D:\\Tesseract-OCR\\tesseract";
 String[] command =
    {
        "cmd",
    };
    Process p;
 try {
 p = Runtime.getRuntime().exec(command);
        new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
        new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
        PrintWriter stdin = new PrintWriter(p.getOutputStream());
        stdin.println("\""+tesseract_install_path+"\" \""+input_file+"\" \""+output_file+"\" -l eng");
        stdin.close();
        p.waitFor();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println(Read_File.read_a_file(output_file+".txt"));
    } catch (Exception e) {
 e.printStackTrace();
    }
  }
}

3.Read_File.java


package com.chillyfacts.com;
import java.io.BufferedReader;
import java.io.FileReader;
public class Read_File {
	public static String read_a_file(String file_name) {
		BufferedReader br = null; 
		String read_string="";
		try {
		String sCurrentLine;
		br = new BufferedReader(new FileReader(file_name));
		while ((sCurrentLine = br.readLine()) != null) {
		read_string=read_string+sCurrentLine;
		}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		try {
			if (br != null)br.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		}
		return read_string;
	}
}

4.SyncPipe.java


package com.chillyfacts.com;
import java.io.InputStream;
import java.io.OutputStream;
class SyncPipe implements Runnable
{
public SyncPipe(InputStream istrm, OutputStream ostrm) {
      istrm_ = istrm;
      ostrm_ = ostrm;
  }
  public void run() {
      try
      {
          final byte[] buffer = new byte[1024];
          for (int length = 0; (length = istrm_.read(buffer)) != -1; )
          {
              ostrm_.write(buffer, 0, length);
          }
      }
      catch (Exception e)
      {
          e.printStackTrace();
      }
  }
  private final OutputStream ostrm_;
  private final InputStream istrm_;
}

5. Test File for OCR.

6. After running the my_main.java as Java Application, The output is as shown in console.


7. Download the complete project here,

Java_imagetotext

Comments

Leave a Comment