Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You don't need a full blown Image processing library for simply resizing an image.</p> <p>The recommended approach is by using <strong>progressive bilinear scaling</strong>, like so (feel free to use this method as is in your code):</p> <pre><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 &gt; targetWidth) { w /= 2; w = (w &lt; targetWidth) ? targetWidth : w; } if (h &gt; targetHeight) { h /= 2; h = (h &lt; 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; } </code></pre> <p>Code modified and cleaned from the original at <a href="http://filthyrichclients.org" rel="noreferrer">Filthy Rich Clients</a>.</p> <hr> <p>Based on your comment, you can reduce quality and encode JPEG bytes like so:</p> <p><code>image</code> is the BufferedImage. </p> <pre><code>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(); </code></pre> <p>Now, since <code>os</code> is a <code>ByteArrayOutputStream</code>, you can Base64 encode it.</p> <pre><code>String base64 = Base64.encode(os.toByteArray()); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload