最佳答案
在 StackOverflow 上已经存在类似于 链接的问题,公认的答案是“铸造”:
Image image = ImageIO.read(new File(file));
BufferedImage buffered = (BufferedImage) image;
在我的程序中,我尝试:
final float FACTOR = 4f;
BufferedImage img = ImageIO.read(new File("graphic.png"));
int scaleX = (int) (img.getWidth() * FACTOR);
int scaleY = (int) (img.getHeight() * FACTOR);
Image image = img.getScaledInstance(scaleX, scaleY, Image.SCALE_SMOOTH);
BufferedImage buffered = (BufferedImage) image;
不幸的是,我得到了运行时错误:
无法将 sun.awt.image. ToolkitImage 转换为 java.awt.image. BufferedImage
显然选角不管用。
问题是: 将 Image 转换为 BufferedImage 的正确方法是什么?