A high recognition rate of Java character recognition technology _java

Source: Internet
Author: User
Tags eol locale stringbuffer

The key to the Java text Recognition program is to look for an OCR engine that can be invoked. TESSERACT-OCR is an OCR engine that was developed by HP Labs from 1985 to 1995 and is now in Google. TESSERACT-OCR 3.0 release, support Chinese. However, TESSERACT-OCR 3.0 is not a graphical interface to the client, others write FREEOCR graphical client does not support the import of new 3.0 Traineddata. But this marks that there is now free Chinese OCR software.

The steps for using tesseract-ocr3.01 in Java are as follows:

1. Download and install Tesseract-ocr-setup-3.01-1.exe (more than 3.0 version to increase the Chinese recognition)

2. In the Setup Wizard, you can choose which language packs you want to download.

3. To the web search download Java graphics processing required 2 packages: Jai_imageio-1.1-alpha.jar,swingx-1.6.1.jar

4.java Program list:

Imageiohelper class:

 Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import java.io.IOException;
Import Java.util.Iterator;

Import Java.util.Locale;
Import Javax.imageio.IIOImage;
Import Javax.imageio.ImageIO;
Import Javax.imageio.ImageReader;
Import Javax.imageio.ImageWriteParam;
Import Javax.imageio.ImageWriter;
Import Javax.imageio.metadata.IIOMetadata;
Import Javax.imageio.stream.ImageInputStream;

Import Javax.imageio.stream.ImageOutputStream;

Import Com.sun.media.imageio.plugins.tiff.TIFFImageWriteParam; public class Imageiohelper {public static file CreateImage (file ImageFile, String imageformat) {File Tempfile = 
  Null 
   try {iterator readers = imageio.getimagereadersbyformatname (imageformat); 
   
   ImageReader reader = Readers.next (); 
   Imageinputstream IIS = Imageio.createimageinputstream (ImageFile); 
   Reader.setinput (IIS); 
    
   Read the stream metadata iiometadata streammetadata = Reader.getstreammetadata (); Set up the Writeparam tiffimagewrIteparam Tiffwriteparam = new Tiffimagewriteparam (Locale.chinese); 
    
   Tiffwriteparam.setcompressionmode (imagewriteparam.mode_disabled); 
   Get TIF writer and set output to file iterator writers = Imageio.getimagewritersbyformatname ("Tiff"); 
    
   ImageWriter writer = Writers.next (); 
   BufferedImage bi = reader.read (0); 
   Iioimage image = New Iioimage (bi,null,reader.getimagemetadata (0)); 
   Tempfile = Tempimagefile (ImageFile); 
   Imageoutputstream iOS = Imageio.createimageoutputstream (tempfile); 
   Writer.setoutput (iOS); 
   Writer.write (streammetadata, image, Tiffwriteparam); 
    
   Ios.close (); 
   Writer.dispose (); 
    
  Reader.dispose (); 
  catch (IOException e) {e.printstacktrace (); 
 return tempfile; 
  private static file Tempimagefile (file imagefile) {String path = Imagefile.getpath (); 
  StringBuffer strb = new StringBuffer (path); 
  Strb.insert (Path.lastindexof ('. '), 0); return new File (Strb.tostring (). Replacefirst (? <=//.) 
 (//w+) $ ", tif")); 
 } 
 
}

OCR class:

 Package com.hhp.util; 
Import Java.io.BufferedReader; 
Import Java.io.File; 
Import Java.io.FileInputStream; 
Import Java.io.InputStreamReader; 
Import java.util.ArrayList; 
Import java.util.List; 
 
Import Org.jdesktop.swingx.util.OS; public class OCR {private final string lang_option = "L";//English Letter lowercase L, not number 1 private final string EOL = System.getprope 
 Rty ("Line.separator"); 
 Private String Tesspath = "C://program Files (x86)//TESSERACT-OCR"; 
  
 Private String Tesspath = new File ("Tesseract"). GetAbsolutePath (); Public String recognizetext (file imagefile,string imageformat) throws exception{file Tempimage = Imageiohelper.createim 
  Age (Imagefile,imageformat); 
  File outputfile = new file (Imagefile.getparentfile (), "output"); 
  StringBuffer strb = new StringBuffer (); 
  List cmd = new ArrayList (); 
  if (Os.iswindowsxp ()) {Cmd.add (tesspath+ "//tesseract"); 
  }else if (Os.islinux ()) {Cmd.add ("tesseract"); 
  }else{Cmd.add (tesspath+ "//tesseract"); } cmd.add (""); 
  Cmd.add (Outputfile.getname ()); 
  Cmd.add (lang_option); 
  Cmd.add ("Chi_sim"); 
   
  Cmd.add ("Eng"); 
  Processbuilder PB = new Processbuilder (); 
   
  Pb.directory (Imagefile.getparentfile ()); 
  Cmd.set (1, Tempimage.getname ()); 
  Pb.command (CMD); 
   
  Pb.redirecterrorstream (TRUE); 
  Process process = Pb.start (); 
   
  Tesseract.exe 1.jpg 1-l chi_sim int w = process.waitfor (); 
   
  Delete temporary working document Tempimage.delete (); if (w==0) {BufferedReader in = new BufferedReader (New InputStreamReader (FileInputStream 
    
   () + ". txt"), "UTF-8"); 
   String str; 
   while ((str = in.readline ())!=null) {strb.append (str). append (EOL); 
  } in.close (); 
   }else{String msg; Switch (w) {Case 1:msg = "Errors accessing files. 
     There May is spaces in your image ' s filename. ' 
    Break 
     Case 29:msg = "Cannot recongnize the image or its selected region."; 
    Break Case 31:msg = "Unsupported image f"Ormat. "; 
    Break 
   Default:msg = "Errors occurred." 
   } tempimage.delete (); 
  throw new RuntimeException (msg); 
  New File (Outputfile.getabsolutepath () + ". txt"). Delete (); 
 return strb.tostring ();
 } 
}

Test class TESTOCR:

Import Java.io.File;
Import java.io.IOException;

Import Com.hhp.util.OCR;

public class Ocrtest {public

 static void Main (string[] args) {
  String path = ' c://temp//ocrcode//4.png ';  
  System.out.println ("ORC Test Begin ...");
  try {  
   String valcode = new OCR (). Recognizetext (New File (path), "PNG");  
   System.out.println (Valcode);  
  } catch (IOException e) {  
   e.printstacktrace ();  
  } catch (Exception e) { 
   e.printstacktrace (); 
  }  
  System.out.println ("ORC Test end ...");
 } 



After testing, TESSERACT-OCR 3.01 of the text recognition rate is very high for the site Common authentication code recognition rate is very high.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.