【问题标题】:Converting an ImageIcon to a BufferedImage将 ImageIcon 转换为 BufferedImage
【发布时间】:2013-02-09 18:49:59
【问题描述】:

我一直在尝试将 ImageIcon 转换为 BufferedImage...但我没有运气。

我有一个预先存在的 ImageIcon,需要将其转换为 Buffered Image 以用于现有的大量 BufferedImage 操作。

我找到了一些方法,但它们都占用大量 CPU。

【问题讨论】:

    标签: java bufferedimage imageicon


    【解决方案1】:

    有什么问题:

    BufferedImage bi = new BufferedImage(
        icon.getIconWidth(),
        icon.getIconHeight(),
        BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.createGraphics();
    // paint the Icon to the BufferedImage.
    icon.paintIcon(null, g, 0,0);
    g.dispose();
    

    【讨论】:

    • 非常有帮助,非常感谢,我以前遇到过这种方法,但由于我的直接假设它会表现不佳,所以很快就将其击落,但在尝试了你的方式之后,一切似乎工作正常。
    • BufferedImage.TYPE_INT_ARGB 如果图标中有透明像素。
    【解决方案2】:

    ImageIconImageBufferedImage

    ImageIcon yourImage;
    Image image = yourImage.getImage();
    BufferedImage buffered = (BufferedImage) image;
    

    【讨论】:

    • 如果 ImageIcon 包含 BufferedImage,则使用此选项,否则使用 Werner 的答案。
    • 这适用于 Windows 但不适用于 Linux(在 Ubuntu 上试过)。
    【解决方案3】:

    我尝试了一个叫做 Scalr 的东西,查看下面的代码

    Scalr.resize((BufferedImage) ImageIO.read(file), Method.SPEED, 250, OP_ANTIALIAS, OP_BRIGHTER);

    干杯。

    【讨论】:

    • 不回答 OP 的问题。您只是在读取文件,而不是将 ImageIcon 对象转换为 BufferedImage 对象
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-01
    相关资源
    最近更新 更多