Search code examples
javagraphicsimage-manipulationimage-scalingjava-2d

Resized image degrades in quality


I resized an image using Java2D Graphics class. But it doesn't look right.

    BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
    g.dispose();

Is it possible to scale an image without introducing artifacts?


Solution

  • Bitmap graphics do not scale well, generally speaking. Degradation is particularly notable when you increase the size of the image, but even scaling down can introduce undesirable artifacts especially if not scaling by integral factors.

    The best solution, if you need multiple sizes of a single image for display, is to either use vector* graphics or take the highest fidelity bitmap you have and scale down, and by integral factors.

    *Note that vector graphics aren't an option for photographs and the like.