javaimage-processingopenimaj

OpenIMAJ and Image Read Exception


I'm following the OpenIMAJ tutorial, and when I run the following code:

import java.io.IOException;
import java.net.URL;
import org.openimaj.image.ImageUtilities;
import org.openimaj.image.MBFImage;
import org.openimaj.image.colour.ColourSpace;
import org.openimaj.image.colour.RGBColour;

public class App {
    public static void main( String[] args ) throws IOException {

    MBFImage image = ImageUtilities.readMBF(new URL("http://dl.dropbox.com/u/8705593/sinaface.jpg"));
    System.out.println(image.colourSpace);

    }
}  

I get the following exception:

Exception in thread "main" java.io.IOException: org.apache.sanselan.ImageReadException: Can't parse this format.
  at org.openimaj.image.ExtendedImageIO.read(ExtendedImageIO.java:189)
  at org.openimaj.image.ExtendedImageIO.read(ExtendedImageIO.java:238)
  at org.openimaj.image.ImageUtilities.readMBF(ImageUtilities.java:381)
  at somefolder.App.main(App.java:37)
Caused by: org.apache.sanselan.ImageReadException: Can't parse this format.
  at org.apache.sanselan.Sanselan.getImageParser(Sanselan.java:621)
  at org.apache.sanselan.Sanselan.getBufferedImage(Sanselan.java:1260)
  at org.apache.sanselan.Sanselan.getBufferedImage(Sanselan.java:1163)
  at org.apache.sanselan.Sanselan.getBufferedImage(Sanselan.java:1136)
  at org.openimaj.image.ExtendedImageIO.read(ExtendedImageIO.java:187)
... 3 more

Does anyone know how I can fix this? Thanks.

EDIT: If I download the image and read it from the file system, it works. Still, I would like to understand why reading from a URL doesn't work.


Solution

  • Dropbox changed there urls a while ago & now use a 302 redirect to https://dl.dropboxusercontent.com/u/8705593/sinaface.jpg. Java's URL class doesn't follow redirects when you open the original URL, and it ends up resulting in this error.

    We've already fixed the documentation for the development version with new working URLs: http://openimaj.github.io/openimaj/tutorial/processing-your-first-image.html

    (Note: if you need to deal with redirects, the org.openimaj.io.HttpUtils class has functionality to open InputStreams from URLs whilst dealing with a number of possible redirection strategies found on the web, including 302s and meta-refresh based redirects).