WebBox Klassenbibliothek
ulrich
2017-12-27 5bfcdd214e1e6b3b7653009137d316dabadcda91
commit | author | age
ca449e 1 /*
U 2     Dateiverwaltung - File management in your browser
3     Copyright (C) 2017 Ulrich Hilger, http://uhilger.de
4
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU Affero General Public License as
7     published by the Free Software Foundation, either version 3 of the
8     License, or (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU Affero General Public License for more details.
14
15     You should have received a copy of the GNU Affero General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package de.uhilger.wbx;
20
21 import java.awt.Graphics2D;
22 import java.awt.Image;
23 import java.awt.RenderingHints;
24 import java.awt.image.BufferedImage;
25 import java.awt.image.ConvolveOp;
26 import java.awt.image.Kernel;
27 import java.io.File;
28 import java.io.FileNotFoundException;
29 import java.io.IOException;
61b659 30 import java.io.OutputStream;
ca449e 31 import java.net.FileNameMap;
U 32 import java.net.URLConnection;
33 import javax.imageio.ImageIO;
34
35 /**
36  * Methoden zur Verkleinerung von Bildern
37  */
38 public class Bild {
39   
40   public static final int WINZIG = 0;
41   public static final int KLEIN = 1;
91d228 42   public static final int SEMI = 2;
U 43   public static final int MITTEL = 3;
44   public static final int GROSS = 4;
ca449e 45   
91d228 46   public static final String[] GRNAME = {"-w", "-k", "-s", "-m", "-g"};
U 47   public static final int[] GR = {120, 240, 500, 700, 1200};
ca449e 48   
61b659 49   public void writeImageStream(Image image, int gr, String mimeType, OutputStream out) throws InterruptedException, IOException {
U 50     ImageIO.write(getReducedImage(image, gr, mimeType), imgType(mimeType), out);
51   }
52   
ca449e 53   /**
U 54    * 
55    * @param image
56    * @param gr
57    * @param mimeType
58    * @param vName Name der verkleinerten Datei
59    * @throws InterruptedException
60    * @throws IOException 
61    */
61b659 62   public void writeImageFile(Image image, int gr, String mimeType, String vName) throws InterruptedException, IOException {
U 63     ImageIO.write(getReducedImage(image, gr, mimeType), imgType(mimeType), new File(vName));
64   }
65   
66   private BufferedImage getReducedImage(Image image, int gr, String mimeType) throws InterruptedException, IOException {
67     BufferedImage img;
ca449e 68     int q = 90;
U 69     float sh = 0.f;
70     if(mimeType.contains("jpeg")) {
71       img = getReducedImage(image, gr, gr, q, sh, false);
72     } else {
73       img = getReducedImage(image, gr, gr, q, sh, true);
74     }
61b659 75     return img;
ca449e 76   }
U 77
78   /**
79    * Eine in Groesse und Qualitaet verringerte Bilddatei erzeugen
80    * @param image  Ablageort und Name der Bilddatei
81    * @param width  neue Breite
82    * @param height  neue hoehe
83    * @param quality neue Qualitaet (0 - 100)
84    * @param factor Faktor fuer Schaerfe / Unschaerfe (z.B. -0.15f fuer leichte Unschaerfe, 0.05f fuer leichtes Nachschaerfen)
85    * @param withTransparency ob Transparenz benoetigt wird
86    * @return neues, in Groesse und Qualitaet entsprechend verringertes Bild
87    * @throws java.lang.InterruptedException
88    * @throws java.io.FileNotFoundException
89    */
90   public BufferedImage getReducedImage(Image image, int width, int height, int quality, float factor, boolean withTransparency) 
91   throws InterruptedException, FileNotFoundException, IOException {
92
93     int imageWidth = image.getWidth(null);
94         int imageHeight = image.getHeight(null);
95
96       int thumbWidth = width;
97         int thumbHeight = height;
98     if(imageWidth < width) {
99       thumbWidth = imageWidth;    
100     }
101     if(imageHeight < height) {
102       thumbHeight = imageHeight;
103     }
104         double thumbRatio = (double)thumbWidth / (double)thumbHeight;
105         double imageRatio = (double)imageWidth / (double)imageHeight;
106         if (thumbRatio < imageRatio) {
107             thumbHeight = (int)(thumbWidth / imageRatio);
108         }
109         else {
110             thumbWidth = (int)(thumbHeight * imageRatio);
111         }
112
113         // draw original image to thumbnail image object and
114         // scale it to the new size on-the-fly
115         //BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
116     BufferedImage thumbImage;
117     if(withTransparency) {
118         thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_ARGB);
119     } else {
120       thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
121     }
122         Graphics2D graphics2D = thumbImage.createGraphics();
123         graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
124         graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
125
126         // 30.7.2007: sharpening hinzugefuegt (Anfang)
127         //float factor = -0.15f; // minus = sharpen, plus = soften
128         //float[] sharpenArray = {0, -1, 0, -1, 5, -1, 0, -1, 0};
129     
130     /*
131       30.6.2013: sharpening als Weichmacher nur, wenn Bild < 400
132     */
133     /*if(thumbWidth < 400 || thumbHeight < 400) {
134       factor = 0.1f;
135     }*/
136     
137         if(factor != 0.f) {
138             float[] array = {0, factor, 0, factor, 1-(factor*4), factor, 0, factor, 0};
139             Kernel kernel = new Kernel(3, 3, array);
140             ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
141             thumbImage = cOp.filter(thumbImage, null);
142         }
143         // 30.7.2007: sharpening hinzugefuegt (Ende)
144     
145     return thumbImage;
146   }
147   
148   public String imgType(String mimeType) {
149     String imgType;
150     if(mimeType.contains("jpg")) {
151       imgType = "jpg";
152     } else if(mimeType.contains("jpeg")) {
153       imgType = "jpg";
154     } else if(mimeType.contains("png")) {
155       imgType = "png";
156     } else if(mimeType.contains("gif")) {
157       imgType = "gif";
158     } else {
159       imgType = "jpg";
160     }
161     return imgType;
162   }
163   
164   /**
165    * 
166    * @param v Bild.WINZIG .. Bild.GROSS
167    * @return L&auml;nge der l&auml;ngsten Kante in Bildpunkten
168    */
169   public int getVariantenGroesse(int v) {
170     return GR[v];
171   }
172   
173   public String getVariantenName(int v) {
174     return GRNAME[v];
175   }
176   
177   public String getMimeType(File file) {
178     String absName = file.getAbsolutePath();
179     FileNameMap fileNameMap = URLConnection.getFileNameMap();
180     return fileNameMap.getContentTypeFor("file://" + absName);    
181   }
182 }