Java:我收到一个类转换异常错误,指向我什至没有导入的类
我的问题很简单,我编写的代码首先缩小图像,然后裁剪到所需的尺寸(由 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您没有导入它,但它是
Image
的子类,而不是BufferedImage
的子类,因此您无法转换为它。要从给定的
Image
创建BufferedImage
,您必须在BufferedImage
的新实例上绘制目标图像。检查 此搜索一个示例 来自 dzone:
You didn't import it, but it is a subclass of
Image
, and not a subclass ofBufferedImage
, so you cannot cast to it.To create a
BufferedImage
from a givenImage
, you have to draw the target image on a new instance ofBufferedImage
. Check some results from this searchOne example from dzone:
您无法转换它,因为在运行时您的scaledImage 具有ToolkitImage 类型,它不是BufferedImage 的子类。
You can't cast it because at runtime your scaledImage has type ToolkitImage, which is not a subclass of BufferedImage.
Sun 类是实际的实现。您不需要导入它,工具包需要创建它。我不熟悉该库的特定部分的内部结构,但它似乎不是 BufferedImage 。
另外,为什么这里有两个演员?
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?
您尚未导入
ToolkitImage
的事实并不会改变getScaledInstance
返回的实际对象是该类的实例的事实。无论您是否愿意,它都是一个ToolkitImage
。我认为这里要做的就是使用
ToolkitImage
和/或找到某种方法将其转换为您想要使用的任何Image
实现。The fact that you've not imported
ToolkitImage
does not change the fact that the actualy object returned bygetScaledInstance
is an instance of that class. It is aToolkitImage
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 whateverImage
implementation that you want to work with.getScaledInstance
不保证返回比Image
更具体的内容。您可能很幸运并获得BufferedImage
,也可能不会。 (理论上,我没有看到任何说法不允许返回伪图像,其实现只是缩放您传入的所有维度并调用底层图像上的方法来完成所有工作。)除此之外,您根本不应该使用 getScaledInstance,因为缩放图像的其他方法更快、更灵活。您可以调整 Bozho 的代码,但替换您的原始图像,并传递您想要的高度和宽度的两个额外参数。 (您可能还想在图形上下文上设置渲染提示。)
getScaledInstance
doesn't guarantee to return anything more specific than anImage
. You might be lucky and get aBufferedImage
, 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.)