API to read text from Image file using OCR

2020-02-20 06:47发布

I am looking out for an example code or API name from OCR (Optical character recognition) in Java using which I can extract all text present from an image file. Without comparing it with any image which I am doing using below code.

public class OCRTest {

    static String STR = "";

    public static void main(String[] args) {
        OCR l = new OCR(0.70f);
        l.loadFontsDirectory(OCRTest.class, new File("fonts"));
        l.loadFont(OCRTest.class, new File("fonts", "font_1"));
        ImageBinaryGrey i = new ImageBinaryGrey(Capture.load(OCRTest.class, "full.png"));
        STR = l.recognize(i, 1285, 654, 1343, 677, "font_1");
        System.out.println(STR);
    }
}

标签: java ocr
3条回答
We Are One
2楼-- · 2020-02-20 07:26

You can try javaocr on sourceforge: http://javaocr.sourceforge.net/

There is also a great example with an applet which uses Encog: http://www.heatonresearch.com/articles/42/page1.html

That said, OCR requires a lot of power, so it means that if you are looking for a heavy use, you should look after OCR libraries written in C and integrate that with Java.

OCR is hard. So be sure to qualify your needs before adventuring yourself in it.

Tesseract and opencv (with javacv for integration for instance) are common choices. There are also commercial solutions such as ABBYY FineReader Engine and ABBYY Cloud OCR SDK.

查看更多
干净又极端
3楼-- · 2020-02-20 07:28

You can try Tess4j or JavaCPP Presets for Tesseract. I perfer later as its easier than the former. Add the dependency to your pom `

        <dependency>
            <groupId>org.bytedeco.javacpp-presets</groupId>
            <artifactId>tesseract-platform</artifactId>
            <version>3.04.01-1.3</version>
        </dependency>

` And its simple to code

import org.bytedeco.javacpp.*;
import static org.bytedeco.javacpp.lept.*;
import static org.bytedeco.javacpp.tesseract.*;

public class BasicExample {
    public static void main(String[] args) {
        BytePointer outText;

        TessBaseAPI api = new TessBaseAPI();
        // Initialize tesseract-ocr with English, without specifying tessdata path
        if (api.Init(null, "eng") != 0) {
            System.err.println("Could not initialize tesseract.");
            System.exit(1);
        }

        // Open input image with leptonica library
        PIX image = pixRead(args.length > 0 ? args[0] : "/usr/src/tesseract/testing/phototest.tif");
        api.SetImage(image);
        // Get OCR result
        outText = api.GetUTF8Text();
        System.out.println("OCR output:\n" + outText.getString());

        // Destroy used object and release memory
        api.End();
        outText.deallocate();
        pixDestroy(image);
    }
}

Tess4j is little complex as its requires specific VC++ redistributable package to be installed.

查看更多
欢心
4楼-- · 2020-02-20 07:39

Open Source OCR engine is available from Google for OCR. It can be processed using CMD. You can process the CMD using java for web applications easily.
Please visit https://www.youtube.com/watch?v=Mjg4yyuqr5E . You will get the step by step details to process OCR using CMD.

查看更多
登录 后发表回答