Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Tess4J to realize Image recognition in Java

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "Java how to use Tess4J to achieve image recognition", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Java how to use Tess4J to achieve image recognition" this article.

Image recognition based on Java Tess4J

Recently, I need to use Java to do an image recognition thing, look up some materials, and write a Tess4J-based tutorial here to facilitate other people's reference and use.

In fact, to do image recognition, you can also use TESSERACT-OCR to achieve, but this way needs to download software, install the environment on the computer, portability is not high, use Tess4J only need to download the relevant Jar package, import the project, and then package the project can be run everywhere.

First, download the resources related to Tess4J (a compressed package)

Official website: http://tess4j.sourceforge.net/codesample.html

Decompress it. The directory structure is as follows:

You need to use the things in the three folders that are circled in it. Lib folder is the need to use the Jar package, tessdata is the language library, the default has an English library, the Chinese library needs to be downloaded separately, download address: https://github.com/tesseract-ocr/tessdata/blob/master/chi_sim.traineddata.

Create a new Java project, copy the lib folder and tessdata folder to the root of the project, find the tess4j.jar under the dist folder (the name may have a version number), and copy the file to the lib folder under the root of the project.

The directory of the project is as follows

The files in lib are as follows (tess4J.jar is also in this directory)

Then open the project in eclipse

Import all jar packages (Build path-- > configure build path) in the lib folder in the project, and the results are as follows:

In this way, the preparatory work is completed, and the code is left.

The code of Tess4J is relatively concise.

As follows:

Tess4JTest.java

Package ocr; import net.sourceforge.tess4j.ITesseract;import net.sourceforge.tess4j.Tesseract;import net.sourceforge.tess4j.TesseractException;import net.sourceforge.tess4j.util.LoadLibs; import java.io.File;import java.io.IOException; / * Tess4J test class * / public class Tess4JTest {public static void main (String [] args) {String path = "D://Java//Tess4J" / / my project storage path File file = new File (path + "/ / photo.jpg"); ITesseract instance = new Tesseract (); / * get the project root path, for example: d:\ IDEAWorkSpace\ tess4J * / File directory = new File (path); String courseFile = null; try {courseFile = directory.getCanonicalPath () } catch (IOException e) {e.printStackTrace ();} / / set the location of the training library instance.setDatapath (courseFile + "/ / tessdata"); instance.setLanguage ("eng"); / / chi_sim: simplified Chinese, eng selects language library String result = null; try {long startTime = System.currentTimeMillis () as needed Result = instance.doOCR (file); long endTime = System.currentTimeMillis (); System.out.println ("Time is:" + (endTime-startTime) + "millisecond");} catch (TesseractException e) {e.printStackTrace ();} System.out.println ("result:"); System.out.println (result);}}

That's it.

The effect is as follows

Original picture:

Read result:

From the results, the accuracy still needs to be improved, l and 1 can not be distinguished, 0 and O can not be distinguished, the accuracy of Chinese characters is even lower, we can train font library optimization.

The above is all the contents of the article "how Java uses Tess4J to achieve image recognition". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report