Java:我收到一个类转换异常错误,指向我什至没有导入的类

发布于 2024-10-27 05:53:13 字数 1333 浏览 3 评论 0原文

我的问题很简单,我编写的代码首先缩小图像,然后裁剪到所需的尺寸(由 Constants 类获得)。

if(image != null){
        Image originalImage = image.getImage();

        int width = Constants.width;

        //Algorithm: get the original width and divide with desired width
        int height = originalImage.getHeight(null)/(originalImage.getWidth(null)/width);

        Image scaledImage = originalImage.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);

        //Now to crop it to specified dimensions
        BufferedImage imageToCrop = (BufferedImage) (java.awt.Image) scaledImage;

        height = Constants.height;

        imageToCrop = imageToCrop.getSubimage(imageToCrop.getWidth() - width, imageToCrop.getHeight() - height, width, height);

        image.setImage(imageToCrop);
    }

运行时,这是我得到的错误:

java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage

这对应于以下行:

BufferedImage imageToCrop = (BufferedImage) (java.awt.Image) scaledImage;

现在我没有在任何地方导入 sun.awt,事实上这里是此类的导入项列表:

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.swing.ImageIcon;

那么为什么会发生此错误?我就是不明白!正如你所看到的,我什至尝试了很多方法来手动强制演员,但仍然无济于事:(

非常感谢任何帮助!谢谢!

my problem is simple, I have the code I have written to first scale the image down and then crop to desired dimensions (obtained by the Constants class).

if(image != null){
        Image originalImage = image.getImage();

        int width = Constants.width;

        //Algorithm: get the original width and divide with desired width
        int height = originalImage.getHeight(null)/(originalImage.getWidth(null)/width);

        Image scaledImage = originalImage.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);

        //Now to crop it to specified dimensions
        BufferedImage imageToCrop = (BufferedImage) (java.awt.Image) scaledImage;

        height = Constants.height;

        imageToCrop = imageToCrop.getSubimage(imageToCrop.getWidth() - width, imageToCrop.getHeight() - height, width, height);

        image.setImage(imageToCrop);
    }

When run, this is the error I get:

java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage

And this corresponds to the line:

BufferedImage imageToCrop = (BufferedImage) (java.awt.Image) scaledImage;

Now I have NOT imported sun.awt anywhere, in fact here is the list of imported items for this class:

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.swing.ImageIcon;

So why does this error occur? I just don't get it! As you can see I even tried many ways to MANUALLY FORCE THE CAST but still to no avail :(

Any help really appreciated! Thanks!

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

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

发布评论

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

评论(5

久伴你 2024-11-03 05:53:13

您没有导入它,但它是 Image 的子类,而不是 BufferedImage 的子类,因此您无法转换为它。

要从给定的 Image 创建 BufferedImage,您必须在 BufferedImage 的新实例上绘制目标图像。检查 此搜索

一个示例 来自 dzone

  public BufferedImage bufferImage(Image image, int type) {
    BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    Graphics2D g = bufferedImage.createGraphics();
    g.drawImage(image, null, null);
    waitForImage(bufferedImage);
    return bufferedImage;
  }

You didn't import it, but it is a subclass of Image, and not a subclass of BufferedImage, so you cannot cast to it.

To create a BufferedImage from a given Image, you have to draw the target image on a new instance of BufferedImage. Check some results from this search

One example from dzone:

  public BufferedImage bufferImage(Image image, int type) {
    BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    Graphics2D g = bufferedImage.createGraphics();
    g.drawImage(image, null, null);
    waitForImage(bufferedImage);
    return bufferedImage;
  }
囚我心虐我身 2024-11-03 05:53:13

您无法转换它,因为在运行时您的scaledImage 具有ToolkitImage 类型,它不是BufferedImage 的子类。

You can't cast it because at runtime your scaledImage has type ToolkitImage, which is not a subclass of BufferedImage.

你丑哭了我 2024-11-03 05:53:13

Sun 类是实际的实现。您不需要导入它,工具包需要创建它。我不熟悉该库的特定部分的内部结构,但它似乎不是 BufferedImage 。

另外,为什么这里有两个演员?

BufferedImage imageToCrop = (BufferedImage) (java.awt.Image) scaledImage;

The Sun class is the actual implementation. You don't need to import it, the toolkit needs to create it. I am not familiar with the guts of that particular part of the library, but it appears that it is not a BufferedImage.

Also, why do you have two casts here?

BufferedImage imageToCrop = (BufferedImage) (java.awt.Image) scaledImage;
寒江雪… 2024-11-03 05:53:13

您尚未导入 ToolkitImage 的事实并不会改变 getScaledInstance 返回的实际对象是该类的实例的事实。无论您是否愿意,它都是一个 ToolkitImage

我认为这里要做的就是使用 ToolkitImage 和/或找到某种方法将其转换为您想要使用的任何 Image 实现。

The fact that you've not imported ToolkitImage does not change the fact that the actualy object returned by getScaledInstance is an instance of that class. It is a ToolkitImage whether you want it or not.

I think the thing to do here is to just work with ToolkitImage and/or find some way to convert it into whatever Image implementation that you want to work with.

梦归所梦 2024-11-03 05:53:13

getScaledInstance 不保证返回比 Image 更具体的内容。您可能很幸运并获得 BufferedImage,也可能不会。 (理论上,我没有看到任何说法不允许返回伪图像,其实现只是缩放您传入的所有维度并调用底层图像上的方法来完成所有工作。)

除此之外,您根本不应该使用 getScaledInstance,因为缩放图像的其他方法更快、更灵活。您可以调整 Bozho 的代码,但替换您的原始图像,并传递您想要的高度和宽度的两个额外参数。 (您可能还想在图形上下文上设置渲染提示。)

getScaledInstance doesn't guarantee to return anything more specific than an Image. You might be lucky and get a BufferedImage, or you might not. (In theory I don't see anything to say that it's not allowed to return a pseudo-image whose implementation just scales all the dimensions that you pass in and calls the methods on the underlying image to do all the work.)

That aside, you shouldn't really use getScaledInstance at all, since the other ways of scaling an image are faster and more flexible. You could adapt Bozho's code but substituting your original image, and passing the two extra arguments for the height and width that you want. (You probably also want to set the rendering hint on the graphics context.)

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