BufferedImage newImage = new BufferedImage(targetWidth, targetHeight, image.getType());
newImage.createGraphics().drawImage(image, 0, 0, targetWidth, targetHeight, null);
try {
ImageIO.write(newImage, "JPG", os);
} catch (IOException e) {
throw new ImageConverterException("Cannot output the converted image. IO error: " + e.getMessage(), e);
}
}