Skip to content

Instantly share code, notes, and snippets.

@kirkbackus
Created January 27, 2016 20:57
Show Gist options
  • Save kirkbackus/2ea6ab20cc57bae3e890 to your computer and use it in GitHub Desktop.
Save kirkbackus/2ea6ab20cc57bae3e890 to your computer and use it in GitHub Desktop.
String fileType = StringUtils.substringAfterLast(file.getOriginalFilename(), ".");
ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.70f);
ByteArrayOutputStream os = new ByteArrayOutputStream();
writer.setOutput(os);
writer.write(null, new IIOImage(bufferedImage, null, null), param);
// ImageIO.write(bufferedImage, fileType, os);
byte[] imageBytes = os.toByteArray();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment