From 35827a56037a583eabea82708a801b7fc3f6d355 Mon Sep 17 00:00:00 2001
From: ulrich <not disclosed>
Date: Sun, 04 Mar 2018 13:25:08 +0000
Subject: [PATCH] Umgestellt auf Thumbnailator

---
 src/de/uhilger/wbx/Bild.java          |  186 ---------------------------------------------
 src/de/uhilger/wbx/web/TNServlet.java |   39 +++------
 2 files changed, 17 insertions(+), 208 deletions(-)

diff --git a/src/de/uhilger/wbx/Bild.java b/src/de/uhilger/wbx/Bild.java
index 0e7aef7..7e58871 100644
--- a/src/de/uhilger/wbx/Bild.java
+++ b/src/de/uhilger/wbx/Bild.java
@@ -17,25 +17,18 @@
  */
 package de.uhilger.wbx;
 
-import java.awt.Graphics2D;
-import java.awt.Image;
-import java.awt.RenderingHints;
-import java.awt.image.BufferedImage;
-import java.awt.image.ConvolveOp;
-import java.awt.image.Kernel;
 import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.OutputStream;
 import java.net.FileNameMap;
 import java.net.URLConnection;
-import javax.imageio.ImageIO;
+import java.util.logging.Logger;
 
 /**
  * Methoden zur Verkleinerung von Bildern
  */
 public class Bild {
 
+  private static final Logger logger = Logger.getLogger(Bild.class.getName());
+  
   public static final int WINZIG = 0;
   public static final int KLEIN = 1;
   public static final int SEMI = 2;
@@ -44,180 +37,7 @@
 
   public static final String[] GRNAME = {"-w", "-k", "-s", "-m", "-g"};
   public static final int[] GR = {120, 240, 500, 700, 1200};
-
-  public void writeImageStream(Image image, int gr, String mimeType, OutputStream out) throws InterruptedException, IOException {
-    ImageIO.write(getReducedImage(image, gr, mimeType), imgType(mimeType), out);
-  }
-
-  /**
-   *
-   * @param image
-   * @param gr
-   * @param mimeType
-   * @param vName Name der verkleinerten Datei
-   * @throws InterruptedException
-   * @throws IOException
-   */
-  public void writeImageFile(Image image, int gr, String mimeType, String vName) throws InterruptedException, IOException {
-    ImageIO.write(getReducedImage(image, gr, mimeType), imgType(mimeType), new File(vName));
-  }
-
-  private BufferedImage getReducedImage(Image image, int gr, String mimeType) throws InterruptedException, IOException {
-    BufferedImage img;
-    int q = 90;
-    float sh = 0.f;
-    if (mimeType.contains("jpeg")) {
-      img = getReducedImage2(image, gr, gr, q, sh, false);
-    } else {
-      img = getReducedImage2(image, gr, gr, q, sh, true);
-    }
-    return img;
-  }
-
-  /**
-   * Eine in Groesse und Qualitaet verringerte Bilddatei erzeugen
-   *
-   * @param image Ablageort und Name der Bilddatei
-   * @param width neue Breite
-   * @param height neue hoehe
-   * @param quality neue Qualitaet (0 - 100)
-   * @param factor Faktor fuer Schaerfe / Unschaerfe (z.B. -0.15f fuer leichte
-   * Unschaerfe, 0.05f fuer leichtes Nachschaerfen)
-   * @param withTransparency ob Transparenz benoetigt wird
-   * @return neues, in Groesse und Qualitaet entsprechend verringertes Bild
-   * @throws java.lang.InterruptedException
-   * @throws java.io.FileNotFoundException
-   */
-  public BufferedImage getReducedImage2(Image image, int width, int height, int quality, float factor, boolean withTransparency)
-          throws InterruptedException, FileNotFoundException, IOException {
-
-    int imageWidth = image.getWidth(null);
-    int imageHeight = image.getHeight(null);
-    
-    int thumbWidth = width;
-    int thumbHeight = height;
-    if (imageWidth < width) {
-      thumbWidth = imageWidth;
-    }
-    if (imageHeight < height) {
-      thumbHeight = imageHeight;
-    }
-    
-    double thumbRatio = (double) thumbWidth / (double) thumbHeight;
-    double imageRatio = (double) imageWidth / (double) imageHeight;
-    
-    if(imageWidth > imageHeight) {
-      // Querformat
-      thumbHeight = (int) (thumbWidth / imageRatio);
-    } else if(imageWidth == imageHeight) {
-      // 1:1 kann so bleiben
-    } else {
-      // Hochformat
-      thumbWidth = (int) (thumbHeight * imageRatio);
-    }
-
-    // draw original image to thumbnail image object and
-    // scale it to the new size on-the-fly
-    //BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
-    BufferedImage thumbImage;
-    if (withTransparency) {
-      thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_ARGB);
-    } else {
-      thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
-    }
-    Graphics2D graphics2D = thumbImage.createGraphics();
-    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
-    graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
-
-    // 30.7.2007: sharpening hinzugefuegt (Anfang)
-    //float factor = -0.15f; // minus = sharpen, plus = soften
-    //float[] sharpenArray = {0, -1, 0, -1, 5, -1, 0, -1, 0};
-    /*
-      30.6.2013: sharpening als Weichmacher nur, wenn Bild < 400
-     */
-    /*if(thumbWidth < 400 || thumbHeight < 400) {
-      factor = 0.1f;
-    }*/
-    if (factor != 0.f) {
-      float[] array = {0, factor, 0, factor, 1 - (factor * 4), factor, 0, factor, 0};
-      Kernel kernel = new Kernel(3, 3, array);
-      ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
-      thumbImage = cOp.filter(thumbImage, null);
-    }
-    // 30.7.2007: sharpening hinzugefuegt (Ende)
-
-    return thumbImage;
-  }
   
