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 |
/* |
9ec1f8
|
20 |
Skript zum Verkleinern von Bildern mit Hilfe |
1cc542
|
21 |
der Klasse Bild |
9ec1f8
|
22 |
|
U |
23 |
args[0] - Anzahl Pixel an der laengsten Kante |
|
24 |
args[1] - qualitaet JPEG, z.B. 75 fuer 75% |
|
25 |
args[2] - Eingangsordner |
|
26 |
args[3] - Ausgabeordner |
1cc542
|
27 |
*/ |
U |
28 |
|
874757
|
29 |
FileNameMap fileNameMap = URLConnection.getFileNameMap(); |
U |
30 |
int width = Integer.parseInt(args[0]); |
|
31 |
int height = width; |
9ec1f8
|
32 |
String inDirName = args[2]; |
U |
33 |
String outDirName = args[3]; |
874757
|
34 |
File inFile = new File(inDirName); |
1cc542
|
35 |
File[] fileList = inFile.listFiles(); |
U |
36 |
if(fileList != null && fileList.length > 0) { |
|
37 |
for(int i = 0; i < fileList.length; i++) { |
|
38 |
System.out.println(fileList[i].getAbsolutePath()); |
9ec1f8
|
39 |
|
874757
|
40 |
File outDir = new File(outDirName); |
1cc542
|
41 |
File outFile = new File(outDir, fileList[i].getName()); |
U |
42 |
System.out.println(outFile.getAbsolutePath()); |
|
43 |
Image image = ImageIO.read(fileList[i]); |
|
44 |
MediaTracker mediaTracker = new MediaTracker(new Container()); |
|
45 |
mediaTracker.addImage(image, 0); |
|
46 |
try { |
|
47 |
mediaTracker.waitForID(0); |
|
48 |
if (!mediaTracker.isErrorAny()) { |
9ec1f8
|
49 |
|
U |
50 |
int quality = Integer.parseInt(args[1]); |
874757
|
51 |
float factor = (float) 0.0; |
9ec1f8
|
52 |
|
874757
|
53 |
BufferedImage thumbImage; |
U |
54 |
int imageWidth = image.getWidth(null); |
1cc542
|
55 |
int imageHeight = image.getHeight(null); |
874757
|
56 |
int thumbWidth = width; |
1cc542
|
57 |
int thumbHeight = height; |
874757
|
58 |
if(imageWidth < width) { |
9ec1f8
|
59 |
thumbWidth = imageWidth; |
874757
|
60 |
} |
U |
61 |
if(imageHeight < height) { |
|
62 |
thumbHeight = imageHeight; |
|
63 |
} |
1cc542
|
64 |
double thumbRatio = (double)thumbWidth / (double)thumbHeight; |
U |
65 |
double imageRatio = (double)imageWidth / (double)imageHeight; |
|
66 |
if (thumbRatio < imageRatio) { |
874757
|
67 |
thumbHeight = (int)(thumbWidth / imageRatio); |
1cc542
|
68 |
} |
U |
69 |
else { |
874757
|
70 |
thumbWidth = (int)(thumbHeight * imageRatio); |
1cc542
|
71 |
} |
9ec1f8
|
72 |
|
U |
73 |
String mimeType = fileNameMap.getContentTypeFor("file://" + fileList[i].getAbsolutePath()); |
|
74 |
|
1cc542
|
75 |
// draw original image to thumbnail image object and |
U |
76 |
// scale it to the new size on-the-fly |
874757
|
77 |
if(mimeType.contains("jpeg") || mimeType.contains("png")) { |
U |
78 |
thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB); |
|
79 |
} else { |
|
80 |
thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_ARGB); |
|
81 |
} |
1cc542
|
82 |
Graphics2D graphics2D = thumbImage.createGraphics(); |
U |
83 |
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); |
|
84 |
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null); |
9ec1f8
|
85 |
|
1cc542
|
86 |
// 30.7.2007: sharpening hinzugefuegt (Anfang) |
U |
87 |
//float factor = -0.15f; // minus = sharpen, plus = soften |
|
88 |
//float[] sharpenArray = {0, -1, 0, -1, 5, -1, 0, -1, 0}; |
874757
|
89 |
/* |
U |
90 |
30.6.2013: sharpening als Weichmacher nur, wenn Bild < 400 |
|
91 |
*/ |
|
92 |
/*if(thumbWidth < 400 || thumbHeight < 400) { |
|
93 |
factor = 0.1f; |
|
94 |
}*/ |
9ec1f8
|
95 |
|
1cc542
|
96 |
if(factor != (float) 0.0) { |
U |
97 |
//float[] array = {0, factor, 0, factor, 1-(factor*4), factor, 0, factor, 0}; |
|
98 |
float[] array = new float[9]; |
|
99 |
array[0] = 0; |
|
100 |
array[1] = factor; |
|
101 |
array[2] = 0; |
|
102 |
array[3] = factor; |
|
103 |
array[4] = 1-(factor*4); |
|
104 |
array[5] = factor; |
|
105 |
array[6] = 0; |
|
106 |
array[7] = factor; |
|
107 |
array[8] = 0; |
|
108 |
Kernel kernel = new Kernel(3, 3, array); |
|
109 |
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); |
|
110 |
thumbImage = cOp.filter(thumbImage, null); |
|
111 |
} |
9ec1f8
|
112 |
// 30.7.2007: sharpening hinzugefuegt (Ende) |
U |
113 |
|
874757
|
114 |
String imgType; |
U |
115 |
if(mimeType.contains("jpg")) { |
|
116 |
imgType = "jpg"; |
|
117 |
} else if(mimeType.contains("jpeg")) { |
|
118 |
imgType = "jpg"; |
|
119 |
} else if(mimeType.contains("png")) { |
|
120 |
imgType = "png"; |
|
121 |
} else if(mimeType.contains("gif")) { |
|
122 |
imgType = "gif"; |
|
123 |
} else { |
|
124 |
imgType = "jpg"; |
|
125 |
} |
9ec1f8
|
126 |
|
U |
127 |
ImageIO.write(thumbImage, imgType, outFile); |
874757
|
128 |
} |
U |
129 |
} catch (InterruptedException ex) { |
|
130 |
System.out.println("Error: " + ex.getLocalizedMessage()); |
9ec1f8
|
131 |
} |
U |
132 |
|
874757
|
133 |
} |
U |
134 |
} else { |
|
135 |
System.out.println("fileList is null or empty"); |
9ec1f8
|
136 |
} |