Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Werbth Rocha werbth

🏠
Working from home
View GitHub Profile
@werbth
werbth / ResizeImage.java
Last active Jul 17, 2020
Given a jpeg image as InputStream, resize it and return another InputStream.
View ResizeImage.java
public static InputStream resizeImage(InputStream inputStream, int width, int height) 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, "jpeg", baos);
return new ByteArrayInputStream(baos.toByteArray());
You can’t perform that action at this time.