commit | author | age
|
b0b4cd
|
1 |
package de.uhilger.bildhelfer; |
U |
2 |
|
|
3 |
import java.awt.Graphics2D; |
|
4 |
import java.awt.Image; |
|
5 |
import java.awt.RenderingHints; |
|
6 |
import java.awt.image.BufferedImage; |
|
7 |
import java.awt.image.ConvolveOp; |
|
8 |
import java.awt.image.Kernel; |
|
9 |
import java.awt.MediaTracker; |
|
10 |
import java.awt.Container; |
|
11 |
import java.io.File; |
|
12 |
import java.io.FileNotFoundException; |
|
13 |
import java.io.IOException; |
|
14 |
import java.net.FileNameMap; |
|
15 |
import java.net.URLConnection; |
|
16 |
import java.util.logging.Level; |
|
17 |
import java.util.logging.Logger; |
|
18 |
import javax.imageio.ImageIO; |
|
19 |
import javax.imageio.ImageWriter; |
|
20 |
import javax.imageio.ImageWriteParam; |
|
21 |
import javax.imageio.stream.FileImageOutputStream; |
|
22 |
import javax.imageio.IIOImage; |
|
23 |
|
|
24 |
class Verkleinerer { |
|
25 |
|
|
26 |
private static final Logger logger = Logger.getLogger(Verkleinerer.class.getName()); |
|
27 |
|
|
28 |
void verkleinern(String[] args) { |
|
29 |
int groesse = getIntFromArg(args[1], 1200); |
|
30 |
float qualitaet = getFloatFromArg(args[2], (float) 0.7); |
|
31 |
File eingangsOrdner = getFileFromArg(args[3]); |
|
32 |
if(eingangsOrdner == null) { |
|
33 |
logger.info("Eingangsordner konnte nicht bestimmt werden, Abbruch."); |
|
34 |
return; |
|
35 |
} else { |
|
36 |
logger.info("Eingangsordner " + eingangsOrdner); |
|
37 |
} |
|
38 |
File ausgabeOrdner = getFileFromArg(args[4]); |
|
39 |
if(ausgabeOrdner == null) { |
|
40 |
logger.info("Ausgabeordner konnte nicht bestimmt werden, Abbruch."); |
|
41 |
return; |
|
42 |
} else { |
|
43 |
logger.info("Ausgabeordner " + ausgabeOrdner); |
|
44 |
} |
|
45 |
dateienVerarbeiten(groesse, qualitaet, eingangsOrdner.getAbsolutePath(), ausgabeOrdner.getAbsolutePath()); |
|
46 |
} |
|
47 |
|
|
48 |
private int getIntFromArg(String arg, int defaultValue) { |
|
49 |
try { |
|
50 |
int argValue = Integer.parseInt(arg); |
|
51 |
logger.info("Ausgabegroesse " + argValue); |
|
52 |
return argValue; |
|
53 |
} catch(Exception ex) { |
|
54 |
logger.info("Ausgabegroesse konnte nicht ermittelt werden, es wird der Standardwert " + defaultValue + " verwendet."); |
|
55 |
return defaultValue; |
|
56 |
} |
|
57 |
} |
|
58 |
|
|
59 |
private float getFloatFromArg(String arg, float defaultValue) { |
|
60 |
try { |
|
61 |
float argValue = Float.parseFloat(arg); |
|
62 |
logger.info("Ausgabequalitaet " + argValue); |
|
63 |
return argValue; |
|
64 |
} catch(Exception ex) { |
|
65 |
logger.info("Ausgabequalitaet konnte nicht ermittelt werden, es wird der Standardwert " + defaultValue + " verwendet."); |
|
66 |
return defaultValue; |
|
67 |
} |
|
68 |
} |
|
69 |
|
|
70 |
private File getFileFromArg(String arg) { |
|
71 |
try { |
|
72 |
File f = new File(arg); |
|
73 |
if(!f.exists()) { |
|
74 |
return null; |
|
75 |
} else { |
|
76 |
return f; |
|
77 |
} |
|
78 |
} catch(Exception ex) { |
|
79 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
80 |
return null; |
|
81 |
} |
|
82 |
} |
|
83 |
|
|
84 |
|
|
85 |
private void dateienVerarbeiten(int gr, float quality, String inDirName, String outDirName) { |
|
86 |
/* |
|
87 |
Java-Code zum Verkleinern von Bildern |
|
88 |
|
|
89 |
args[0] - Anzahl Pixel an der laengsten Kante |
|
90 |
args[1] - qualitaet JPEG, z.B. 0.75 fuer 75% des Originals |
|
91 |
args[2] - Eingangsordner |
|
92 |
args[3] - Ausgabeordner |
|
93 |
*/ |
|
94 |
|
|
95 |
FileNameMap fileNameMap = URLConnection.getFileNameMap(); |
|
96 |
//int width = Integer.parseInt(args[0]); |
|
97 |
int width = gr; |
|
98 |
int height = width; |
|
99 |
//String inDirName = args[2]; |
|
100 |
//String outDirName = args[3]; |
|
101 |
File inFile = new File(inDirName); |
|
102 |
File[] fileList = inFile.listFiles(); |
|
103 |
if(fileList != null && fileList.length > 0) { |
|
104 |
for(int i = 0; i < fileList.length; i++) { |
|
105 |
//System.out.println(fileList[i].getAbsolutePath()); |
|
106 |
|
3435e8
|
107 |
logger.fine(fileList[i].getAbsolutePath()); |
U |
108 |
|
b0b4cd
|
109 |
File outDir = new File(outDirName); |
U |
110 |
File outFile = new File(outDir, fileList[i].getName()); |
3435e8
|
111 |
logger.info(outFile.getAbsolutePath()); |
b0b4cd
|
112 |
try { |
U |
113 |
Image image = ImageIO.read(fileList[i]); |
|
114 |
MediaTracker mediaTracker = new MediaTracker(new Container()); |
|
115 |
mediaTracker.addImage(image, 0); |
|
116 |
mediaTracker.waitForID(0); |
|
117 |
if (!mediaTracker.isErrorAny()) { |
|
118 |
|
|
119 |
//float quality = Float.parseFloat(args[1]); |
|
120 |
float factor = (float) 0.0; |
|
121 |
|
|
122 |
BufferedImage thumbImage; |
|
123 |
int imageWidth = image.getWidth(null); |
|
124 |
int imageHeight = image.getHeight(null); |
|
125 |
int thumbWidth = width; |
|
126 |
int thumbHeight = height; |
|
127 |
if(imageWidth < width) { |
|
128 |
thumbWidth = imageWidth; |
|
129 |
} |
|
130 |
if(imageHeight < height) { |
|
131 |
thumbHeight = imageHeight; |
|
132 |
} |
|
133 |
double thumbRatio = (double)thumbWidth / (double)thumbHeight; |
|
134 |
double imageRatio = (double)imageWidth / (double)imageHeight; |
|
135 |
if (thumbRatio < imageRatio) { |
|
136 |
thumbHeight = (int)(thumbWidth / imageRatio); |
|
137 |
} |
|
138 |
else { |
|
139 |
thumbWidth = (int)(thumbHeight * imageRatio); |
|
140 |
} |
|
141 |
|
|
142 |
String mimeType = fileNameMap.getContentTypeFor("file://" + fileList[i].getAbsolutePath()); |
|
143 |
|
|
144 |
// draw original image to thumbnail image object and |
|
145 |
// scale it to the new size on-the-fly |
|
146 |
if(mimeType.contains("jpeg") || mimeType.contains("png")) { |
|
147 |
thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); |
|
148 |
} else { |
|
149 |
thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_ARGB); |
|
150 |
} |
|
151 |
Graphics2D graphics2D = thumbImage.createGraphics(); |
|
152 |
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); |
|
153 |
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null); |
|
154 |
|
|
155 |
// 30.7.2007: sharpening hinzugefuegt (Anfang) |
|
156 |
//float factor = -0.15f; // minus = sharpen, plus = soften |
|
157 |
//float[] sharpenArray = {0, -1, 0, -1, 5, -1, 0, -1, 0}; |
|
158 |
/* |
|
159 |
30.6.2013: sharpening als Weichmacher nur, wenn Bild < 400 |
|
160 |
*/ |
|
161 |
/*if(thumbWidth < 400 || thumbHeight < 400) { |
|
162 |
factor = 0.1f; |
|
163 |
}*/ |
|
164 |
|
|
165 |
if(factor != (float) 0.0) { |
|
166 |
//float[] array = {0, factor, 0, factor, 1-(factor*4), factor, 0, factor, 0}; |
|
167 |
float[] array = new float[9]; |
|
168 |
array[0] = 0; |
|
169 |
array[1] = factor; |
|
170 |
array[2] = 0; |
|
171 |
array[3] = factor; |
|
172 |
array[4] = 1-(factor*4); |
|
173 |
array[5] = factor; |
|
174 |
array[6] = 0; |
|
175 |
array[7] = factor; |
|
176 |
array[8] = 0; |
|
177 |
Kernel kernel = new Kernel(3, 3, array); |
|
178 |
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); |
|
179 |
thumbImage = cOp.filter(thumbImage, null); |
|
180 |
} |
|
181 |
// 30.7.2007: sharpening hinzugefuegt (Ende) |
|
182 |
|
|
183 |
String imgType; |
|
184 |
if(mimeType.contains("jpg")) { |
|
185 |
imgType = "jpg"; |
|
186 |
} else if(mimeType.contains("jpeg")) { |
|
187 |
imgType = "jpg"; |
|
188 |
} else if(mimeType.contains("png")) { |
|
189 |
imgType = "png"; |
|
190 |
} else if(mimeType.contains("gif")) { |
|
191 |
imgType = "gif"; |
|
192 |
} else { |
|
193 |
imgType = "jpg"; |
|
194 |
} |
|
195 |
|
|
196 |
/* |
|
197 |
14.1.2018: Ausgabe um Qualitaetsparameter erweitert |
|
198 |
Beginn |
|
199 |
*/ |
|
200 |
ImageWriter writer = ImageIO.getImageWritersByFormatName(imgType).next(); |
|
201 |
ImageWriteParam iwp = writer.getDefaultWriteParam(); |
|
202 |
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); |
|
203 |
iwp.setCompressionQuality(quality); |
|
204 |
writer.setOutput(new FileImageOutputStream(outFile)); |
|
205 |
writer.write(null, new IIOImage(thumbImage, null, null), iwp); |
|
206 |
writer.dispose(); |
|
207 |
/* 14.1.2018 Ende */ |
|
208 |
} |
|
209 |
} catch (InterruptedException ex) { |
|
210 |
//System.out.println("Error: " + ex.getLocalizedMessage()); |
3435e8
|
211 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
b0b4cd
|
212 |
} catch (FileNotFoundException e) { |
3435e8
|
213 |
logger.log(Level.SEVERE, e.getLocalizedMessage(), e); |
b0b4cd
|
214 |
} catch (IOException e) { |
3435e8
|
215 |
logger.log(Level.SEVERE, e.getLocalizedMessage(), e); |
b0b4cd
|
216 |
} |
U |
217 |
} |
|
218 |
} else { |
3435e8
|
219 |
logger.info("fileList is null or empty"); |
b0b4cd
|
220 |
} |
U |
221 |
} |
|
222 |
} |
|
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|