Skip to content

Instantly share code, notes, and snippets.

@worthlesscog
Created July 1, 2012 08:25
Show Gist options
  • Save worthlesscog/3027556 to your computer and use it in GitHub Desktop.
Save worthlesscog/3027556 to your computer and use it in GitHub Desktop.
Image compress and save
BufferedImage scaledImage = scale(sourceImage, width, height, SMOOTHING_STEPS);
ByteArrayOutputStream buffer = new ByteArrayOutputStream(1024 * 1024);
ImageOutputStream stream = ImageIO.createImageOutputStream(new BufferedOutputStream(buffer));
float percentage = quality;
do {
buffer.reset();
buildImage(scaledImage, width, height, imageType, stream, percentage);
quality = percentage;
percentage -= 0.005f;
} while (buffer.size() > maxSize);
buffer.writeTo(new FileOutputStream(target));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment