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