简体   繁体   中英

Check if ImageJ has an image open

I'm currently using the ImageJ jar (ij.jar) in a Java application that I am constructing and have managed to get everything working. However, there is a chance that when I execute the line:

ImagePlus image = new ImagePlus(path);

(where path is a user specified path to an image), the image may not be there. The ImageJ library will not throw an error until methods of image are accessed, and at that time, ImageJ will print to the console There are no images open. and terminate the program. I believe that ImageJ is actually calling System.exit() when it prints that line since my whole application terminates -- incuding Deamon threads.

Is there a way using ImageJ to check if an image was successfully opened? Or should I just check if the file exists by using Java's File class?

EDIT:

To clarify, this is a multithreaded application that may attempt to open and process many images at the same time. The ImageJ class ( IJ ) has functions to open images, but IJ is a singleton class, and thus has odd behavior when multiple calls to IJ.openImage("string") occur at the same time.

One way to test whether the image was loaded successfully is to check whether the width and height of the image are both zero:

ImagePlus image = new ImagePlus("/non/existent");
if (image.getWidth() == 0 && image.getHeight() == 0) {
    // Then the image wasn't loaded...
}

I'm afraid I can't reproduce the behaviour you describe where the application exits, though, so maybe this won't work for you...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM