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/>. |
671a35
|
17 |
*/ |
ca449e
|
18 |
package de.uhilger.wbx; |
U |
19 |
|
|
20 |
import java.awt.Graphics2D; |
|
21 |
import java.awt.Image; |
|
22 |
import java.awt.RenderingHints; |
|
23 |
import java.awt.image.BufferedImage; |
|
24 |
import java.awt.image.ConvolveOp; |
|
25 |
import java.awt.image.Kernel; |
|
26 |
import java.io.File; |
|
27 |
import java.io.FileNotFoundException; |
|
28 |
import java.io.IOException; |
61b659
|
29 |
import java.io.OutputStream; |
ca449e
|
30 |
import java.net.FileNameMap; |
U |
31 |
import java.net.URLConnection; |
|
32 |
import javax.imageio.ImageIO; |
|
33 |
|
|
34 |
/** |
|
35 |
* Methoden zur Verkleinerung von Bildern |
|
36 |
*/ |
|
37 |
public class Bild { |
671a35
|
38 |
|
ca449e
|
39 |
public static final int WINZIG = 0; |
U |
40 |
public static final int KLEIN = 1; |
91d228
|
41 |
public static final int SEMI = 2; |
U |
42 |
public static final int MITTEL = 3; |
|
43 |
public static final int GROSS = 4; |
671a35
|
44 |
|
91d228
|
45 |
public static final String[] GRNAME = {"-w", "-k", "-s", "-m", "-g"}; |
U |
46 |
public static final int[] GR = {120, 240, 500, 700, 1200}; |
671a35
|
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 |
} |
671a35
|
51 |
|
ca449e
|
52 |
/** |
671a35
|
53 |
* |
ca449e
|
54 |
* @param image |
U |
55 |
* @param gr |
|
56 |
* @param mimeType |
|
57 |
* @param vName Name der verkleinerten Datei |
|
58 |
* @throws InterruptedException |
671a35
|
59 |
* @throws IOException |
ca449e
|
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 |
} |
671a35
|
64 |
|
61b659
|
65 |
private BufferedImage getReducedImage(Image image, int gr, String mimeType) throws InterruptedException, IOException { |
U |
66 |
BufferedImage img; |
ca449e
|
67 |
int q = 90; |
U |
68 |
float sh = 0.f; |
671a35
|
69 |
if (mimeType.contains("jpeg")) { |
U |
70 |
img = getReducedImage2(image, gr, gr, q, sh, false); |
ca449e
|
71 |
} else { |
671a35
|
72 |
img = getReducedImage2(image, gr, gr, q, sh, true); |
ca449e
|
73 |
} |
61b659
|
74 |
return img; |
ca449e
|
75 |
} |
U |
76 |
|
|
77 |
/** |
|
78 |
* Eine in Groesse und Qualitaet verringerte Bilddatei erzeugen |
671a35
|
79 |
* |
U |
80 |
* @param image Ablageort und Name der Bilddatei |
|
81 |
* @param width neue Breite |
|
82 |
* @param height neue hoehe |
ca449e
|
83 |
* @param quality neue Qualitaet (0 - 100) |
671a35
|
84 |
* @param factor Faktor fuer Schaerfe / Unschaerfe (z.B. -0.15f fuer leichte |
U |
85 |
* Unschaerfe, 0.05f fuer leichtes Nachschaerfen) |
ca449e
|
86 |
* @param withTransparency ob Transparenz benoetigt wird |
U |
87 |
* @return neues, in Groesse und Qualitaet entsprechend verringertes Bild |
|
88 |
* @throws java.lang.InterruptedException |
|
89 |
* @throws java.io.FileNotFoundException |
|
90 |
*/ |
671a35
|
91 |
public BufferedImage getReducedImage2(Image image, int width, int height, int quality, float factor, boolean withTransparency) |
U |
92 |
throws InterruptedException, FileNotFoundException, IOException { |
ca449e
|
93 |
|
U |
94 |
int imageWidth = image.getWidth(null); |
671a35
|
95 |
int imageHeight = image.getHeight(null); |
U |
96 |
|
|
97 |
int thumbWidth = width; |
|
98 |
int thumbHeight = height; |
|
99 |
if (imageWidth < width) { |
|
100 |
thumbWidth = imageWidth; |
ca449e
|
101 |
} |
671a35
|
102 |
if (imageHeight < height) { |
ca449e
|
103 |
thumbHeight = imageHeight; |
U |
104 |
} |
671a35
|
105 |
|
U |
106 |
double thumbRatio = (double) thumbWidth / (double) thumbHeight; |
|
107 |
double imageRatio = (double) imageWidth / (double) imageHeight; |
|
108 |
|
|
109 |
if(imageWidth > imageHeight) { |
|
110 |
// Querformat |
|
111 |
thumbHeight = (int) (thumbWidth / imageRatio); |
|
112 |
} else if(imageWidth == imageHeight) { |
|
113 |
// 1:1 kann so bleiben |
|
114 |
} else { |
|
115 |
// Hochformat |
|
116 |
thumbWidth = (int) (thumbHeight * imageRatio); |
|
117 |
} |
ca449e
|
118 |
|
671a35
|
119 |
// draw original image to thumbnail image object and |
U |
120 |
// scale it to the new size on-the-fly |
|
121 |
//BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); |
ca449e
|
122 |
BufferedImage thumbImage; |
671a35
|
123 |
if (withTransparency) { |
U |
124 |
thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_ARGB); |
ca449e
|
125 |
} else { |
U |
126 |
thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); |
|
127 |
} |
671a35
|
128 |
Graphics2D graphics2D = thumbImage.createGraphics(); |
U |
129 |
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); |
|
130 |
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null); |
ca449e
|
131 |
|
671a35
|
132 |
// 30.7.2007: sharpening hinzugefuegt (Anfang) |
U |
133 |
//float factor = -0.15f; // minus = sharpen, plus = soften |
|
134 |
//float[] sharpenArray = {0, -1, 0, -1, 5, -1, 0, -1, 0}; |
ca449e
|
135 |
/* |
U |
136 |
30.6.2013: sharpening als Weichmacher nur, wenn Bild < 400 |
671a35
|
137 |
*/ |
ca449e
|
138 |
/*if(thumbWidth < 400 || thumbHeight < 400) { |
U |
139 |
factor = 0.1f; |
|
140 |
}*/ |
671a35
|
141 |
if (factor != 0.f) { |
U |
142 |
float[] array = {0, factor, 0, factor, 1 - (factor * 4), factor, 0, factor, 0}; |
|
143 |
Kernel kernel = new Kernel(3, 3, array); |
|
144 |
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); |
|
145 |
thumbImage = cOp.filter(thumbImage, null); |
|
146 |
} |
|
147 |
// 30.7.2007: sharpening hinzugefuegt (Ende) |
|
148 |
|
ca449e
|
149 |
return thumbImage; |
U |
150 |
} |
|
151 |
|
671a35
|
152 |
/** |
U |
153 |
* Eine in Groesse und Qualitaet verringerte Bilddatei erzeugen |
|
154 |
* |
|
155 |
* @param image Ablageort und Name der Bilddatei |
|
156 |
* @param width neue Breite |
|
157 |
* @param height neue hoehe |
|
158 |
* @param quality neue Qualitaet (0 - 100) |
|
159 |
* @param factor Faktor fuer Schaerfe / Unschaerfe (z.B. -0.15f fuer leichte |
|
160 |
* Unschaerfe, 0.05f fuer leichtes Nachschaerfen) |
|
161 |
* @param withTransparency ob Transparenz benoetigt wird |
|
162 |
* @return neues, in Groesse und Qualitaet entsprechend verringertes Bild |
|
163 |
* @throws java.lang.InterruptedException |
|
164 |
* @throws java.io.FileNotFoundException |
|
165 |
*/ |
|
166 |
public BufferedImage getReducedImage(Image image, int width, int height, int quality, float factor, boolean withTransparency) |
|
167 |
throws InterruptedException, FileNotFoundException, IOException { |
|
168 |
|
|
169 |
int imageWidth = image.getWidth(null); |
|
170 |
int imageHeight = image.getHeight(null); |
|
171 |
|
|
172 |
int thumbWidth = width; |
|
173 |
int thumbHeight = height; |
|
174 |
if (imageWidth < width) { |
|
175 |
thumbWidth = imageWidth; |
|
176 |
} |
|
177 |
if (imageHeight < height) { |
|
178 |
thumbHeight = imageHeight; |
|
179 |
} |
|
180 |
double thumbRatio = (double) thumbWidth / (double) thumbHeight; |
|
181 |
double imageRatio = (double) imageWidth / (double) imageHeight; |
|
182 |
if (thumbRatio < imageRatio) { |
|
183 |
thumbHeight = (int) (thumbWidth / imageRatio); |
|
184 |
} else { |
|
185 |
thumbWidth = (int) (thumbHeight * imageRatio); |
|
186 |
} |
|
187 |
|
|
188 |
// draw original image to thumbnail image object and |
|
189 |
// scale it to the new size on-the-fly |
|
190 |
//BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); |
|
191 |
BufferedImage thumbImage; |
|
192 |
if (withTransparency) { |
|
193 |
thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_ARGB); |
|
194 |
} else { |
|
195 |
thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); |
|
196 |
} |
|
197 |
Graphics2D graphics2D = thumbImage.createGraphics(); |
|
198 |
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); |
|
199 |
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null); |
|
200 |
|
|
201 |
// 30.7.2007: sharpening hinzugefuegt (Anfang) |
|
202 |
//float factor = -0.15f; // minus = sharpen, plus = soften |
|
203 |
//float[] sharpenArray = {0, -1, 0, -1, 5, -1, 0, -1, 0}; |
|
204 |
/* |
|
205 |
30.6.2013: sharpening als Weichmacher nur, wenn Bild < 400 |
|
206 |
*/ |
|
207 |
/*if(thumbWidth < 400 || thumbHeight < 400) { |
|
208 |
factor = 0.1f; |
|
209 |
}*/ |
|
210 |
if (factor != 0.f) { |
|
211 |
float[] array = {0, factor, 0, factor, 1 - (factor * 4), factor, 0, factor, 0}; |
|
212 |
Kernel kernel = new Kernel(3, 3, array); |
|
213 |
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); |
|
214 |
thumbImage = cOp.filter(thumbImage, null); |
|
215 |
} |
|
216 |
// 30.7.2007: sharpening hinzugefuegt (Ende) |
|
217 |
|
|
218 |
return thumbImage; |
|
219 |
} |
|
220 |
|
ca449e
|
221 |
public String imgType(String mimeType) { |
U |
222 |
String imgType; |
671a35
|
223 |
if (mimeType.contains("jpg")) { |
ca449e
|
224 |
imgType = "jpg"; |
671a35
|
225 |
} else if (mimeType.contains("jpeg")) { |
ca449e
|
226 |
imgType = "jpg"; |
671a35
|
227 |
} else if (mimeType.contains("png")) { |
ca449e
|
228 |
imgType = "png"; |
671a35
|
229 |
} else if (mimeType.contains("gif")) { |
ca449e
|
230 |
imgType = "gif"; |
U |
231 |
} else { |
|
232 |
imgType = "jpg"; |
|
233 |
} |
|
234 |
return imgType; |
|
235 |
} |
671a35
|
236 |
|
ca449e
|
237 |
/** |
671a35
|
238 |
* |
ca449e
|
239 |
* @param v Bild.WINZIG .. Bild.GROSS |
U |
240 |
* @return Länge der längsten Kante in Bildpunkten |
|
241 |
*/ |
|
242 |
public int getVariantenGroesse(int v) { |
|
243 |
return GR[v]; |
|
244 |
} |
671a35
|
245 |
|
ca449e
|
246 |
public String getVariantenName(int v) { |
U |
247 |
return GRNAME[v]; |
|
248 |
} |
671a35
|
249 |
|
ca449e
|
250 |
public String getMimeType(File file) { |
U |
251 |
String absName = file.getAbsolutePath(); |
|
252 |
FileNameMap fileNameMap = URLConnection.getFileNameMap(); |
671a35
|
253 |
return fileNameMap.getContentTypeFor("file://" + absName); |
ca449e
|
254 |
} |
U |
255 |
} |