-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathTesseractExample.java
64 lines (47 loc) · 2.13 KB
/
TesseractExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.recognition;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.text.DecimalFormat;
import net.sourceforge.tess4j.*;
import org.bytedeco.javacpp.opencv_core.Mat;
import org.bytedeco.javacpp.opencv_core.Rect;
import static cv.skindetect.CvConvertHelper.matToImg;
/**
* Created by DW_xiongyu on 2016/11/21.
*/
public class TesseractExample {
public static String getRectWord(BufferedImage img, Rect rect) throws TesseractException {
ITesseract instance = new Tesseract(); // JNA Interface Mapping
String fontPath = "E:/char_recongition/Tesseract-OCR/tessdata";
instance.setDatapath(fontPath);
instance.setLanguage("chi_sim");
long start = System.currentTimeMillis();
Rectangle rectangle = new Rectangle(rect.x(), rect.y(), rect.width(), rect.height());
String result = instance.doOCR(img, rectangle);
long end = System.currentTimeMillis();
DecimalFormat form = new DecimalFormat("0.00");
//float xRatio = (float)rect.width() / img.getWidth();
//float yRatio = (float)rect.height() / img.getHeight();
//String strRatio = " xRatio " + form.format(xRatio) + " yRatio :" + form.format(yRatio);
System.out.println(" result: " + result+" time consume :"+(float)(end-start)/1000 + "S");
return result;
}
public static void main(String[] args) {
String path = "F:/img_test/online_sample_img/10.jpg";
File imageFile = new File(path);
ITesseract instance = new Tesseract(); // JNA Interface Mapping
try {
String fontPath = "E:/char_recongition/Tesseract-OCR/tessdata";
instance.setDatapath(fontPath);
instance.setLanguage("chi_sim");
long start = System.currentTimeMillis();
String result = instance.doOCR(imageFile);
long end = System.currentTimeMillis();
System.out.println("Tesseract time consume :"+(float)(end-start)/1000 + "S");
System.out.println(result);
} catch (TesseractException e) {
System.err.println(e.getMessage());
}
}
}