I am extracting text from a pdf file which is having row text and some image reports which is having text. Which OCR technique should I use to extract the row text and the text which is in the Image. I have tried some OCR techniques which are either doing OCR or reading the text from pdf. I need both in a single go. Thanks.
1 Answers
1
votes
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.SAXException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class ContentsExtraction {
private static void extractFromFile(final Parser parser, final String fileName)throws IOException,SAXException,TikaException {
long start =System.currentTimeMillis();
BodyContentHandler handler=new BodyContentHandler(10000000);
Metadata metaData=new Metadata();
FileInputStream content=new FileInputStream(fileName);
parser.parse(content,handler,metaData,new ParseContext());
System.out.println("\n\n File content : "+handler.toString());
}
private static void extract(final String fileName,final String writePath)throws IOException{
File image=new File(fileName);
Tesseract tessInst=new Tesseract();
try{
tessInst.setDatapath("./tessdata");
String result = tessInst.doOCR(image);
System.out.println(result);
}catch (TesseractException e){
System.err.println(e.getMessage());
}
}
public static void main(String[] args)throws IOException,SAXException,TikaException {
String path="input.pdf";
String writePathImage=null;
String[] ext=path.split("\\.");
Parser parser=new AutoDetectParser();
if(ext[1].equalsIgnoreCase("png")||ext[1].equalsIgnoreCase("jpg")||ext[1].equalsIgnoreCase("pdf"))
{
writePathImage="output.txt";
extractFromFile(parser,path);
extractPdfImage(path,writePathImage);
}else
{
extractFromFile(parser,path);
}
System.out.println("Processing Completed");
}
}