如何使用 Java 调整图像大小?

发布于 2024-09-29 03:54:17 字数 381 浏览 7 评论 0原文

我有一堆 48x48 图像,需要 16x16 版本,我不想存储 16x16 版本,而是想动态调整它们的大小。我当前的代码如下所示(model.icon() 返回 48x48 图像):

Icon icon = model.icon();
Image image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
return new ImageIcon(image.getScaledInstance(16, 16, Image.SCALE_AREA_AVERAGING));

不幸的是,当运行此代码时,我得到一个 16x16 黑色方块而不是图像。

I have a bunch of 48x48 images that I need 16x16 versions of, and instead of storing the 16x16 versions, I want to resize them on the fly. My current code looks like this (model.icon() returns the 48x48 image):

Icon icon = model.icon();
Image image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
return new ImageIcon(image.getScaledInstance(16, 16, Image.SCALE_AREA_AVERAGING));

Unfortunately, when this code is run, I get a 16x16 black square instead of the image.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

靖瑶 2024-10-06 03:54:17

试试这个。

ImageIcon icon = model.icon();
Image image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(icon.getImage(), 0, 0, 16, 16, null);
return new ImageIcon(image);

Try this.

ImageIcon icon = model.icon();
Image image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(icon.getImage(), 0, 0, 16, 16, null);
return new ImageIcon(image);
昔梦 2024-10-06 03:54:17

您需要的不仅仅是图标参考的更多信息。您需要访问实际图像。您的新图像是一个黑色方块,因为您从未设置图像的源(即您创建一个新的黑色图像,然后缩放空图像)。

You need more information than just the Icon reference. You need access to the actual image. You're new image is a black square because you never set the source if the image (i.e. you create a new black image and then scale the empty image).

独闯女儿国 2024-10-06 03:54:17

您没有将图标放入图像中。如果图标是一个ImageIcon,那么你可以这样做:

..
Graphics2D g2 = image.createGraphics();
g2.drawImage(icon.getImage(), 0, 0, 16, 16, null);
g2.dispose();
return new ImageIcon(image);

You are not putting the Icon into the Image. If icon is an ImageIcon, then you can do:

..
Graphics2D g2 = image.createGraphics();
g2.drawImage(icon.getImage(), 0, 0, 16, 16, null);
g2.dispose();
return new ImageIcon(image);
じее 2024-10-06 03:54:17

您也可以使用此方法来捕获图像

 public static void resize(final String inputImagePath, final String outputImagePath, final int scaledWidth, final int scaledHeight) throws IOException {
    final File inputFile = new File(inputImagePath);
    final BufferedImage inputImage = ImageIO.read(inputFile);

    final BufferedImage outputImage = new BufferedImage(scaledWidth, scaledHeight, inputImage.getType());

    final Graphics2D g2d = outputImage.createGraphics();
    g2d.drawImage(inputImage, 0, 0, scaledWidth, scaledHeight, null);
    g2d.dispose();

    final String formatName = outputImagePath.substring(outputImagePath.lastIndexOf(".") + 1);

    ImageIO.write(outputImage, formatName, new File(outputImagePath));
}

You can use this method also for seizing images

 public static void resize(final String inputImagePath, final String outputImagePath, final int scaledWidth, final int scaledHeight) throws IOException {
    final File inputFile = new File(inputImagePath);
    final BufferedImage inputImage = ImageIO.read(inputFile);

    final BufferedImage outputImage = new BufferedImage(scaledWidth, scaledHeight, inputImage.getType());

    final Graphics2D g2d = outputImage.createGraphics();
    g2d.drawImage(inputImage, 0, 0, scaledWidth, scaledHeight, null);
    g2d.dispose();

    final String formatName = outputImagePath.substring(outputImagePath.lastIndexOf(".") + 1);

    ImageIO.write(outputImage, formatName, new File(outputImagePath));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文