D
D
Dmitry Belousov2017-02-07 20:12:34
Java
Dmitry Belousov, 2017-02-07 20:12:34

How to detect text from image using javacpp tesseract?

BytePointer outText;

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

        File imgPath = new File("test.png");
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        BufferedImage img;

        try {
            img = ImageIO.read(imgPath);
            ImageIO.write(img, "png", baos );
        } catch (IOException e) {
            System.err.println("Reading file or writing byte[] failed.");
            e.printStackTrace();
        }

        byte[] imageInByte=baos.toByteArray();

        PIX image = pixReadMemPng(imageInByte, imageInByte.length);

        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);

here is my code, when i run it then image = null? why is it so?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question