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