import com.sun.image.codec.jpeg.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.io.File;
public class test {
public static void main(String[] args) throws Exception {
// load image from INFILE
File directory = new File("bilder"); //Verzeichnis in dem sich die zu konvertierenden Bilder befinden
String[] dateinamen = directory.list();
int anzahl = dateinamen.length;
System.out.println("Anzahl der Dateien Ordner '" + directory + "': " + anzahl);
// Beginn for-Schleife
int i;
for (i = 0; i < anzahl; i++)
{
System.out.println("Konvertiere '" + dateinamen[i] + "'!");
// System.out.println(dateinamen[i]);
Image image = Toolkit.getDefaultToolkit().getImage(dateinamen[i]);
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
// determine thumbnail size from WIDTH and HEIGHT
int thumbWidth = Integer.parseInt("22"); // Max Breite der Bilder
int thumbHeight = Integer.parseInt("30"); // Max Höhe der Bilder
double thumbRatio = (double)thumbWidth / (double)thumbHeight;
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
double imageRatio = (double)imageWidth / (double)imageHeight;
if (thumbRatio < imageRatio) {
thumbHeight = (int)(thumbWidth / imageRatio);
} else {
thumbWidth = (int)(thumbHeight * imageRatio);
}
// draw original image to thumbnail image object and
// scale it to the new size on-the-fly
BufferedImage thumbImage = new BufferedImage(thumbWidth,
thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
// save thumbnail image to OUTFILE
//Ausgabe Das Verzeichnis wird nicht neu erstellt und muss schon bestehen
BufferedOutputStream out = new BufferedOutputStream(new
FileOutputStream( (directory + "/kl_" + dateinamen[i]) ));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.
getDefaultJPEGEncodeParam(thumbImage);
int quality = Integer.parseInt("70"); //Qualität des Bildes
quality = Math.max(0, Math.min(quality, 100));
param.setQuality((float)quality / 100.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(thumbImage);
out.close();
}
// Ende for-Schleife
System.out.println("Fertig mit dem Konvertieren!");
System.exit(0);
}
}