簡體   English   中英

Java:嘗試讀取圖像時出現異常“文件錯誤不是圖像文件”

[英]Java : Getting an exception “File error not an image file” while trying to read an image

我的程序中有這個簡短的子程序在 Java 7 和 Windows 7 上運行良好,可以讀取.jpeg 圖像文件。

我將 Java 升級到版本 12,現在使用 Windows 10,它無法再讀取.jpeg 文件。 我正在使用 regular.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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM