Today, I think of a previously done Android text detection and recognition application good review, because the previous writing Java program, the purpose is to be able to use the line, will not carefully look at each part of the code, will not remember their usage, do not go back to check the API, learn from other people's routines, used to forget, now think about changing , so I reviewed it.
The previous detection used TESSERACT_OCR, the reason that can be run on Android, because the Dark lord of the Great God has put Tess-two (for Android Tesseract-tools) compiled, and then I directly use. I am still small white, completely do not understand compiles those, if let me engage in .... Anyway finally compiled into so file (this is the Linux platform dynamic Link library, you can analogy DLL), and then I use the compiled so file, and the jar package into the project, divert the text recognition part and I read the paper written by the text detection part together, and then Baidu how to call the camera, Then write a camera to take pictures, then detect the text in the area, process, and then identify the application. Later there will be time to re-comb the application in detail, and then record, of course, this is not the focus of today. If you are interested in how to do OCR on Android, you can refer to this.
Back to the point, a few days ago the classmate asked me to do before the word recognition is how to make, he wanted to use, I would like to move the program back to Java, the results of my Baidu found there is another way of thinking, that is the implementation of EXE process. Install the TESSERACT_ORC on the PC first, then the JVM runs the command line to run the recognition program, completes the recognition result writes the TXT, finally reads the TXT to return the content to the Java program. It's OK to think so, so I tried it. (PS: I was later found also have Java API tess4j, refer to here)
Then it's about how you do it:
Engineering is also two parts, part of the GUI, anyway, I now have a weak basic knowledge of Java, and then often use the class is not much, so take this opportunity just familiar.
GUI mainly uses the Javax.swing package and the java.awt package, swing main write component, AWT is the event (in fact, AWT can also write components), but said swing is the optimization of the formation of AWT, so now commonly used swing. The most important concept inside the GUI is the container, which must be placed inside the container to display, commonly used frame and dialog. This time I write GUI to use Jframe,jbutton,filedialog,jlabel,imageicon these classes
The interface is as follows
- One of the buttons with the JButton: "Push" button implementation, you can set the listener.
- Button set the Listener, "open picture" pop-up Filedoalog, select the picture file, "identify" the new text recognition class, to identify the return result:
FileDialog class displays a dialog window where the user can select a file.
- JLabel for picture display and text display:
JLabel objects can display text, images, or both. You can specify where the label content is aligned in the label display area by setting vertical and horizontal alignment. By default, labels are centered vertically within their display area. By default, labels that display only text are the start edge alignment, while labels that display only the image are centered horizontally.
- JFrame Building the entire container
- ImageIcon is used to load images. ImageIcon: An implementation of an icon interface that draws an icon based on an Image. You can use Mediatracker to preload an image created from a URL, file name, or byte array to monitor the load status of the image.
The specific code of the interface I will not stick up, it is worth noting that the picture compression shows a small trick.
ImagePath =NewString (dirpath+fileName); Imageico=NewImageIcon (imagePath);intW =imageico.geticonwidth ();inth =imageico.geticonheight ();DoubleRatio = (Double) w/(Double) H;if(RATIO>4/3) {h= (int) (640*h/W); W= 640; }Else{W= (int) (480*w/h); H= 480;} Imageico.setimage (Imageico.getimage (). getScaledInstance (W,h,imageico.getimage (). Scale_default));
In addition, is the text recognition of the part, is mainly a process of execution, then the first download installation TESSERACT_OCR:
Install the download work reference here
So the main challenge here is to use Java to execute the process and do the file Io, the main points are:
1.java operation command line is mainly used in Processbuilder & Process class, from Java.lang
Typically, the Processbuilder.start () and Runtime.exec (arraylist<string>) methods create a native process and return an instance of the process subclass that can be used to control the process and obtain relevant information. The process class provides a way to perform input from the process, perform output to the process, wait for the process to complete, check the exit status of the process, and destroy (kill) the process.
Runtime.getRuntime.exec (arraylist<string> cmd)
Processbuilder PB; Pb.command (arraylist<string> cmd);
Here is the use of Processbuilder PB, create a new instance, and save the running parameters to the string form cmd, and then Pb.command (arraylist<string> cmd) execution, the results are saved to the specified txt;
2. The class file class that represents the document is derived from java.io
It is an abstract representation of the path names of files and directories.
Main methods GetName (), GetPath (), Getparentpath ();
When executing a command line, represents the input picture, which indicates that the output txt can be used in the file class.
3. Read the byte stream from java.io
FileInputStream gets the input bytes from a file in the file system. Which files are available depends on the host environment, where you can read the pictures.
New FileInputStream (Outputfile.getabsolutepath ()) Create a file input byte stream
New InputStreamReader (New FileInputStream (Outputfile.getabsolutepath ()), "UTF-8"). File input byte stream becomes file input character stream
BufferedReader reads text from the character input stream, buffering individual characters, enabling efficient reading of characters, arrays, and rows.
There are two main usages that can be used in this case.
BufferedReader in = new BufferedReader (New FileReader ("foo.in"));
Or
BufferedReader in = new BufferedReader (new InputStreamReader (New FileInputStream ("foo.in"), "UTF-8"));
The former cannot specify the encoding, while the latter can.
At the end of the TXT text read to the Java program, and then display in the GUI to use
The code is identified as follows
ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileReader;ImportJava.io.InputStreamReader;Importjava.util.ArrayList;Importjava.util.List; Public classtextrecognizer{PrivateString Textresult; /*** Results of the output*/ Private FinalString EOL = System.getproperty ("Line.separator"); //Enter PrivateString Tesspath = "D:\\TESSERACT-OCR"; //directory where the TESSOCR program is located PublicTextrecognizer (String path) {Try{File ImageFile=NewFile (path); Textresult= This. Recognizetext (ImageFile); } Catch(Exception e) {e.printstacktrace (); } } PublicString GetResult () {returnTextresult; } PrivateString Recognizetext (File imagefile)throwsException {/*** Set the saved file directory of the output file*/File OutputFile=NewFile (Imagefile.getparentfile (), "Output"); StringBuffer StrB=NewStringBuffer (); //set cmd command line string formlist<string> cmd =NewArraylist<string>(); Cmd.add (Tesspath+ "\\tesseract"); Cmd.add (Imagefile.getname ()); Cmd.add (Outputfile.getname ()); Cmd.add ("-L"); Cmd.add ("Eng"); //start EXE processProcessbuilder PB =NewProcessbuilder (); Pb.directory (Imagefile.getparentfile ()); Pb.command (CMD); Pb.redirecterrorstream (true); Process Process=Pb.start (); //wait for this process to complete intW =process.waitfor (); if(w = = 0) {//0 means normal exit .BufferedReader in =NewBufferedReader (NewInputStreamReader (NewFileInputStream (Outputfile.getabsolutepath () + ". txt"), "UTF-8")); String str; while(str = in.readline ())! =NULL) {strb.append (str). append (EOL); } in.close (); } Else{String msg; Switch(w) { Case1: Msg= "Errors accessing files. There may is spaces in your image ' s filename. "; Break; Case29: Msg= "Cannot recognize the image or its selected region."; Break; Case31: Msg= "Unsupported image format."; Break; default: Msg= "Errors occurred."; } Throw Newruntimeexception (msg); } NewFile (Outputfile.getabsolutepath () + ". txt"). Delete (); /*** If you do verification code * return Strb.tostring (). ReplaceAll ("\\s*", "" "); */ returnstrb.tostring (); }}
Unfortunately, the results of the final test are generally.
There are other interesting places that I have recorded during the writing process today.
- Java foreach traversal, for (File file:testDataDir.listFiles ()), supported after jdk1.6.
- Private final String EOL = System.getproperty ("Line.separator"); String representation of carriage return line wrap
"Java Learning note" An implementation method on 2015.1.6 Tesseract_orc Java