Java : Getting an exception "File error not an image file" while trying to read an image

标签 java image bufferedreader

我的程序中有这个简短的子例程,在 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);

最佳答案

正如答案中提到的here

尝试使用Toolkit createImage()、getImage() 而不是 ImageIO.read()

关于Java : Getting an exception "File error not an image file" while trying to read an image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58030471/

相关文章:

java - ColdFusion 8 性能和负载监控

java - 如何使用java从现在开始减去30天、3个月和1年的纪元时间?

php - 使用 mySQL 在 PHP 中上传图像

Android - 如何使用位图避免内存过载?

java - 不读取文件?

java - 设置要使用 BufferedReader 读取的文件的相对路径

java - 读取纯文本文件

java - Spring MVC错误验证错误

java - 滑动拼图Java Shuffle问题

ios - 图像未在 ipad 模拟器中显示