View Javadoc
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  //import org.springframework.security.access.prepost.PreAuthorize;
18  import org.springframework.stereotype.Service;
19  
20  import net.sourceforge.tess4j.ITesseract;
21  import net.sourceforge.tess4j.TesseractException;
22  
23  /**
24   * A Java class to access tesseract application
25   * 
26   * @author Fernando Romulo da Silva
27   */
28  @Service
29  @Loggable
30  public class TesseractService {
31  
32      // @PreAuthorize("hasAuthority('ADMIN') or @accessChecker.hasLocalAccess(authentication)")
33      /**
34       * Convert a file image to text.
35       * 
36       * @param fileName  The file's name
37       * @param fileBytes A file that will be convert
38       * @return A {@link String} object with the conversion
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  //	} catch (final Exception ex) {
72  //
73  //	    final var msg = format("Unexpected error: ''{0}''.", getRootCauseMessage(ex));
74  //	    throw new ImageConvertServiceException(msg, ex);
75  	}
76      }
77  
78      /**
79       * Convert a file image to text.
80       * 
81       * @param fileName  The file's name
82       * @param fileBytes A file that will be convert
83       * @param xAxis     The image's x coordinate
84       * @param yAxis     The image's y coordinate
85       * @param width     The image's width in pixels
86       * @param height    The image's height in pixels
87       * 
88       * @return A {@link String} object with the conversion
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 //	} catch (final Exception ex) {
121 //
122 //	    final var msg = format("Unexpected error: ''{0}'', X {1}, Y {2}, Width {3} and Heigh {4}.", getRootCauseMessage(ex), xAxis, yAxis, width, height);
123 //	    throw new ImageConvertServiceException(msg, ex);
124 	}
125     }
126 }