Java 使用 Tess4J 实现图像识别

最近需要用Java做一个图像识别的东西,查了一些资料,在此写一个基于Tess4J的教程,方便其他人参考和使用。

其实做图像识别,也可以使用TESSERACT-OCR来实现,但是该方式需要下载软件,在电脑上安装环境,移植性不高,使用Tess4J只需要下载相关Jar包,导入项目,再把项目封装好就可以处处运行了。

首先,下载Tess4J的相关资源(一个压缩包),官网:http://tess4j.sourceforge.net/codesample.html

解压,其中的目录结构如下:


需要用到其中圈起来的三个文件夹中的东西。lib文件夹下放的是需要用到的Jar包,tessdata下放的是语言库,默认的有英语库,中文库需要另外下载,下载地址:https://github.com/tesseract-ocr/tessdata/blob/master/chi_sim.traineddata。

新建一个Java项目,将lib文件夹和tessdata文件夹复制到项目的根目录下,找到dist文件夹下的tess4j.jar(名字可能有版本号),将该文件也复制到项目根目录下的lib文件夹下。

项目的的目录如下:


lib中的文件如下(tess4J.jar也在该目录下):


再在eclipse中打开项目,在项目中导入lib文件夹中所有的jar包(Build path --> configure build path),导入后的结果如下:


这样前期准备工作就完成了,下面就剩下代码了。Tess4J的代码比较简洁,如下:

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测试类
 */
public class Tess4JTest {

    public static void main(String[] args){

    	String path = "D://Java//Tess4J";		//我的项目存放路径
    	
    	File file = new File(path + "//photo.jpg");
        ITesseract instance = new Tesseract();

        /**
         *  获取项目根路径,例如: D:\IDEAWorkSpace\tess4J
         */
        File directory = new File(path);
        String courseFile = null;
        try {
            courseFile = directory.getCanonicalPath();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //设置训练库的位置
        instance.setDatapath(courseFile + "//tessdata");

        instance.setLanguage("eng");//chi_sim :简体中文, eng	根据需求选择语言库
        String result = null;
        try {
            long startTime = System.currentTimeMillis();
             result =  instance.doOCR(file);
            long endTime = System.currentTimeMillis();
            System.out.println("Time is:" + (endTime - startTime) + " 毫秒");
        } catch (TesseractException e) {
            e.printStackTrace();
        }
        
        System.out.println("result: ");
        System.out.println(result);
    }

}

这样就搞定了。

效果如下:

原图:


读取结果:


从结果来看准确率还有待提高,l 和 1分不清,0 和 O 也没有分清,汉字的准确率还要低一些,大家可以自行训练字体库优化。

  • 15
    点赞
  • 131
    收藏
    觉得还不错? 一键收藏
  • 36
    评论
ImageComparerUI——基于Java语言实现的相似图像识别,基于直方图比较算法。 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.MediaTracker; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JPanel; public class ImageComparerUI extends JComponent implements ActionListener { /** * */ private static final long serialVersionUID = 1L; private JButton browseBtn; private JButton histogramBtn; private JButton compareBtn; private Dimension mySize; // image operator private MediaTracker tracker; private BufferedImage sourceImage; private BufferedImage candidateImage; private double simility; // command constants public final static String BROWSE_CMD = "Browse..."; public final static String HISTOGRAM_CMD = "Histogram Bins"; public final static String COMPARE_CMD = "Compare Result"; public ImageComparerUI() { JPanel btnPanel = new JPanel(); btnPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); browseBtn = new JButton("Browse..."); histogramBtn = new JButton("Histogram Bins"); compareBtn = new JButton("Compare Result"); // buttons btnPanel.add(browseBtn); btnPanel.add(histogramBtn); btnPanel.add(compareBtn); // setup listener... browseBtn.addActionListener(this); histogramBtn.addActionListener(this); compareBtn.addActionListener(this); mySize = new Dimension(620, 500); JFrame demoUI = new JFrame("Similiar Image Finder"); demoUI.getContentPane().setLayout(new BorderLayout()); demoUI.getContentPane().add(this, BorderLayout.CENTER); demoUI.getContentPane().add(btnPanel, BorderLayout.SOUTH); demoUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); demoUI.pack(); demoUI.setVisible(true); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; if(sourceImage != null) { Image scaledImage = sourceImage.getScaledInstance(300, 300, Image.SCALE_FAST); g2.drawImage(scaledImage, 0, 0, 300, 300, null); } if(candidateImage != null) { Image scaledImage = candidateImage.getScaledInstance(300, 330, Image.SCALE_FAST); g2.drawImage(scaledImage, 310, 0, 300, 300, null); } // display compare result info here Font myFont = new Font("Serif", Font.BOLD, 16); g2.setFont(myFont); g2.setPaint(Color.RED); g2.drawString("The degree of similarity : " + simility, 50, 350); } public void actionPerformed(ActionEvent e) { if(BROWSE_CMD.equals(e.getActionCommand())) { JFileChooser chooser = new JFileChooser(); chooser.showOpenDialog(null); File f = chooser.getSelectedFile(); BufferedImage bImage = null; if(f == null) return; try { bImage = ImageIO.read(f); } catch (IOException e1) { e1.printStackTrace(); } tracker = new MediaTracker(this); tracker.addImage(bImage, 1); // blocked 10 seconds to load the image data try { if (!tracker.waitForID(1, 10000)) { System.out.println("Load error."); System.exit(1); }// end if } catch (InterruptedException ine) { ine.printStackTrace(); System.exit(1); } // end catch if(sourceImage == null) { sourceImage = bImage; }else if(candidateImage == null) { candidateImage = bImage; } else { sourceImage = null; candidateImage = null; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 36
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值