Java Orc Image Recognition

Source: Internet
Author: User

Java calls Tesseract to complete text recognition in Images

1.install tesseract-ocr-setup-3.00.exe (Chinese recognition is added only in Versions later than 3.0)

2. Download the Chinese package chi_sim.traineddata.gz of tesseract-ocr. decompress the package and copy it to the installation directory of tesseract-OCR. For example, D:/Tesseract-OCR/tessdata

3. Java needs to add 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. shareimagewriteparam;

Public class imageiohelper {
/**
* Converting image files to Tif format
* @ Param imagefile file path
* @ Param imageformat File Extension
* @ Return
*/
Public static file createimage (File imagefile, string imageformat ){
File tempfile = NULL;
Try {
Iterator <imagereader> 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
Required imagewriteparam parameters writeparam = new required imagewriteparam (locale. Chinese );
Required writeparam. setcompressionmode (imagewriteparam. mode_disabled );

// Get TIF writer and set output to file
Iterator <imagewriter> 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, writable writeparam );
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:

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"; // lowercase L, not digit 1
Private final string EOL = system. getproperty ("line. separator ");
Private string tesspath = "D: // Tesseract-OCR ";
// Private string tesspath = new file ("tesseract"). getabsolutepath ();

Public String recognizetext (File imagefile, string imageformat) throws exception {
File tempimage = imageiohelper. createimage (imagefile, imageformat );
File outputfile = new file (imagefile. getparentfile (), "output ");
Stringbuffer strb = new stringbuffer ();
List <string> cmd = new arraylist <string> ();
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 = Pb. Start ();
// Tesseract.exe 1.jpg 1-l chi_sim
Int W = process. waitfor ();

// Delete a temporary working file
Tempimage. Delete ();

If (W = 0 ){
Bufferedreader in = new bufferedreader (New inputstreamreader (New fileinputstream (outputfile. getabsolutepath () + ". 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 be spaces in your image's filename .";
Break;
Case 29:
MSG = "cannot recongnize the image or its selected region .";
Break;
Case 31:
MSG = "unsupported image format .";
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;

Public class testocr {

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
String Path = "D: // test // C360_2011-04-25 16-15-34.jpg ";
System. Out. println ("Orc test begin ......");
Try {
String valcode = new OCR (). recognizetext (new file (PATH), "jpg ");
System. Out. println (valcode );
} Catch (ioexception e ){
E. printstacktrace ();
} Catch (exception e ){
E. printstacktrace ();
}
System. Out. println ("Orc test end ......");
}

}

 

But to be honest, although Tesseract has just provided support for Chinese recognition in version 3.0, the recognition rate is really unsatisfactory. After my own tests, even if the scanning is not ideal, pictures taken using mobile phones. I use the HTC G7 camera (pixels) in good light and tested with a business card. It is not ideal. In poor light, it is basically garbled. Here I want to call the office Orc recognition system for testing.

 

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.