Java图像缩放提高质量?

标签 java image scaling

我目前正在使用以下代码缩放图像。

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);

任何人都知道缩放图像并获得更好质量结果的更好方法,或者甚至可以帮助改进我当前的代码以获得更好质量的输出。

最佳答案

您可以使用 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()));
}

也试试这个 Example .

也试试 java-image-scaling图书馆

关于Java图像缩放提高质量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15975610/

相关文章:

JAVA基于文本的迷宫游戏

java - 将图像加载到数组中

html - CSS 背景图像与选择器的高度/宽度不匹配?

css - 使用 rem 单位时出现 Chrome 缩放问题

java 对象复制,数组索引越界

java - GetExtra PutExtra

java - 使图像文件夹在项目中可用

python - 在 Google App Engine 上获取 DISTINCT 用户

java - Fitviewport 无法正常工作

java - 如何使用 iText 库获取 pdf 文件的给定段落内容?