tesseract ocr java_java通过开源框架tesseract-ocr引擎实现图文识别

米丰
2023-12-01

package major;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import javax.swing.Box;

import javax.swing.JButton;

import javax.swing.JFormattedTextField;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

import tools.update;

public class Test {

private static boolean t=true;

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

//OCR ocr=new OCR();

// try {

//String maybe2 = new OCR().recognizeText(new File("E:\\temp\\222.png"), "png");

//update.updateFromWeb(maybe2,"E:\\temp\\222.txt",true);

//System.out.println(maybe2);

//System.out.println("**********");

MyString str=new MyString();

System.out.println(str.getString(maybe2));

//} catch (Exception e) {

TODO Auto-generated catch block

//e.printStackTrace();

//}

//SoundServer s=new SoundServer();

//s.playSound("E:\\111\\HOOK1.wav");

Box form = Box.createVerticalBox();

JTextField field = new JTextField(30);

form.add(new JLabel("图片路径:"));

final JFormattedTextField webUrl =

new JFormattedTextField();

webUrl.setValue("e:/temp/");

form.add(webUrl);

JTextField field1 = new JTextField(30);

form.add(new JLabel("保存路径:"));

final JFormattedTextField saveUrl =

new JFormattedTextField();

saveUrl.setValue("e:/temp/");

form.add(saveUrl);

JButton button=new JButton("开始");

button.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

String fileUrls=(String)webUrl.getValue();

String maybe2;

try {

File file=new File(fileUrls);

File[] tempList = file.listFiles();

if(tempList==null){

JOptionPane.showMessageDialog( null , "该文件夹为空,","提示" , JOptionPane.INFORMATION_MESSAGE) ;

return;

}

for(int i=0;t&&i

String type=tempList[i].getName().split("\\.")[tempList[i].getName().split("\\.").length-1];

if(!type.equals("png")&&!type.equals("PNG")&&!type.equals("jpg")&&!type.equals("JPG")&&!type.equals("bmp")&&!type.equals("tiff")&&!type.equals("gif")

&&!type.equals("pcx")&&!type.equals("tga")&&!type.equals("fpx")&&!type.equals("svg")&&!type.equals("psd")&&!type.equals("cdr")

&&!type.equals("pcd")&&!type.equals("dxf")&&!type.equals("ufo")&&!type.equals("eps")&&!type.equals("ai")&&!type.equals("raw"))

{

continue;

}else{

String saveUrls=(String)saveUrl.getValue();

String name=tempList[i].getName().split("/")[tempList[i].getName().split("/").length-1].split("\\.")[0];

maybe2 = new OCR().recognizeText(tempList[i], type);

update.updateFromWeb(maybe2,saveUrls+"/"+name+".txt",true);

}

}

} catch (Exception e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

});

form.add(button);

// JButton button1=new JButton("停止");

//button1.addActionListener(new ActionListener() {

//@Override

//public void actionPerformed(ActionEvent e) {

TODO Auto-generated method stub

//d.setT(false);

//d=new Demo();

//}

//});

// form.add(button1);

JFrame frame = new JFrame("User Information");

frame.getContentPane().add(form);

frame.pack();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

 类似资料: