Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: Line appears when using AffineTransform to scale image
    text
    copied!<p>I'm having a problem with image scaling. When I use the following code to scale an image it ends up with a line either at the bottom or on the right side of the image.</p> <pre><code>double scale = 1; if (scaleHeight &gt;= scaleWidth) { scale = scaleWidth; } else { scale = scaleHeight; } AffineTransform af = new AffineTransform(); af.scale(scale, scale); AffineTransformOp operation = new AffineTransformOp(af, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); BufferedImage bufferedThumb = operation.filter(img, null); </code></pre> <p>The original image </p> <p><img src="https://i.stack.imgur.com/LAEuk.gif" alt="enter image description here"> </p> <p>The scaled image</p> <p><img src="https://i.stack.imgur.com/3JIie.gif" alt="enter image description here"></p> <p>Does anyone know why the line appears?</p> <p>Thanks!</p> <p>EDIT:</p> <p>Added the complete method code:</p> <pre><code>public static final int SPINNER_MAX_WIDTH = 105; public static final int SPINNER_MAX_HEIGHT = 70; public void scaleImage(BufferedImage img, int maxWidth, int maxHeight, String fileName) { double scaleWidth = 1; double scaleHeight = 1; if (maxHeight != NOT_SET) { if (img.getHeight() &gt; maxHeight) { scaleHeight = (double) maxHeight / (double) img.getHeight(); } } if (maxWidth != NOT_SET) { if (img.getWidth() &gt; maxWidth) { scaleWidth = (double) maxWidth / (double) img.getWidth(); } } double scale = 1; if (scaleHeight &gt;= scaleWidth) { scale = scaleWidth; } else { scale = scaleHeight; } AffineTransform af = new AffineTransform(); af.scale(scale, scale); AffineTransformOp operation = new AffineTransformOp(af, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); BufferedImage bufferedThumb = operation.filter(img, null); if (bufferedThumb != null) { File imageFile = new File(fileName); String fileType = fileName.substring(fileName.lastIndexOf(".") + 1); try { ImageIO.write(bufferedThumb, fileType, imageFile); } catch (IOException e) { logger.error("Failed to save scaled image: " + fileName + "\n" + e.getMessage()); } } } </code></pre> <p>The maxWidth and maxHeight parameters in the method call is set to the SPINNER_MAX_* constants.</p> <p>Thanks!</p>
 

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