1 package org.imageconverter.domain.conversion;
2
3 import static org.apache.commons.lang3.exception.ExceptionUtils.getRootCauseMessage;
4
5 import java.awt.Rectangle;
6 import java.io.ByteArrayInputStream;
7 import java.io.IOException;
8 import java.util.Objects;
9
10 import javax.imageio.ImageIO;
11
12 import org.imageconverter.infra.exception.ConversionException;
13 import org.imageconverter.infra.exception.TesseractConversionException;
14 import org.imageconverter.infra.exception.TesseractNotSetException;
15 import org.imageconverter.util.BeanUtil;
16 import org.imageconverter.util.logging.Loggable;
17
18 import org.springframework.stereotype.Service;
19
20 import net.sourceforge.tess4j.ITesseract;
21 import net.sourceforge.tess4j.TesseractException;
22
23
24
25
26
27
28 @Service
29 @Loggable
30 public class TesseractService {
31
32
33
34
35
36
37
38
39
40 public String convert(final String fileName, final byte[] fileBytes) {
41
42 final var tesseractBeanProvider = BeanUtil.getBeanProviderFrom(ITesseract.class);
43 final var tesseract = tesseractBeanProvider.getObject();
44
45 if (Objects.isNull(tesseract)) {
46 throw new TesseractNotSetException();
47 }
48
49 try {
50
51 final var bufferedImage = ImageIO.read(new ByteArrayInputStream(fileBytes));
52
53 return tesseract.doOCR(bufferedImage);
54
55 } catch (final IllegalArgumentException ex) {
56
57 throw new TesseractNotSetException(ex);
58
59 } catch (final IOException ex) {
60
61 final Object[] params = { fileName, getRootCauseMessage(ex) };
62
63 throw new ConversionException("{exception.conversionExceptionIO}", ex, params);
64
65 } catch (final TesseractException | Error ex) {
66
67 final Object[] params = { fileName, getRootCauseMessage(ex) };
68
69 throw new TesseractConversionException("{exception.conversionExceptionIO}", ex, params);
70
71
72
73
74
75 }
76 }
77
78
79
80
81
82
83
84
85
86
87
88
89
90 public String convert(final String fileName, final byte[] fileBytes, final int xAxis, final int yAxis, final int width, final int height) {
91
92 final var tesseractTess4j = BeanUtil.getBeanProviderFrom(ITesseract.class).getIfAvailable();
93
94 if (Objects.isNull(tesseractTess4j)) {
95 throw new TesseractNotSetException();
96 }
97
98 try {
99
100 final var bufferedImage = ImageIO.read(new ByteArrayInputStream(fileBytes));
101
102 return tesseractTess4j.doOCR(bufferedImage, new Rectangle(xAxis, yAxis, width, height));
103
104 } catch (final IllegalArgumentException ex) {
105
106 throw new TesseractNotSetException(ex);
107
108 } catch (final IOException ex) {
109
110 final Object[] params = { fileName, getRootCauseMessage(ex), xAxis, yAxis, width, height };
111
112 throw new ConversionException("{exception.conversionAreaExceptionIO}", ex, params);
113
114 } catch (final TesseractException | Error ex) {
115
116 final Object[] params = { fileName, getRootCauseMessage(ex), xAxis, yAxis, width, height };
117
118 throw new TesseractConversionException("{exception.conversionAreaTesseractError}", ex, params);
119
120
121
122
123
124 }
125 }
126 }