0

I had this short subroutine in my program working well on Java 7 and Windows 7 to read .jpeg image files.

I upgraded Java to version 12 and now using Windows 10 and it cannot read anymore .jpeg file. I am using regular .jpeg file RGB 8 & 12 bit. I'm wondering if a newer class is available now but I'm not able to find the information about it.

 /*
  * 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);*/

I will have an exception saying "File error not an image file..Aborted.. All file can be open with a photo editor like Photoshop.

This is the new code. I always received -1 for width and length.

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);
Mike
  • 1
  • 1

1 Answers1

0

As mentioned in the answer here

Try using Toolkit createImage(), getImage() instead of ImageIO.read()

  • Thanks, it work to get the image but unfortunately it return -1 for the width and length. I had a tracker to wait the image to be load or use createImage without success... – Mike Sep 23 '19 at 16:05
  • Can you explain more on what you tried or even add an edit to the already shared code above? – Guhan Sundaresan Sep 23 '19 at 16:22
  • I edited my code.... I just try to get the image width in pixel and the image length in pixel. – Mike Sep 23 '19 at 17:36