当前位置: 首页 > 面试题库 >

如何在Java中2d图形中调整缓冲图像的大小?

柳镜
2023-03-14
问题内容
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);

嗨,我有大尺寸的缓冲…我必须调整缓冲图像的大小… PLZ你能帮助我吗?


问题答案:
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;
    }
};


 类似资料:
  • 问题内容: 我正在尝试调整缓冲图像的大小。我能够存储它并显示在jframe上没有问题,但是我似乎无法调整它的大小。关于如何更改它以使其正常运行并将图像显示为200 * 200文件的任何提示都很好 问题答案: 更新的答案 我不知道为什么我的原始答案有效,但是在单独的环境中对其进行了测试,我同意,原始的可接受答案不起作用(为什么我说过,我也不记得了)。另一方面,这确实起作用:

  • Photoshop 中的“图像大小”命令包含一种可以保留细节,并在放大图像时提供更优锐度的方法。未裁剪的原始图像(左图);调整大小后的锐化图像(右图) 此外,还更新了 Photoshop 的图像大小对话框以方便使用: 通过一个窗口显示调整参数的预览图像。 调整对话框的大小也将调整预览窗口的大小。 可从对话框右上角的齿轮菜单内启用和禁用“缩放样式”选项。 从“尺寸”弹出菜单中,选取其他度量单位显示最

  • 我有一个尺寸为800x800的图像,其大小为170 kb。我想将此图像调整为600x600。调整大小后,我希望缩小图像大小。我该怎么做?

  • 我已经用Java编写了一个图像旋转的方法(允许旋转90度、180度和270度),但它似乎没有正常工作。很明显,我做错了什么,但我完全不知道是什么。输出的问题是图像确实是旋转的,但图像中有黑色部分,就像图像不在正确的位置一样。 我的第一次尝试是在不使用作为目标的结果变量的情况下执行此操作,而是执行以下操作: 旋转很好,图像中没有黑色部分,但颜色很奇怪,就像有些颜色改变了,皮肤颜色变红了一样。所以当我

  • 问题内容: 在Java中,如何将图像的大小调整为任何类型或大小的默认大小? 问题答案: 网上有很多关于如何执行此操作的文章,类似的内容应该可以帮助您: 最终的Java图像处理(包括 许多 其他图像功能)

  • 问题内容: 您好,我有一个QR码图像,我想调整它的大小,当我尝试使用此代码将其调整为小图像时,我总是得到模糊的图像,并且扫描时QR码不再有效,但是当我使用相同的代码将大小调整为大尺寸图像时,它可以正常工作: 有什么问题,我不清楚,请至少给我一个提示,谢谢。 问题答案: 我使用仿射变换来完成此任务,这是我的代码,希望对您有所帮助