001 package com.khubla.pragmatach.plugin.responsive;
002
003 import java.awt.Graphics;
004 import java.awt.Image;
005 import java.awt.image.BufferedImage;
006 import java.io.InputStream;
007 import java.io.OutputStream;
008
009 import javax.imageio.ImageIO;
010
011 import com.khubla.pragmatach.framework.api.PragmatachException;
012
013 /**
014 * @author tome
015 */
016 public class ImageResizer {
017 public static void resize(InputStream inputImage, OutputStream outputImage, double xscale, double yscale) throws PragmatachException {
018 try {
019 final BufferedImage bufferedImage = ImageIO.read(inputImage);
020 final int width = (int) Math.round(bufferedImage.getWidth() * xscale);
021 final int height = (int) Math.round(bufferedImage.getHeight() * yscale);
022 final Image scaledImage = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
023 final BufferedImage imageBuff = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
024 final Graphics g = imageBuff.createGraphics();
025 g.drawImage(scaledImage, 0, 0, null, null);
026 g.dispose();
027 ImageIO.write(imageBuff, "png", outputImage);
028 } catch (final Exception e) {
029 throw new PragmatachException("Exception in resize", e);
030 }
031 }
032
033 public static void resize(InputStream inputImage, OutputStream outputImage, int width, int height) throws PragmatachException {
034 try {
035 final BufferedImage bufferedImage = ImageIO.read(inputImage);
036 final Image scaledImage = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
037 final BufferedImage imageBuff = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
038 final Graphics g = imageBuff.createGraphics();
039 g.drawImage(scaledImage, 0, 0, null, null);
040 g.dispose();
041 ImageIO.write(imageBuff, "png", outputImage);
042 } catch (final Exception e) {
043 throw new PragmatachException("Exception in resize", e);
044 }
045 }
046 }