Java OCR tesseract 图片识别技术(二)

来源:互联网 发布:java zookeeper 使用 编辑:程序博客网 时间:2024/06/05 05:01

一、前面已经测试过了tesseract的dos方式调用,接下来使用java代码方式调用tesseract工具识别验证码。

package com.cyn.utils;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;public class OCRUtil {    //OCR的都是命令方式调用    public static String getImgText(String imgPath) {          String result = "";          BufferedReader br = null;        //中文识别   -l chi_sim -psm 7 nobatch        String ocrLangData="outputbase nobatch digits";//识别语言        String outPath = imgPath.substring(0, imgPath.lastIndexOf("."));        File file = new File(outPath + ".txt");        try {            //dos执行            Runtime runtime = Runtime.getRuntime();              String command = "tesseract " + imgPath + " " + outPath +" "+ ocrLangData;              Process ps = runtime.exec(command);              ps.waitFor();              // 读取文件              br = new BufferedReader(new FileReader(file));              String temp = "";              StringBuffer sb = new StringBuffer();              while ((temp = br.readLine()) != null) {                  sb.append(temp);              }            // 文字识别结果              result = sb.toString();        } catch (Exception e) {              System.out.println("识别图片异常!");            e.printStackTrace();          }finally{            try {                br.close();                //读取完后删除文件                file.delete();            } catch (IOException e) {                e.printStackTrace();            }        }        return result;      }     //测试    public static void main(String[] args) {        getImgTxtList("E:\\TestCode");    }    //处理一个文件夹中的所有验证码    public static void getImgTxtList(String filepath){        File file = new File(filepath);        File [] fileList = file.listFiles();        for(File f:fileList){            String imgpath=f.getAbsolutePath();            if(imgpath.endsWith("jpg")||imgpath.endsWith("png")||imgpath.endsWith("bmp")){                String resultTxt = getImgText(imgpath);                System.out.println("result: "+resultTxt);            }        }    }

上面的代码作用是将E:\TestCode文件夹下的所有图片文件加以识别。在java代码中调用dos命令使用tesseract工具。

阅读全文
0 0