1 | package org.atea.nlptools.ocr.services;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 |
|
---|
5 | import org.atea.nlptools.ocr.Exceptions.LeptonicaException;
|
---|
6 | import org.atea.nlptools.ocr.Exceptions.TesseractException;
|
---|
7 | import org.atea.nlptools.ocr.abstractions.objects.IOcrOptions;
|
---|
8 | import org.atea.nlptools.ocr.abstractions.services.IOcrService;
|
---|
9 | import org.atea.nlptools.ocr.objects.TesseractOptions;
|
---|
10 |
|
---|
11 | import org.bytedeco.javacpp.BytePointer;
|
---|
12 | import org.bytedeco.leptonica.PIX;
|
---|
13 | import org.bytedeco.leptonica.global.lept;
|
---|
14 | import org.bytedeco.tesseract.TessBaseAPI;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Represents a service for performing OCR on files using the Tesseract engine.
|
---|
18 | */
|
---|
19 | public class TesseractOcrService implements IOcrService
|
---|
20 | {
|
---|
21 | private final String tessData;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Initializes a new instance of the {@link TesseractOcrService} class.
|
---|
25 | * @param tesseractDataPath Path to the tesseract data directory.
|
---|
26 | */
|
---|
27 | public TesseractOcrService(String tesseractDataPath)
|
---|
28 | {
|
---|
29 | this.tessData = tesseractDataPath;
|
---|
30 | }
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | public String run(File file, IOcrOptions options)
|
---|
34 | throws Exception, IllegalArgumentException
|
---|
35 | {
|
---|
36 | if (options.getClass() != TesseractOptions.class)
|
---|
37 | {
|
---|
38 | throw new IllegalArgumentException("Expected an options parameter of type " + TesseractOptions.class);
|
---|
39 | }
|
---|
40 |
|
---|
41 | TesseractOptions tOptions = (TesseractOptions)options;
|
---|
42 | PIX inputImage = null;
|
---|
43 | TessBaseAPI api = null;
|
---|
44 | BytePointer outputTextPtr = null;
|
---|
45 |
|
---|
46 | try
|
---|
47 | {
|
---|
48 | inputImage = lept.pixRead(file.getAbsolutePath());
|
---|
49 | if (inputImage == null) {
|
---|
50 | throw new LeptonicaException("Didn't recognise the image format. Check stdout for more info.");
|
---|
51 | }
|
---|
52 |
|
---|
53 | api = new TessBaseAPI();
|
---|
54 |
|
---|
55 | if (api.Init(this.tessData, tOptions.getLanguage()) != 0) {
|
---|
56 | throw new TesseractException("Could not initialize tesseract. Check stdout for more info");
|
---|
57 | }
|
---|
58 |
|
---|
59 | api.SetPageSegMode(tOptions.pageSegmentationMode);
|
---|
60 | api.SetImage(inputImage);
|
---|
61 | outputTextPtr = api.GetUTF8Text();
|
---|
62 |
|
---|
63 | return outputTextPtr.getString();
|
---|
64 | }
|
---|
65 | finally
|
---|
66 | {
|
---|
67 | if (inputImage != null)
|
---|
68 | {
|
---|
69 | lept.pixDestroy(inputImage);
|
---|
70 | }
|
---|
71 |
|
---|
72 | if (api != null)
|
---|
73 | {
|
---|
74 | api.End();
|
---|
75 | api.close();
|
---|
76 | }
|
---|
77 |
|
---|
78 | if (outputTextPtr != null)
|
---|
79 | {
|
---|
80 | outputTextPtr.deallocate();
|
---|
81 | outputTextPtr.close();
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|