From 51ca668d316d8b4d284a255ac9b28c112f9f2690 Mon Sep 17 00:00:00 2001
From: ulrich <not disclosed>
Date: Mon, 15 Jan 2018 17:17:53 +0000
Subject: [PATCH] ..

---
 src/bilder.groovy |   60 +++++++++++++++++++++++++++++++++++++++---------------------
 1 files changed, 39 insertions(+), 21 deletions(-)

diff --git a/src/bilder.groovy b/src/bilder.groovy
index c7e9131..00ce2a1 100644
--- a/src/bilder.groovy
+++ b/src/bilder.groovy
@@ -14,24 +14,32 @@
 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;
 
 /*
-	Skript zum Verkleinern von Bildern mit Hilfe 
-	der Klasse Bild
+	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[1];
-String outDirName = args[2];
+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());
@@ -41,17 +49,17 @@
     try {
       mediaTracker.waitForID(0);
       if (!mediaTracker.isErrorAny()) {
-        
-        int quality = 90;
+
+        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;    
+          thumbWidth = imageWidth;
         }
         if(imageHeight < height) {
           thumbHeight = imageHeight;
@@ -64,9 +72,9 @@
         else {
           thumbWidth = (int)(thumbHeight * imageRatio);
         }
- 
-        String mimeType = fileNameMap.getContentTypeFor("file://" + fileList[i].getAbsolutePath());    
-        
+
+        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")) {
@@ -77,7 +85,7 @@
         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};
@@ -87,7 +95,7 @@
         /*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];
@@ -104,8 +112,8 @@
             ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
             thumbImage = cOp.filter(thumbImage, null);
         }
-        // 30.7.2007: sharpening hinzugefuegt (Ende)        
-        
+        // 30.7.2007: sharpening hinzugefuegt (Ende)
+
         String imgType;
         if(mimeType.contains("jpg")) {
           imgType = "jpg";
@@ -118,14 +126,24 @@
         } else {
           imgType = "jpg";
         }
-        
-        ImageIO.write(thumbImage, imgType, outFile);                
+
+				/*
+					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");
-}
\ No newline at end of file
+}

--
Gitblit v1.9.3