You do not need a fully functional image processing library to easily resize an image.
The recommended approach is to use progressive bilinear scaling , for example (feel free to use this method, as in your code):
public BufferedImage scale(BufferedImage img, int targetWidth, int targetHeight) { int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; BufferedImage ret = img; BufferedImage scratchImage = null; Graphics2D g2 = null; int w = img.getWidth(); int h = img.getHeight(); int prevW = w; int prevH = h; do { if (w > targetWidth) { w /= 2; w = (w < targetWidth) ? targetWidth : w; } if (h > targetHeight) { h /= 2; h = (h < targetHeight) ? targetHeight : h; } if (scratchImage == null) { scratchImage = new BufferedImage(w, h, type); g2 = scratchImage.createGraphics(); } g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(ret, 0, 0, w, h, 0, 0, prevW, prevH, null); prevW = w; prevH = h; ret = scratchImage; } while (w != targetWidth || h != targetHeight); if (g2 != null) { g2.dispose(); } if (targetWidth != ret.getWidth() || targetHeight != ret.getHeight()) { scratchImage = new BufferedImage(targetWidth, targetHeight, type); g2 = scratchImage.createGraphics(); g2.drawImage(ret, 0, 0, null); g2.dispose(); ret = scratchImage; } return ret; }
The code is changed and cleared from the original in Filthy Rich Clients .
Based on your comment, you can reduce the quality and encode JPEG bytes like this:
image - BufferedImage.
ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageWriter writer = (ImageWriter) ImageIO.getImageWritersByFormatName("jpeg").next(); ImageWriteParam param = writer.getDefaultWriteParam(); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(0.2f); // Change this, float between 0.0 and 1.0 writer.setOutput(ImageIO.createImageOutputStream(os)); writer.write(null, new IIOImage(image, null, null), param); writer.dispose();
Now, since os is a ByteArrayOutputStream , you can encode it with Base64.
String base64 = Base64.encode(os.toByteArray());