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