gpt4 book ai didi

java - 错误 : sun. awt.image.ToolkitImage 无法转换为 java.awt.image.BufferedImage

转载 作者:行者123 更新时间:2023-11-29 03:23:19 32 4
gpt4 key购买 nike

请帮我解决这个问题 sun.awt.image.ToolkitImage 无法转换为 java.awt.image.BufferedImage

 if (shape.hasImage())
{
// If this shape is an image, extract image to file
String extension = ImageTypeToExtension(shape.getImageData().getImageType());
String imageFileName = MessageFormat.format("Image.ExportImages.{0} Out.{1}", imageIndex, extension);
String strBarCodeImageExtracted = "" + imageFileName;
shape.getImageData().save(strBarCodeImageExtracted);

// Recognize barcode from this image
BarCodeReader reader = new BarCodeReader ((BufferedImage) Toolkit.getDefaultToolkit().getImage(strBarCodeImageExtracted),BarCodeReadType.Code39Standard);
while (reader.read())
{
System.out.println("codetext: " + reader.getCodeText());
}
imageIndex++;
}

最佳答案

编辑:这个答案在写完评论后被接受,所以必须假设评论是实际的解决方案。评论是

... to replace Toolkit.getDefaultToolkit().getImage(...) with ImageIO.read(...) ...


原始答案:

您可以尝试使用 ImageIO 直接读取图像,或者考虑将图像绘制到新分配的 BufferedImage 中,例如用类似的方法

public static BufferedImage convertToBufferedImage(Image image)
{
BufferedImage newImage = new BufferedImage(
image.getWidth(null), image.getHeight(null),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = newImage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return newImage;
}

然后在你的情况下:

Image image = Toolkit.getDefaultToolkit().getImage(strBarCodeImageExtracted);
BufferedImage bufferedImage = convertToBufferedImage(image);
BarCodeReader reader = new BarCodeReader(bufferedImage,BarCodeReadType.Code39Standard);

关于java - 错误 : sun. awt.image.ToolkitImage 无法转换为 java.awt.image.BufferedImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22426040/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com