我的程序中有这个简短的子例程,在 Java 7 和 Windows 7 上运行良好,可以读取 .jpeg 图像文件。
我将 Java 升级到版本 12,现在使用 Windows 10,但它无法再读取 .jpeg 文件。我使用常规 .jpeg 文件 RGB 8 和 12 位。我想知道现在是否有更新的类(class),但我无法找到有关它的信息。
/*
* Get image subroutine
*/
BufferedImage img = null;
try {
img = ImageIO.read(new File(InputFolder + name));
/*
* Get dimension subroutine
*/
int imagewidth = img.getWidth();
int imageheight = img.getHeight();
System.out.println(" retrieving width of " + name + " .... " + imagewidth);
System.out.println(" retriving height of " + name + " .... " + imageheight);
/*textArea1.append("\n retrieving width of "+name+" .... \n"+imagewidth);
textArea1.append("\n retriving height of "+name+" .... \n"+imageheight);*/
我会遇到一个异常,说“文件错误不是图像文件..已中止.. 所有文件都可以使用 Photoshop 等照片编辑器打开。
这是新代码。我总是收到 -1 的宽度和长度。
try {
MediaTracker mt = new MediaTracker(new JPanel());
Image img = Toolkit.getDefaultToolkit().getImage(InputFolder + name);
mt.addImage(img, 0);
System.out.println("Wait image to be load to retrieve info...");
mt.waitForAll();
System.out.println("Loaded");
/**
* Get dimension subroutine
*/
System.out.println(" Get Width & Height ");
int imagewidth = img.getWidth(this);
int imageheight = img.getHeight(this);
最佳答案
关于Java : Getting an exception "File error not an image file" while trying to read an image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58030471/