Skip to content

Instantly share code, notes, and snippets.

Avatar
💾

Computeiful

💾
  • London
View GitHub Profile
@Computeiful
Computeiful / JPEG.java
Created Jul 1, 2020
Compress a BufferedImage to JPEG in memory
View JPEG.java
public static BufferedImage comp(BufferedImage image, float comp) throws IOException {
ImageWriter jpgWriter = ImageIO.getImageWritersByFormatName("jpg").next();
ImageWriteParam jpgWriteParam = jpgWriter.getDefaultWriteParam();
jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpgWriteParam.setCompressionQuality(comp);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageOutputStream stream = ImageIO.createImageOutputStream(outputStream);
jpgWriter.setOutput(stream);
jpgWriter.write(null, new IIOImage(image, null, null), jpgWriteParam);
You can’t perform that action at this time.