import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.RenderingHints.Key;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.fileupload.util.Streams;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class TranscodeDemo {
	private static int widht = 400;
	private static int height = 400;
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static final Map<Key, Object> RENDERING_HINTS = new HashMap<Key, Object>();
    public static void init(){
        RENDERING_HINTS.put(RenderingHints.KEY_COLOR_RENDERING,
                RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        RENDERING_HINTS.put(RenderingHints.KEY_ALPHA_INTERPOLATION,
                RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        RENDERING_HINTS.put(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        RENDERING_HINTS.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        RENDERING_HINTS.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    }
	public static void main(String[] args) throws IOException {
		init();
		byte[] imageBytes = null;
		File file = new File("c:/sample/fff.jpg");
		imageBytes = readStreamIntoMemory(new FileInputStream(file));
		BufferedImage img = javax.imageio.ImageIO.read(new ByteArrayInputStream(imageBytes));
		img = scaledImage(img, widht, height, true, RENDERING_HINTS);
		FileOutputStream fos = new FileOutputStream("c:/sample/fff_1.jpg");
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
		encoder.encode(img);
		fos.close();
	}
	public static byte[] readStreamIntoMemory(InputStream in) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Streams.copy(in, baos, true);
        return baos.toByteArray();
    }
	public static BufferedImage scaledImage(BufferedImage img, int targetWidth, int targetHeight, boolean higherQuality, Map renderingHints){
		int width = img.getWidth();
        int height = img.getHeight();
        if (width != height) {
            if (width > height) {
                targetHeight = (int) (((double) targetWidth * (double) height) / (double) width);
            } else {
                targetWidth = (int) (((double) targetHeight * (double) width) / (double) height);
            }
        }
        int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
                : BufferedImage.TYPE_INT_ARGB;
        BufferedImage scaledImg = (BufferedImage) img;
        int w, h;
        if (higherQuality) {
            w = img.getWidth();
            h = img.getHeight();
        } else {
            w = targetWidth;
            h = targetHeight;
        }
        do {
            if (w < targetWidth && h < targetHeight) {
                break;
            }
            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.setRenderingHints(renderingHints);
            g2.drawImage(scaledImg, 0, 0, w, h, null);
            g2.dispose();
            scaledImg = tmp;
        } while (w != targetWidth || h != targetHeight);
        return scaledImg;
	}
}