This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// formatName: the image format name (jpeg, png) | |
public static InputStream resizeImage(InputStream inputStream, int width, int height, String formatName) throws IOException { | |
BufferedImage sourceImage = ImageIO.read(inputStream); | |
Image thumbnail = sourceImage.getScaledInstance(width, height, Image.SCALE_SMOOTH); | |
BufferedImage bufferedThumbnail = new BufferedImage(thumbnail.getWidth(null), | |
thumbnail.getHeight(null), | |
BufferedImage.TYPE_INT_RGB); | |
bufferedThumbnail.getGraphics().drawImage(thumbnail, 0, 0, null); | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
ImageIO.write(bufferedThumbnail, formatName, baos); |