java - 如何在 java 中调整 bufferedimage n graphics2d 的大小?

标签 java image bufferedimage

int width = 175;
Graphics2D gb = (Graphics2D) g;

        bufferedimage = (BufferedImage) createImage(width, width);

        Graphics2D graphics = bufferedimage.createGraphics();
        graphics.setColor(/*this.getBackground()*/Color.red);
        graphics.fillRect(0, 0, width, width);

嗨,我有大尺寸的缓冲...我必须调整缓冲图像的大小...请问有人能帮我吗?

最佳答案

public BufferedImage scaleImage(BufferedImage img, int width, int height,
        Color background) {
    int imgWidth = img.getWidth();
    int imgHeight = img.getHeight();
    if (imgWidth*height < imgHeight*width) {
        width = imgWidth*height/imgHeight;
    } else {
        height = imgHeight*width/imgWidth;
    }
    BufferedImage newImage = new BufferedImage(width, height,
            BufferedImage.TYPE_INT_RGB);
    Graphics2D g = newImage.createGraphics();
    try {
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g.setBackground(background);
        g.clearRect(0, 0, width, height);
        g.drawImage(img, 0, 0, width, height, null);
    } finally {
        g.dispose();
    }
    return newImage;
}

更新:不同的算法

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

public enum Resizer {
    NEAREST_NEIGHBOR {
        @Override
        public BufferedImage resize(BufferedImage source,
                int width, int height) {
            return commonResize(source, width, height,
                    RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
        }
    },
    BILINEAR {
        @Override
        public BufferedImage resize(BufferedImage source,
                int width, int height) {
            return commonResize(source, width, height,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        }
    },
    BICUBIC {
        @Override
        public BufferedImage resize(BufferedImage source,
                int width, int height) {
            return commonResize(source, width, height,
                    RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        }
    },
    PROGRESSIVE_BILINEAR {
        @Override
        public BufferedImage resize(BufferedImage source,
                int width, int height) {
            return progressiveResize(source, width, height,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        }
    },
    PROGRESSIVE_BICUBIC {
        @Override
        public BufferedImage resize(BufferedImage source,
                int width, int height) {
            return progressiveResize(source, width, height,
                    RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        }
    },
    AVERAGE {
        @Override
        public BufferedImage resize(BufferedImage source,
                int width, int height) {
            Image img2 = source.getScaledInstance(width, height,
                    Image.SCALE_AREA_AVERAGING);
            BufferedImage img = new BufferedImage(width, height,
                    source.getType());
            Graphics2D g = img.createGraphics();
            try {
                g.drawImage(img2, 0, 0, width, height, null);
            } finally {
                g.dispose();
            }
            return img;
        }
    };

    public abstract BufferedImage resize(BufferedImage source,
            int width, int height);

    private static BufferedImage progressiveResize(BufferedImage source,
            int width, int height, Object hint) {
        int w = Math.max(source.getWidth()/2, width);
        int h = Math.max(source.getHeight()/2, height);
        BufferedImage img = commonResize(source, w, h, hint);
        while (w != width || h != height) {
            BufferedImage prev = img;
            w = Math.max(w/2, width);
            h = Math.max(h/2, height);
            img = commonResize(prev, w, h, hint);
            prev.flush();
        }
        return img;
    }

    private static BufferedImage commonResize(BufferedImage source,
            int width, int height, Object hint) {
        BufferedImage img = new BufferedImage(width, height,
                source.getType());
        Graphics2D g = img.createGraphics();
        try {
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
            g.drawImage(source, 0, 0, width, height, null);
        } finally {
            g.dispose();
        }
        return img;
    }
};

关于java - 如何在 java 中调整 bufferedimage n graphics2d 的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4756268/

相关文章:

java - 最小化 Java 中的垃圾收集尝试(示例)

java - 设置主框架不可调整大小

java - 用 2 个字符串的字符填充二维数组

java - 在数组中存储最低有效位?

java - 如何绘制带有色调的 BufferedImage

java - 构造函数 Intent(DataCountUtilities, Class<DataCountService>) 未定义

javascript - 浏览器缓存图像防止淡入

javascript - 确定图像 src 是否已更改

c# - 16 位灰度图像到热图

java - 如何加载 BufferedImage 作为资源以及将其放置在哪里?