Search code examples
javaimagej

ImageJ API: how to display an image with zoom and drag to scroll features active in the image window?


If I use Fiji application to open an image, then the image is displayed in a window where I can use + and - keys to zoom in and out, also I can hold down the space bar and drag the zoomed image with mouse to scroll through it.

I want to implement the same features in a java application using ImageJ API. If I use the following code (taken from here) to open an image, then the image is displayed in the same type of window as in Fiji case, but zoom and drag to scroll features are not active in the window.

Opener opener = new Opener();  
ImagePlus imp = opener.openImage("/path/to/image.tif");  
imp.show();

Could somebody suggest how to get the two features active? To me they look like standard features that everybody uses, so I expect that their activation could be easily available through the ImageJ API. However I found no hints in the API specs.

As far as I understand, the call imp.show(); in the code above is equivalent to the following:

ImageWindow imageWindow = new ImageWindow(imp);
imageWindow.setVisible(true);

If so, there should be some methods of this ImageWindow class that would assure activation of zoom and of drag to scroll. Does anyone have a clue?

Or, maybe, could someone share a direct link to Fiji source code where these exact features are implemented? Is it supposed to be here? If yes, then where exactly?


Solution

  • I resolved it. As I expected, there exists a simple way to activate zoom and drag to scroll features of ImageJ in a Java program.

    As I see now, most of the features of ImageJ are implemented as plugins under ij.plugin.*.

    To activate a desired ImageJ feature in your Java program you need (1) to find the plugin that corresponds to the feature inside ij.plugin.* package, (2) to call the plugin in your code, and (3) to put IJ_Props.txt file taken from the standard ImageJ distribution to the directory where you run your java .jar executable (it appears that IJ_Props.txt file contains settings used by many of the available ImageJ plugins).

    So, in our case we need to activate ij.plugin.Zoom plugin in our java code:

    //Launch ImageJ in NO_SHOW mode: Run embedded and invisible in another application.
    ImageJ imageJApplication = new ImageJ(2);
    
    Opener opener = new Opener();  
    String imageFilePath = "path/to/your/image.png";
    ImagePlus imp = opener.openImage(imageFilePath);
    imp.show();
    IJ.runPlugIn("ij.plugin.Zoom", null);
    

    When executing this code we get ij.plugin.Zoom plugin activated and both of the desired features (zoom and drag to scroll) will be functional in the window where our image is displayed. We also get the traditional ImageJ thumbnail view of the displayed image area in the upper left corner of the window.