3

I am currently scaling images using the following code.

Image scaledImage = img.getScaledInstance( width, int height, Image.SCALE_SMOOTH);
BufferedImage imageBuff = new BufferedImage(width, scaledImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics g = imageBuff.createGraphics();
g.drawImage(scaledImage, 0, 0, new Color(0, 0, 0), null);
g.dispose();
ImageIO.write(imageBuff, "jpg", newFile);

Anyone have an idea of a better way of scaling an image and getting better quality results, or even any help on improving my current code to get better quality output.

Maroun
  • 94,125
  • 30
  • 188
  • 241
JCS
  • 897
  • 4
  • 20
  • 43
  • You could look at the `SCALE_*` fields on http://docs.oracle.com/javase/7/docs/api/java/awt/Image.html – Kninnug Apr 12 '13 at 15:48
  • I think it will be helpful http://ebhor.com/high-quality-thumbnail-generation-in-java/ – xrcwrn Mar 13 '14 at 08:08
  • I've answered you question here in detail: http://stackoverflow.com/questions/3967731/how-to-improve-the-performance-of-g-drawimage-method-for-resizing-images/32266733#32266733 – nemo Oct 24 '15 at 10:17

3 Answers3

3

You can use Affine Transorm

public static BufferedImage getScaledImage(BufferedImage image, int width, int height) throws IOException {
    int imageWidth  = image.getWidth();
    int imageHeight = image.getHeight();

    double scaleX = (double)width/imageWidth;
    double scaleY = (double)height/imageHeight;
    AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
    AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);

    return bilinearScaleOp.filter(
        image,
        new BufferedImage(width, height, image.getType()));
}

Also try this Example .

Also Try java-image-scaling library

Alya'a Gamal
  • 5,624
  • 19
  • 34
2

You might want to look at this image scaling library. It has algorithms like bicubic and Lanczos and also an unsharp filter.

Eelke
  • 20,897
  • 4
  • 50
  • 76
1

Try avoiding Image.getScaledInstance().

Stefan
  • 12,108
  • 5
  • 47
  • 66