The following links helped me a lot!
import java.util.*;
import java.io.*;
import java.awt.image.*;
import java.awt.*;
import javax.imageio.ImageIO;
import java.io.FilenameFilter;
public class Autoboxing
{
public static String DESTINATION_PATH = "C:\\destination";
public static String SOURCE_PATH = "C:\\source";
public static boolean verbose = false;
public static int MAX_RETRIES = 20;
public static void saveImage(BufferedImage img, String ref)
{
try {
String format = (ref.endsWith(".png")) ? "png" : "jpg";
ImageIO.write(img, format, new File(ref));
} catch (IOException e) {
e.printStackTrace();
}
}
public static BufferedImage loadImage(File ref)
{
BufferedImage bimg = null;
try {
bimg = ImageIO.read(ref);
} catch (Exception e) {
e.printStackTrace();
}
return bimg;
}
public static BufferedImage getScaledInstance(BufferedImage img,
int targetWidth,
int targetHeight,
Object hint,
boolean higherQuality)
{
int type = (img.getTransparency() == Transparency.OPAQUE) ?
BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
BufferedImage ret = (BufferedImage)img;
int w, h;
if (higherQuality) {
w = img.getWidth();
h = img.getHeight();
} else {
w = targetWidth;
h = targetHeight;
}
int count = 0;
do {
count ++;
if (count > MAX_RETRIES)
{
throw new RuntimeException("Max retries reached.");
}
if (higherQuality && w > targetWidth) {
w /= 2;
if (w < targetWidth) {
w = targetWidth;
}
}
if (higherQuality && h > targetHeight) {
h /= 2;
if (h < targetHeight) {
h = targetHeight;
}
}
BufferedImage tmp = new BufferedImage(w, h, type);
Graphics2D g2 = tmp.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
g2.drawImage(ret, 0, 0, w, h, null);
g2.dispose();
ret = tmp;
} while (w != targetWidth || h != targetHeight);
return ret;
}
public static void changeFiles(
File directory,
File destination,
FilenameFilter filter,
boolean recurse,
boolean recreate,
boolean overwrite)
throws IOException
{
if (verbose) System.out.println("Checking directory : " + directory.getCanonicalPath() + " with " + destination.getCanonicalPath() );
Vector<File> files = new Vector<File>();
File[] entries = directory.listFiles();
for (File entry : entries)
{
if (verbose) System.out.println(" File: " + entry.getName());
if (filter == null || filter.accept(directory, entry.getName()))
{
File newFile = new File(destination.getCanonicalPath() + File.separator + entry.getName());
if (newFile.exists() && !overwrite)
{
if (verbose) System.out.println(" File " + newFile .getCanonicalPath() + " exists, skipped.");
continue;
}
try
{
BufferedImage theImage = loadImage(entry);
theImage = getScaledInstance(theImage, 50, 50, RenderingHints.VALUE_INTERPOLATION_BILINEAR, true);
if (!recreate)
{
saveImage(theImage, destination.getCanonicalPath() + "\\" + entry.getName());
}
else
{
if (!newFile.exists())
{
newFile.createNewFile();
}
saveImage(theImage, newFile.getCanonicalPath());
}
}
catch (Exception e)
{
System.out.println("Error resizing image " + entry.getCanonicalPath());
e.printStackTrace();
}
}
if (recurse && entry.isDirectory())
{
if (recreate)
{
File newFile = new File(destination.getCanonicalPath() + File.separator + entry.getName());
if (!newFile.exists())
{
newFile.mkdir();
}
changeFiles(entry, new File(destination.getCanonicalPath() + File.separator + entry.getName()), filter, recurse, recreate, overwrite);
}
else
{
changeFiles(entry, new File(destination.getCanonicalPath()), filter, recurse, recreate, overwrite);
}
}
}
}
public static class MyFilenameFilter implements FilenameFilter
{
public boolean accept(File dir,
String name)
{
return name.endsWith(".jpg");
}
}
public static void main(String args[])
throws IOException
{
boolean overwrite = false;
boolean recursive = false;
boolean recreate = false;
boolean nextIsDestination = false;
boolean nextIsSource = false;
String destination = DESTINATION_PATH;
String source = SOURCE_PATH;
for (String argument : args)
{
if (nextIsDestination)
{
destination = argument;
nextIsDestination = false;
}
if (nextIsSource)
{
source = argument;
nextIsSource = false;
}
if ("-r".equals(argument))
{
recursive = true;
}
if ("-tree".equals(argument))
{
recreate = true;
}
if ("-s".equals(argument))
{
nextIsSource = true;
}
if ("-d".equals(argument))
{
nextIsDestination = true;
}
if ("-o".equals(argument))
{
overwrite = true;
}
if ("-v".equals(argument))
{
verbose = true;
}
}
File src_file = new File(source);
File dest_file = new File(destination);
System.out.println("absolute path: " + src_file.getAbsolutePath());
System.out.println("canonical path: " + src_file.getCanonicalPath());
System.out.println("path: " + src_file.getPath());
System.out.println("name: " + src_file.getName());
changeFiles(src_file, dest_file, new MyFilenameFilter(), recursive, recreate, overwrite);
}
}