【问题标题】:Java : Getting an exception "File error not an image file" while trying to read an imageJava:尝试读取图像时出现异常“文件错误不是图像文件”
【发布时间】:2019-09-24 00:12:34
【问题描述】:

我的程序中有这个简短的子例程,在 Java 7 和 Windows 7 上运行良好,可以读取 .jpeg 图像文件。

我将 Java 升级到版本 12,现在使用 Windows 10,它无法再读取 .jpeg 文件。我正在使用常规的 .jpeg 文件 RGB 8 和 12 位。我想知道现在是否有更新的课程,但我找不到有关它的信息。

 /*
  * 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 image bufferedreader


    【解决方案1】:

    如答案here中所述

    尝试使用ToolkitcreateImage()、getImage() 代替 ImageIO.read()

    【讨论】:

    • 谢谢,它可以获取图像,但不幸的是它返回 -1 的宽度和长度。我有一个跟踪器来等待图像加载或使用 createImage 没有成功......
    • 您能否详细说明您的尝试,甚至对上面已经共享的代码进行编辑?
    • 我编辑了我的代码....我只是尝试获取以像素为单位的图像宽度和以像素为单位的图像长度。
    猜你喜欢
    • 2020-09-13
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-04
    • 2016-07-05
    • 2019-07-24
    相关资源
    最近更新 更多