-  /**
-   * Eine in Groesse und Qualitaet verringerte Bilddatei erzeugen
-   *
-   * @param image Ablageort und Name der Bilddatei
-   * @param width neue Breite
-   * @param height neue hoehe
-   * @param quality neue Qualitaet (0 - 100)
-   * @param factor Faktor fuer Schaerfe / Unschaerfe (z.B. -0.15f fuer leichte
-   * Unschaerfe, 0.05f fuer leichtes Nachschaerfen)
-   * @param withTransparency ob Transparenz benoetigt wird
-   * @return neues, in Groesse und Qualitaet entsprechend verringertes Bild
-   * @throws java.lang.InterruptedException
-   * @throws java.io.FileNotFoundException
-   */
-  public BufferedImage getReducedImage(Image image, int width, int height, int quality, float factor, boolean withTransparency)
-          throws InterruptedException, FileNotFoundException, IOException {
-
-    int imageWidth = image.getWidth(null);
-    int imageHeight = image.getHeight(null);
-
-    int thumbWidth = width;
-    int thumbHeight = height;
-    if (imageWidth < width) {
-      thumbWidth = imageWidth;
-    }
-    if (imageHeight < height) {
-      thumbHeight = imageHeight;
-    }
-    double thumbRatio = (double) thumbWidth / (double) thumbHeight;
-    double imageRatio = (double) imageWidth / (double) imageHeight;
-    if (thumbRatio < imageRatio) {
-      thumbHeight = (int) (thumbWidth / imageRatio);
-    } else {
-      thumbWidth = (int) (thumbHeight * imageRatio);
-    }
-
-    // draw original image to thumbnail image object and
-    // scale it to the new size on-the-fly
-    //BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
-    BufferedImage thumbImage;
-    if (withTransparency) {
-      thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_ARGB);
-    } else {
-      thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
-    }
-    Graphics2D graphics2D = thumbImage.createGraphics();
-    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
-    graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
-
-    // 30.7.2007: sharpening hinzugefuegt (Anfang)
-    //float factor = -0.15f; // minus = sharpen, plus = soften
-    //float[] sharpenArray = {0, -1, 0, -1, 5, -1, 0, -1, 0};
-    /*
-      30.6.2013: sharpening als Weichmacher nur, wenn Bild < 400
-     */
- /*if(thumbWidth < 400 || thumbHeight < 400) {
-      factor = 0.1f;
-    }*/
-    if (factor != 0.f) {
-      float[] array = {0, factor, 0, factor, 1 - (factor * 4), factor, 0, factor, 0};
-      Kernel kernel = new Kernel(3, 3, array);
-      ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
-      thumbImage = cOp.filter(thumbImage, null);
-    }
-    // 30.7.2007: sharpening hinzugefuegt (Ende)
-
-    return thumbImage;
-  }
-
   public String imgType(String mimeType) {
     String imgType;
     if (mimeType.contains("jpg")) {
diff --git a/src/de/uhilger/wbx/web/TNServlet.java b/src/de/uhilger/wbx/web/TNServlet.java
index e86adb4..922fe89 100644
--- a/src/de/uhilger/wbx/web/TNServlet.java
+++ b/src/de/uhilger/wbx/web/TNServlet.java
@@ -17,20 +17,15 @@
  */
 package de.uhilger.wbx.web;
 
-import de.uhilger.wbx.Bild;
-import java.awt.Container;
-import java.awt.Image;
-import java.awt.MediaTracker;
-import java.awt.Toolkit;
 import java.io.File;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
-import java.util.logging.Level;
 import java.util.logging.Logger;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import net.coobird.thumbnailator.Thumbnails;
 import org.apache.catalina.servlets.DefaultServlet;
 
 /**
@@ -95,15 +90,15 @@
         
     if(uriStr.endsWith(JPG) || uriStr.endsWith(JPEG) || uriStr.endsWith(PNG)) {
       if(uriStr.contains(TN)) {
-        bildAusgeben(request, response, relname, TN, Bild.WINZIG);
+        bildAusgeben(request, response, relname, TN, 120);
       } else if(uriStr.contains(KL)) {
-        bildAusgeben(request, response, relname, KL, Bild.KLEIN);
+        bildAusgeben(request, response, relname, KL, 240);
       } else if(uriStr.contains(SM)) {
-        bildAusgeben(request, response, relname, SM, Bild.SEMI);
+        bildAusgeben(request, response, relname, SM, 500);
       } else if(uriStr.contains(MT)) {
-        bildAusgeben(request, response, relname, MT, Bild.MITTEL);
+        bildAusgeben(request, response, relname, MT, 700);
       } else if(uriStr.contains(GR)) {
-        bildAusgeben(request, response, relname, GR, Bild.GROSS);
+        bildAusgeben(request, response, relname, GR, 1200);
       } else {
         super.doGet(request, response);
       }      
@@ -112,22 +107,16 @@
     }
   }
   
-  private void bildAusgeben(HttpServletRequest request, HttpServletResponse response, String relname, String indicator, int bildTyp) throws UnsupportedEncodingException, IOException {
+  private void bildAusgeben(HttpServletRequest request, HttpServletResponse response, String relname, String indicator, int gr) throws UnsupportedEncodingException, IOException {
     File dir = new File(request.getServletContext().getRealPath("/"));
     relname = relname.replace(indicator, "");
     File imgfile = new File(dir, URLDecoder.decode(relname, "utf-8"));
-    Image image = Toolkit.getDefaultToolkit().getImage(imgfile.getAbsolutePath());
-    MediaTracker mediaTracker = new MediaTracker(new Container());
-    mediaTracker.addImage(image, 0);
-    try {
-      mediaTracker.waitForID(0);
+    
+    // 120, 240, 500, 700, 1200
 
-      if (!mediaTracker.isErrorAny()) {
-        Bild bild = new Bild();
-        bild.writeImageStream(image, bild.getVariantenGroesse(bildTyp), bild.getMimeType(imgfile), response.getOutputStream());
-      }
-    } catch (InterruptedException ex) {
-      logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
-    }
-  }
+    Thumbnails.of(imgfile)
+            .size(gr, gr)
+            .keepAspectRatio(true)
+            .toOutputStream(response.getOutputStream());
+  }  
 }

--
Gitblit v1.9.3