Resizer.java

package ac.essex.ooechs.imaging.commons; 
 
import java.io.File; 
import java.io.BufferedOutputStream; 
import java.io.FileOutputStream; 
import java.io.FileInputStream; 
import java.awt.image.renderable.ParameterBlock; 
import java.nio.channels.FileChannel; 
 
/* 
 * Thumbnail.java (requires Java 1.2+) 
 */ 
import com.sun.media.jai.codec.JPEGEncodeParam; 
import com.sun.media.jai.codecimpl.JPEGImageEncoder; 
import javax.media.jai.Interpolation; 
import javax.media.jai.JAI; 
import javax.media.jai.KernelJAI; 
 
public class Resizer { 
 
    public static void resize(File file, int width) throws Exception { 
        resize(file.getAbsolutePath(), width, -1); 
    } 
 
    public static void resize(File in, File out,  int width) throws Exception { 
        produceThumbnail(in.getAbsolutePath(), out.getAbsolutePath(), width, -1); 
    }     
 
    public static void resize(String inFile, int width, int height) throws Exception { 
        produceThumbnail(inFile, inFile, width, height); 
    } 
 
    public static void produceThumbnail(String inFile, String outFile, int width, int height) throws Exception { 
        javax.media.jai.PlanarImage image; 
        // Load your JPEG image here 
        image = javax.media.jai.JAI.create("fileload", inFile); 
        float xScale = (float) width / (float) image.getWidth(); 
        float yScale = xScale; //(float) imageHeight / (float) image.getHeight(); 
        //System.out.println("XScale: " + xScale); 
        // decide how much to soften before resizing 
        // on a scale of 1 - 5 
        int softenAmount = 1; // no softening 
        if (xScale > 0.4 && xScale < 0.6) { 
            softenAmount = 2; 
        } 
        if (xScale > 0.2 && xScale <= 0.4) { 
            softenAmount = 3; 
        } 
        if (xScale <= 0.2) { 
            softenAmount = 5; 
        } 
        float[] softenKernel = new float[softenAmount * softenAmount]; 
        for (int i = 0; i < softenKernel.length; i++) { 
            softenKernel[i] = 1.0f  / softenKernel.length; 
        } 
        // Create the kernel. 
        KernelJAI kernel = new KernelJAI(softenAmount, softenAmount, softenKernel); 
        // Create the convolve operation. 
        image = JAI.create("convolve", image, kernel); 
        // Scales the original image 
        ParameterBlock pb = new ParameterBlock(); 
        pb.addSource(image); 
        pb.add(xScale); 
        pb.add(yScale); 
        pb.add(0.0F); 
        pb.add(0.0F); 
        pb.add(Interpolation.getInstance(Interpolation.INTERP_BICUBIC)); 
        // Creates a new, scaled image and uses it on the DisplayJAI component 
        image = JAI.create("scale", pb); 
        JPEGEncodeParam encodeParam = new JPEGEncodeParam(); 
        encodeParam.setHorizontalSubsampling(0, 1); 
        encodeParam.setHorizontalSubsampling(1, 2); 
        encodeParam.setHorizontalSubsampling(2, 2); 
        encodeParam.setVerticalSubsampling(0, 1); 
        encodeParam.setVerticalSubsampling(1, 1); 
        encodeParam.setVerticalSubsampling(2, 1); 
        encodeParam.setRestartInterval(64); 
        encodeParam.setQuality(0.75f); 
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFile)); 
        JPEGImageEncoder encoder = new JPEGImageEncoder(out, encodeParam); 
        encoder.setParam(encodeParam); 
        encoder.encode(image); 
    } 
    public static void copy(File from, File to) throws Exception { 
        FileChannel sourceChannel = new FileInputStream(from).getChannel(); 
        FileChannel destinationChannel = new FileOutputStream(to).getChannel(); 
        sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel); 
        sourceChannel.close(); 
        destinationChannel.close(); 
    } 
    public static void copy(String from, String to) throws Exception { 
        copy(new File(from), new File(to)); 
    } 
    public static void copyDirectoryTo(String directoryFrom, String intoDirectory) throws Exception { 
        File to = new File(intoDirectory); 
        File from = new File(directoryFrom); 
        copyDirectoryTo(from, to); 
    } 
    /** 
     * Copies the contents of one directory into another. Used when the template web-app is copied 
     * into the new webapp. 
     * @throws Exception 
     */ 
    public static void copyDirectoryTo(File from, File to) throws Exception { 
        if (!from.exists()) { 
            throw new Exception("Cannot copy - the source directory does not exist"); 
        } 
        if (!from.isDirectory()) { 
            throw new Exception("Cannot copy - the source is not a directory"); 
        } 
        if (!to.exists()) { 
            System.out.println("Creating directory: " + to.getName()); 
            to.mkdir(); 
        } 
        if (!to.isDirectory()) { 
            throw new Exception("Cannot copy - the destination is not a directory"); 
        } 
        // get the files in this directory 
        File[] files = from.listFiles(); 
        for (int i = 0; i < files.length; i++) { 
            File file = files[i]; 
            if (file.isDirectory()) { 
                // create a new directory in the destination 
                File newDir = new File(to, file.getName()); 
                newDir.mkdir(); 
                System.out.println("Creating directory: " + file.getName()); 
                // copy contents of this directory 
                copyDirectoryTo(file, newDir); 
            } else { 
                // copy each file individually 
                System.out.println("Copying file: " + file.getName() + " to: " + to.getName()); 
                copy(file, new File(to, file.getName())); 
            } 
        } 
    } 
}