简体   繁体   English

检查ImageJ是否打开了图像

[英]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. 我目前在正在构建的Java应用程序中使用ImageJ jar(ij.jar),并设法使所有功能正常运行。 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. (其中path是用户指定的图像路径),该图像可能不存在。 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. 在访问image方法之前,ImageJ库不会引发错误,并且此时,ImageJ将打印到控制台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. 我相信ImageJ在打印该行时实际上正在调用System.exit() ,因为我的整个应用程序都终止了-包括Deamon线程。

Is there a way using ImageJ to check if an image was successfully opened? 有没有一种使用ImageJ的方法来检查图像是否成功打开? Or should I just check if the file exists by using Java's File class? 还是应该使用Java的File类检查文件是否存在?

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. ImageJ类( IJ )具有打开图像的功能,但是IJ是单例类,因此,当IJ.openImage("string")IJ.openImage("string")多次调用时,具有奇怪的行为。

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... 恐怕我无法重现您描述的应用程序退出位置的行为,因此这可能对您不起作用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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