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 |
|
|
107 |
File outDir = new File(outDirName); |
|
108 |
File outFile = new File(outDir, fileList[i].getName()); |
|
109 |
//System.out.println(outFile.getAbsolutePath()); |
|
110 |
try { |
|
111 |
Image image = ImageIO.read(fileList[i]); |
|
112 |
MediaTracker mediaTracker = new MediaTracker(new Container()); |
|
113 |
mediaTracker.addImage(image, 0); |
|
114 |
mediaTracker.waitForID(0); |
|
115 |
if (!mediaTracker.isErrorAny()) { |
|
116 |
|
|
117 |
//float quality = Float.parseFloat(args[1]); |
|
118 |
float factor = (float) 0.0; |
|
119 |
|
|
120 |
BufferedImage thumbImage; |
|
121 |
int imageWidth = image.getWidth(null); |
|
122 |
int imageHeight = image.getHeight(null); |
|
123 |
int thumbWidth = width; |
|
124 |
int thumbHeight = height; |
|
125 |
if(imageWidth < width) { |
|
126 |
thumbWidth = imageWidth; |
|
127 |
} |
|
128 |
if(imageHeight < height) { |
|
129 |
thumbHeight = imageHeight; |
|
130 |
} |
|
131 |
double thumbRatio = (double)thumbWidth / (double)thumbHeight; |
|
132 |
double imageRatio = (double)imageWidth / (double)imageHeight; |
|
133 |
if (thumbRatio < imageRatio) { |
|
134 |
thumbHeight = (int)(thumbWidth / imageRatio); |
|
135 |
} |
|
136 |
else { |
|
137 |
thumbWidth = (int)(thumbHeight * imageRatio); |
|
138 |
} |
|
139 |
|
|
140 |
String mimeType = fileNameMap.getContentTypeFor("file://" + fileList[i].getAbsolutePath()); |
|
141 |
|
|
142 |
// draw original image to thumbnail image object and |
|
143 |
// scale it to the new size on-the-fly |
|
144 |
if(mimeType.contains("jpeg") || mimeType.contains("png")) { |
|
145 |
thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); |
|
146 |
} else { |
|
147 |
thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_ARGB); |
|
148 |
} |
|
149 |
Graphics2D graphics2D = thumbImage.createGraphics(); |
|
150 |
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); |
|
151 |
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null); |
|
152 |
|
|
153 |
// 30.7.2007: sharpening hinzugefuegt (Anfang) |
|
154 |
//float factor = -0.15f; // minus = sharpen, plus = soften |
|
155 |
//float[] sharpenArray = {0, -1, 0, -1, 5, -1, 0, -1, 0}; |
|
156 |
/* |
|
157 |
30.6.2013: sharpening als Weichmacher nur, wenn Bild < 400 |
|
158 |
*/ |
|
159 |
/*if(thumbWidth < 400 || thumbHeight < 400) { |
|
160 |
factor = 0.1f; |
|
161 |
}*/ |
|
162 |
|
|
163 |
if(factor != (float) 0.0) { |
|
164 |
//float[] array = {0, factor, 0, factor, 1-(factor*4), factor, 0, factor, 0}; |
|
165 |
float[] array = new float[9]; |
|
166 |
array[0] = 0; |
|
167 |
array[1] = factor; |
|
168 |
array[2] = 0; |
|
169 |
array[3] = factor; |
|
170 |
array[4] = 1-(factor*4); |
|
171 |
array[5] = factor; |
|
172 |
array[6] = 0; |
|
173 |
array[7] = factor; |
|
174 |
array[8] = 0; |
|
175 |
Kernel kernel = new Kernel(3, 3, array); |
|
176 |
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); |
|
177 |
thumbImage = cOp.filter(thumbImage, null); |
|
178 |
} |
|
179 |
// 30.7.2007: sharpening hinzugefuegt (Ende) |
|
180 |
|
|
181 |
String imgType; |
|
182 |
if(mimeType.contains("jpg")) { |
|
183 |
imgType = "jpg"; |
|
184 |
} else if(mimeType.contains("jpeg")) { |
|
185 |
imgType = "jpg"; |
|
186 |
} else if(mimeType.contains("png")) { |
|
187 |
imgType = "png"; |
|
188 |
} else if(mimeType.contains("gif")) { |
|
189 |
imgType = "gif"; |
|
190 |
} else { |
|
191 |
imgType = "jpg"; |
|
192 |
} |
|
193 |
|
|
194 |
/* |
|
195 |
14.1.2018: Ausgabe um Qualitaetsparameter erweitert |
|
196 |
Beginn |
|
197 |
*/ |
|
198 |
ImageWriter writer = ImageIO.getImageWritersByFormatName(imgType).next(); |
|
199 |
ImageWriteParam iwp = writer.getDefaultWriteParam(); |
|
200 |
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); |
|
201 |
iwp.setCompressionQuality(quality); |
|
202 |
writer.setOutput(new FileImageOutputStream(outFile)); |
|
203 |
writer.write(null, new IIOImage(thumbImage, null, null), iwp); |
|
204 |
writer.dispose(); |
|
205 |
/* 14.1.2018 Ende */ |
|
206 |
} |
|
207 |
} catch (InterruptedException ex) { |
|
208 |
//System.out.println("Error: " + ex.getLocalizedMessage()); |
|
209 |
} catch (FileNotFoundException e) { |
|
210 |
e.printStackTrace(); |
|
211 |
} catch (IOException e) { |
|
212 |
e.printStackTrace(); |
|
213 |
} |
|
214 |
} |
|
215 |
} else { |
|
216 |
//System.out.println("fileList is null or empty"); |
|
217 |
} |
|
218 |
} |
|
219 |
} |
|
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|