commit | author | age
|
1cc542
|
1 |
import java.awt.Graphics2D; |
U |
2 |
import java.awt.Image; |
|
3 |
import java.awt.RenderingHints; |
|
4 |
import java.awt.image.BufferedImage; |
|
5 |
import java.awt.image.ConvolveOp; |
|
6 |
import java.awt.image.Kernel; |
|
7 |
import java.awt.MediaTracker; |
|
8 |
import java.awt.Container; |
|
9 |
import java.awt.Toolkit; |
|
10 |
import java.io.File; |
|
11 |
import java.io.FileNotFoundException; |
|
12 |
import java.io.IOException; |
|
13 |
import java.io.OutputStream; |
|
14 |
import java.net.FileNameMap; |
|
15 |
import java.net.URLConnection; |
|
16 |
import javax.imageio.ImageIO; |
|
17 |
import java.lang.Integer; |
|
18 |
|
|
19 |
/* |
|
20 |
Skript zum Verkleinern von Bildern mit Hilfe |
|
21 |
der Klasse Bild |
|
22 |
*/ |
|
23 |
|
|
24 |
File inFile = new File(args[1]); |
|
25 |
File[] fileList = inFile.listFiles(); |
|
26 |
if(fileList != null && fileList.length > 0) { |
|
27 |
for(int i = 0; i < fileList.length; i++) { |
|
28 |
System.out.println(fileList[i].getAbsolutePath()); |
|
29 |
|
|
30 |
File outDir = new File(args[2]); |
|
31 |
File outFile = new File(outDir, fileList[i].getName()); |
|
32 |
System.out.println(outFile.getAbsolutePath()); |
|
33 |
//java.awt.Image image = (java.awt.Image) Toolkit.getDefaultToolkit().getImage(fileList[i].getAbsolutePath()); |
|
34 |
Image image = ImageIO.read(fileList[i]); |
|
35 |
MediaTracker mediaTracker = new MediaTracker(new Container()); |
|
36 |
mediaTracker.addImage(image, 0); |
|
37 |
try { |
|
38 |
mediaTracker.waitForID(0); |
|
39 |
if (!mediaTracker.isErrorAny()) { |
|
40 |
Bild bild = new Bild(); |
|
41 |
bild.writeImageFile(image, Integer.parseInt(args[0]), bild.getMimeType(fileList[i]), new File(outDir, fileList[i].getName()).getAbsolutePath()); |
|
42 |
} |
|
43 |
} catch (InterruptedException ex) { |
|
44 |
System.out.println("Error: " + ex.getLocalizedMessage()); |
|
45 |
} |
|
46 |
|
|
47 |
} |
|
48 |
} else { |
|
49 |
System.out.println("fileList is null or empty"); |
|
50 |
} |
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
/* |
|
55 |
Klasse Bild |
|
56 |
*/ |
|
57 |
|
|
58 |
public class Bild { |
|
59 |
|
|
60 |
public static final int WINZIG = 0; |
|
61 |
public static final int KLEIN = 1; |
|
62 |
public static final int SEMI = 2; |
|
63 |
public static final int MITTEL = 3; |
|
64 |
public static final int GROSS = 4; |
|
65 |
|
|
66 |
public String[] GRNAME; // = {"-w", "-k", "-s", "-m", "-g"}; |
|
67 |
public int[] GR; // = {120, 240, 500, 700, 1200}; |
|
68 |
|
|
69 |
public Bild() { |
|
70 |
GRNAME = new String[5] |
|
71 |
GRNAME[0] = "-w"; |
|
72 |
GRNAME[1] = "-k"; |
|
73 |
GRNAME[2] = "-s"; |
|
74 |
GRNAME[3] = "-m"; |
|
75 |
GRNAME[4] = "-g"; |
|
76 |
|
|
77 |
GR = new int[5]; |
|
78 |
GR[0] = 120; |
|
79 |
GR[1] = 240; |
|
80 |
GR[2] = 500; |
|
81 |
GR[3] = 700; |
|
82 |
GR[4] = 1200; |
|
83 |
|
|
84 |
} |
|
85 |
|
|
86 |
public void writeImageStream(Image image, int gr, String mimeType, OutputStream out) throws InterruptedException, IOException { |
|
87 |
ImageIO.write(getReducedImage(image, gr, mimeType), imgType(mimeType), out); |
|
88 |
} |
|
89 |
|
|
90 |
/** |
|
91 |
* |
|
92 |
* @param image |
|
93 |
* @param gr |
|
94 |
* @param mimeType |
|
95 |
* @param vName Name der verkleinerten Datei |
|
96 |
* @throws InterruptedException |
|
97 |
* @throws IOException |
|
98 |
*/ |
|
99 |
public void writeImageFile(BufferedImage image, int gr, String mimeType, String vName) throws InterruptedException, IOException { |
|
100 |
ImageIO.write(getReducedImage(image, gr, mimeType), imgType(mimeType), new File(vName)); |
|
101 |
} |
|
102 |
|
|
103 |
private BufferedImage getReducedImage(Image image, int gr, String mimeType) throws InterruptedException, IOException { |
|
104 |
BufferedImage img; |
|
105 |
int q = 90; |
|
106 |
float sh = (float) 0.0; |
|
107 |
if(mimeType.contains("jpeg")) { |
|
108 |
img = getReducedImageImpl(image, gr, gr, q, sh, false); |
|
109 |
} else { |
|
110 |
img = getReducedImageImpl(image, gr, gr, q, sh, true); |
|
111 |
} |
|
112 |
return img; |
|
113 |
} |
|
114 |
|
|
115 |
/** |
|
116 |
* Eine in Groesse und Qualitaet verringerte Bilddatei erzeugen |
|
117 |
* @param image Ablageort und Name der Bilddatei |
|
118 |
* @param width neue Breite |
|
119 |
* @param height neue hoehe |
|
120 |
* @param quality neue Qualitaet (0 - 100) |
|
121 |
* @param factor Faktor fuer Schaerfe / Unschaerfe (z.B. -0.15f fuer leichte Unschaerfe, 0.05f fuer leichtes Nachschaerfen) |
|
122 |
* @param withTransparency ob Transparenz benoetigt wird |
|
123 |
* @return neues, in Groesse und Qualitaet entsprechend verringertes Bild |
|
124 |
* @throws java.lang.InterruptedException |
|
125 |
* @throws java.io.FileNotFoundException |
|
126 |
*/ |
|
127 |
public BufferedImage getReducedImageImpl(BufferedImage image, int width, int height, int quality, float factor, boolean withTransparency) |
|
128 |
throws InterruptedException, FileNotFoundException, IOException { |
|
129 |
|
|
130 |
int imageWidth = image.getWidth(null); |
|
131 |
int imageHeight = image.getHeight(null); |
|
132 |
|
|
133 |
int thumbWidth = width; |
|
134 |
int thumbHeight = height; |
|
135 |
if(imageWidth < width) { |
|
136 |
thumbWidth = imageWidth; |
|
137 |
} |
|
138 |
if(imageHeight < height) { |
|
139 |
thumbHeight = imageHeight; |
|
140 |
} |
|
141 |
double thumbRatio = (double)thumbWidth / (double)thumbHeight; |
|
142 |
double imageRatio = (double)imageWidth / (double)imageHeight; |
|
143 |
if (thumbRatio < imageRatio) { |
|
144 |
thumbHeight = (int)(thumbWidth / imageRatio); |
|
145 |
} |
|
146 |
else { |
|
147 |
thumbWidth = (int)(thumbHeight * imageRatio); |
|
148 |
} |
|
149 |
|
|
150 |
// draw original image to thumbnail image object and |
|
151 |
// scale it to the new size on-the-fly |
|
152 |
//BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); |
|
153 |
BufferedImage thumbImage; |
|
154 |
if(withTransparency) { |
|
155 |
thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_ARGB); |
|
156 |
} else { |
|
157 |
thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); |
|
158 |
} |
|
159 |
Graphics2D graphics2D = thumbImage.createGraphics(); |
|
160 |
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); |
|
161 |
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null); |
|
162 |
|
|
163 |
// 30.7.2007: sharpening hinzugefuegt (Anfang) |
|
164 |
//float factor = -0.15f; // minus = sharpen, plus = soften |
|
165 |
//float[] sharpenArray = {0, -1, 0, -1, 5, -1, 0, -1, 0}; |
|
166 |
|
|
167 |
/* |
|
168 |
30.6.2013: sharpening als Weichmacher nur, wenn Bild < 400 |
|
169 |
*/ |
|
170 |
/*if(thumbWidth < 400 || thumbHeight < 400) { |
|
171 |
factor = 0.1f; |
|
172 |
}*/ |
|
173 |
|
|
174 |
if(factor != (float) 0.0) { |
|
175 |
//float[] array = {0, factor, 0, factor, 1-(factor*4), factor, 0, factor, 0}; |
|
176 |
float[] array = new float[9]; |
|
177 |
array[0] = 0; |
|
178 |
array[1] = factor; |
|
179 |
array[2] = 0; |
|
180 |
array[3] = factor; |
|
181 |
array[4] = 1-(factor*4); |
|
182 |
array[5] = factor; |
|
183 |
array[6] = 0; |
|
184 |
array[7] = factor; |
|
185 |
array[8] = 0; |
|
186 |
Kernel kernel = new Kernel(3, 3, array); |
|
187 |
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); |
|
188 |
thumbImage = cOp.filter(thumbImage, null); |
|
189 |
} |
|
190 |
// 30.7.2007: sharpening hinzugefuegt (Ende) |
|
191 |
|
|
192 |
return thumbImage; |
|
193 |
} |
|
194 |
|
|
195 |
public String imgType(String mimeType) { |
|
196 |
String imgType; |
|
197 |
if(mimeType.contains("jpg")) { |
|
198 |
imgType = "jpg"; |
|
199 |
} else if(mimeType.contains("jpeg")) { |
|
200 |
imgType = "jpg"; |
|
201 |
} else if(mimeType.contains("png")) { |
|
202 |
imgType = "png"; |
|
203 |
} else if(mimeType.contains("gif")) { |
|
204 |
imgType = "gif"; |
|
205 |
} else { |
|
206 |
imgType = "jpg"; |
|
207 |
} |
|
208 |
return imgType; |
|
209 |
} |
|
210 |
|
|
211 |
/** |
|
212 |
* |
|
213 |
* @param v Bild.WINZIG .. Bild.GROSS |
|
214 |
* @return Länge der längsten Kante in Bildpunkten |
|
215 |
*/ |
|
216 |
public int getVariantenGroesse(int v) { |
|
217 |
return GR[v]; |
|
218 |
} |
|
219 |
|
|
220 |
public String getVariantenName(int v) { |
|
221 |
return GRNAME[v]; |
|
222 |
} |
|
223 |
|
|
224 |
public String getMimeType(File file) { |
|
225 |
String absName = file.getAbsolutePath(); |
|
226 |
FileNameMap fileNameMap = URLConnection.getFileNameMap(); |
|
227 |
return fileNameMap.getContentTypeFor("file://" + absName); |
|
228 |
} |
|
229 |
} |
|
230 |
|
|
231 |
|
|
232 |
|
|
233 |
|
|
234 |
|