WebBox Klassenbibliothek
ulrich
2017-03-11 61b6598d15a7400bed909a15a9af61a3925262a8
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;
42   public static final int MITTEL = 2;
43   public static final int GROSS = 3;
44   
45   public static final String[] GRNAME = {"-w", "-k", "-m", "-g"};
46   public static final int[] GR = {120, 240, 700, 1200};
47   
61b659 48   public void writeImageStream(Image image, int gr, String mimeType, OutputStream out) throws InterruptedException, IOException {
U 49     ImageIO.write(getReducedImage(image, gr, mimeType), imgType(mimeType), out);
50   }
51   
ca449e 52   /**
U 53    * 
54    * @param image
55    * @param gr
56    * @param mimeType
57    * @param vName Name der verkleinerten Datei
58    * @throws InterruptedException
59    * @throws IOException 
60    */
61b659 61   public void writeImageFile(Image image, int gr, String mimeType, String vName) throws InterruptedException, IOException {
U 62     ImageIO.write(getReducedImage(image, gr, mimeType), imgType(mimeType), new File(vName));
63   }
64   
65   private BufferedImage getReducedImage(Image image, int gr, String mimeType) throws InterruptedException, IOException {
66     BufferedImage img;
ca449e 67     int q = 90;
U 68     float sh = 0.f;
69     if(mimeType.contains("jpeg")) {
70       img = getReducedImage(image, gr, gr, q, sh, false);
71     } else {
72       img = getReducedImage(image, gr, gr, q, sh, true);
73     }
61b659 74     return img;
ca449e 75   }
U 76
77   /**
78    * Eine in Groesse und Qualitaet verringerte Bilddatei erzeugen
79    * @param image  Ablageort und Name der Bilddatei
80    * @param width  neue Breite
81    * @param height  neue hoehe
82    * @param quality neue Qualitaet (0 - 100)
83    * @param factor Faktor fuer Schaerfe / Unschaerfe (z.B. -0.15f fuer leichte Unschaerfe, 0.05f fuer leichtes Nachschaerfen)
84    * @param withTransparency ob Transparenz benoetigt wird
85    * @return neues, in Groesse und Qualitaet entsprechend verringertes Bild
86    * @throws java.lang.InterruptedException
87    * @throws java.io.FileNotFoundException
88    */
89   public BufferedImage getReducedImage(Image image, int width, int height, int quality, float factor, boolean withTransparency) 
90   throws InterruptedException, FileNotFoundException, IOException {
91
92     int imageWidth = image.getWidth(null);
93         int imageHeight = image.getHeight(null);
94
95       int thumbWidth = width;
96         int thumbHeight = height;
97     if(imageWidth < width) {
98       thumbWidth = imageWidth;    
99     }
100     if(imageHeight < height) {
101       thumbHeight = imageHeight;
102     }
103         double thumbRatio = (double)thumbWidth / (double)thumbHeight;
104         double imageRatio = (double)imageWidth / (double)imageHeight;
105         if (thumbRatio < imageRatio) {
106             thumbHeight = (int)(thumbWidth / imageRatio);
107         }
108         else {
109             thumbWidth = (int)(thumbHeight * imageRatio);
110         }
111
112         // draw original image to thumbnail image object and
113         // scale it to the new size on-the-fly
114         //BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
115     BufferedImage thumbImage;
116     if(withTransparency) {
117         thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_ARGB);
118     } else {
119       thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
120     }
121         Graphics2D graphics2D = thumbImage.createGraphics();
122         graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
123         graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
124
125         // 30.7.2007: sharpening hinzugefuegt (Anfang)
126         //float factor = -0.15f; // minus = sharpen, plus = soften
127         //float[] sharpenArray = {0, -1, 0, -1, 5, -1, 0, -1, 0};
128     
129     /*
130       30.6.2013: sharpening als Weichmacher nur, wenn Bild < 400
131     */
132     /*if(thumbWidth < 400 || thumbHeight < 400) {
133       factor = 0.1f;
134     }*/
135     
136         if(factor != 0.f) {
137             float[] array = {0, factor, 0, factor, 1-(factor*4), factor, 0, factor, 0};
138             Kernel kernel = new Kernel(3, 3, array);
139             ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
140             thumbImage = cOp.filter(thumbImage, null);
141         }
142         // 30.7.2007: sharpening hinzugefuegt (Ende)
143     
144     return thumbImage;
145   }
146   
147   public String imgType(String mimeType) {
148     String imgType;
149     if(mimeType.contains("jpg")) {
150       imgType = "jpg";
151     } else if(mimeType.contains("jpeg")) {
152       imgType = "jpg";
153     } else if(mimeType.contains("png")) {
154       imgType = "png";
155     } else if(mimeType.contains("gif")) {
156       imgType = "gif";
157     } else {
158       imgType = "jpg";
159     }
160     return imgType;
161   }
162   
163   /**
164    * 
165    * @param v Bild.WINZIG .. Bild.GROSS
166    * @return L&auml;nge der l&auml;ngsten Kante in Bildpunkten
167    */
168   public int getVariantenGroesse(int v) {
169     return GR[v];
170   }
171   
172   public String getVariantenName(int v) {
173     return GRNAME[v];
174   }
175   
176   public String getMimeType(File file) {
177     String absName = file.getAbsolutePath();
178     FileNameMap fileNameMap = URLConnection.getFileNameMap();
179     return fileNameMap.getContentTypeFor("file://" + absName);    
180   }
181 }