import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
/**
* Utility class to resize images
*/
public class ImageResizer
{
private static final Log log = LogFactory.getLog(ImageResizer.class);
private static class ImageContainer {
public BufferedImage image;
public String format;
}
/**
* Reads an image stream from from and writes a PNG version
* to the file to to.
*
* @param from where to read the file from
* @param to where wo write the file to.
* @param pixelWidth the maximum number of pixels wide in the output image
*/
public static void resizeToPNG(File from, File to, int pixelWidth)
{
log.info("writing resized file to " + to);
ImageContainer ic = readImage(from);
if (ic.image.getWidth() <= pixelWidth) {
if ("png".equals(ic.format)) {
try {
FileUtils.copyFile(from, to);
return;
} catch (IOException e) {
throw new Error(e);
}
}
writeImage(ic.image, to);
return;
}
writeImage(resize(ic.image, pixelWidth), to);
}
private static BufferedImage resize(BufferedImage image, int pixelWidth)
{
double scale = pixelWidth / (double)image.getWidth();
BufferedImage target = new BufferedImage(pixelWidth,
(int)(image.getHeight() * scale), BufferedImage.TYPE_INT_RGB);
Image i= image.getScaledInstance(pixelWidth,
(int)(image.getHeight() * scale), Image.SCALE_SMOOTH);
Graphics g = target.getGraphics();
g.drawImage(i,0,0,null);
g.dispose();
return target;
}
public static BufferedImage sunResize(BufferedImage image, int pixelWidth)
{
double scale = pixelWidth / (double)image.getWidth();
return createResizedCopy(image, pixelWidth,
(int)(image.getHeight() * scale), false);
}
private static BufferedImage createResizedCopy(Image originalImage,
int scaledWidth, int scaledHeight,
boolean preserveAlpha)
{
int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, imageType);
Graphics2D g = scaledBI.createGraphics();
if (preserveAlpha) {
g.setComposite(AlphaComposite.Src);
}
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null);
g.dispose();
return scaledBI;
}
private static void writeImage(BufferedImage image, File to)
{
try {
ImageIO.write(image, "png", to);
} catch (IOException e) {
throw new Error(e);
}
}
private static ImageContainer readImage(File from)
{
try {
ImageInputStream iis = ImageIO.createImageInputStream(from);
Iterator i = ImageIO.getImageReaders(iis);
if (!i.hasNext()) {
throw new Error("Don't know how to read file "+ from);
}
ImageReader ir = (ImageReader)i.next();
ir.setInput(iis);
ImageContainer ic = new ImageContainer();
ic.image = ir.read(0);
ic.format = ir.getFormatName();
return ic;
} catch (IOException e) {
throw new Error(e);
}
}
}