commit 4cedc63bde34e7d6e3c613cb6e60e683bc954669
Author: smoldas15 <jan.smolar@seznam.cz>
Date:   Wed Apr 29 17:26:58 2020 +0200

    Re #7912: Ořezávání obrázků
    
    - úprava stávajícího ořezávání obrázků
    - přidání rozhodování na základě počtu nalezených výrazů

diff --git a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/SolrService.java b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/SolrService.java
index 50b5ea9..009080f 100644
--- a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/SolrService.java
+++ b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/SolrService.java
@@ -55,6 +55,8 @@ public class SolrService {
     private static final int DEFAULT_LINE_FIELD_COUNT = 150;
     private static final int DEFAULT_WORD_FIELD_COUNT = 30;
 
+    private static final int DEFAULT_WORD_HEIGHT = 30;
+
     private HttpSolrClient solrClientDocuments;
     private HttpSolrClient solrClientFiles;
     private List<String> fieldNamesDocumentCore = new ArrayList<>();
@@ -300,6 +302,54 @@ public class SolrService {
             Set<String> linesNames = linesAndWordsNames.stream().filter(name -> name.startsWith(PREFIX_TEXT_LINE)).collect(Collectors.toSet());
             Set<String> wordNames = linesAndWordsNames.stream().filter(name -> name.startsWith(PREFIX_TEXT_WORD)).collect(Collectors.toSet());
 
+            //-----
+
+            String imgFilename = (String) solrDocument.getFieldValue(FIELD_IMG_FILENAME);
+            byte[] imageBytes = getImageFromFilesCore(imgFilename);
+
+            BufferedImage img = ImageIO.read(new ByteArrayInputStream(Base64.getDecoder().decode(imageBytes)));
+            Image cropImg = new Image(img);
+
+            int y1 = textRegion.getRegionCoords().getPoints()[0].getY();
+            int y2 = textRegion.getRegionCoords().getPoints()[2].getY();
+
+            if (wordNames.size() == 1 || (wordNames.size() == 0 && linesNames.size() == 1)) {
+                String coordsValue = "";
+
+                if (wordNames.size() == 1) {
+                    coordsValue = (String) solrDocument.getFieldValue(wordNames.stream().findFirst().get() + SUFFIX_COORDS);
+
+                } else{
+                    coordsValue = (String) solrDocument.getFieldValue(linesNames.stream().findFirst().get() + SUFFIX_COORDS);
+                }
+                Point[] arrPoints = Coords.parsePointString(coordsValue);
+                int wordHeight = DEFAULT_WORD_HEIGHT;
+
+                if (arrPoints.length == 4) {
+                    wordHeight = arrPoints[2].getY() - arrPoints[0].getY();
+                }
+
+                if (y1 < (arrPoints[0].getY() - (2 * wordHeight))) {
+                    y1 = arrPoints[0].getY() - (2 * wordHeight);
+                }
+
+                if (y2 > (arrPoints[0].getY() + (3 * wordHeight))) {
+                    y2 = arrPoints[0].getY() + (3 * wordHeight);
+                }
+            }
+
+            cropImg.crop(textRegion.getRegionCoords().getPoints()[0].getX(), y1,
+                    textRegion.getRegionCoords().getPoints()[1].getX() - textRegion.getRegionCoords().getPoints()[0].getX(),
+                    y2 - y1);
+
+            cropImg.setOutputQuality(IMG_OUTPUT_QUALITY);
+
+            imageBytes = cropImg.getByteArray();
+            imageBytes = Base64.getEncoder().encode(imageBytes);
+            textRegion.setImageCut(new String(imageBytes));
+
+            //-----
+
             Map<String, TextLine> textLines = new HashMap<>();
             for (String lineName: linesNames) {
                 TextLine textLine = new TextLine();
@@ -355,21 +405,6 @@ public class SolrService {
 
             textRegion.setTextLines(textLines.values().toArray(new TextLine[0]));
 
-            // cropping image
-            String imgFilename = (String) solrDocument.getFieldValue(FIELD_IMG_FILENAME);
-            byte[] imageBytes = getImageFromFilesCore(imgFilename);
-
-            BufferedImage img = ImageIO.read(new ByteArrayInputStream(Base64.getDecoder().decode(imageBytes)));
-            Image cropImg = new Image(img);
-            cropImg.crop(textRegion.getRegionCoords().getPoints()[0].getX(),textRegion.getRegionCoords().getPoints()[0].getY(),
-                    textRegion.getRegionCoords().getPoints()[1].getX()-textRegion.getRegionCoords().getPoints()[0].getX(),
-                    textRegion.getRegionCoords().getPoints()[2].getY()-textRegion.getRegionCoords().getPoints()[0].getY());
-
-            cropImg.setOutputQuality(IMG_OUTPUT_QUALITY);
-
-            imageBytes = cropImg.getByteArray();
-            imageBytes = Base64.getEncoder().encode(imageBytes);
-
             // recalculating Coords - during init for words and lines
             for (int k = 1; k<textRegion.getRegionCoords().getPoints().length; k++){
                 textRegion.getRegionCoords().getPoints()[k].setX(textRegion.getRegionCoords().getPoints()[k].getX() - textRegion.getRegionCoords().getPoints()[0].getX());
@@ -378,8 +413,6 @@ public class SolrService {
             textRegion.getRegionCoords().getPoints()[0].setX(0);
             textRegion.getRegionCoords().getPoints()[0].setY(0);
 
-            textRegion.setImageCut(new String(imageBytes));
-
             // image size
             BufferedImage image = ImageIO.read(new ByteArrayInputStream(Base64.getDecoder().decode(imageBytes)));
             DocumentSize documentSize = new DocumentSize(image.getHeight(), image.getWidth());
