Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java image scaling improve quality?

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.

like image 550
JCS Avatar asked Sep 12 '26 11:09

JCS


2 Answers

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

like image 185
Alya'a Gamal Avatar answered Sep 15 '26 01:09

Alya'a Gamal


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

like image 42
Eelke Avatar answered Sep 15 '26 01:09

Eelke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!