zjh
2024-01-09 de2ec37e66b2f3a56c2aa5134f47cd3b3c08371e
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
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
 
import java.io.File;
 
public class ImageTextExtractor {
 
    public static String extractTextFromImage(String imagePath) {
        File imageFile = new File(imagePath);
        Tesseract tesseract = new Tesseract();
        try {
            tesseract.setLanguage("eng");
            String result = tesseract.doOCR(imageFile);
            return result;
        } catch (TesseractException e) {
            e.printStackTrace();
            return null;
        }
    }
 
    public static void main(String[] args) {
        String imagePath = "C:\\Users\\w\\Pictures\\Saved Pictures\\中华人民共和国万岁.jpg";
        String extractedText = extractTextFromImage(imagePath);
        System.out.println(extractedText);
    }
}