Skip to content

Instantly share code, notes, and snippets.

@bogovicj
Last active January 3, 2017 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bogovicj/90a1d5f15833fe70181a02ede2a87ab1 to your computer and use it in GitHub Desktop.
Save bogovicj/90a1d5f15833fe70181a02ede2a87ab1 to your computer and use it in GitHub Desktop.
ImageJ Keypress example
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import ij.IJ;
import ij.ImageJ;
import ij.ImagePlus;
public class SpaceToContinueExample implements KeyListener {
final ImageJ ij;
ImagePlus ip;
public SpaceToContinueExample( )
{
ij = IJ.getInstance();
System.out.println( "ij: " + ij );
}
public SpaceToContinueExample( ImagePlus ip )
{
this();
this.ip = ip;
ip.show();
ip.getCanvas().addKeyListener(this);
}
public void processBefore()
{
ip = IJ.getImage();
IJ.run("Gaussian Blur...", "sigma=2");
IJ.getImage().show();
}
public void processAfter()
{
System.out.println("process after");
IJ.run("Measure");
// remove this key listener to be safe
ip.getCanvas().removeKeyListener(this);
}
public void run()
{
processBefore();
// process After is called on keypress
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) { }
@Override
public void keyReleased(KeyEvent e) {
if ( e.getKeyCode() == KeyEvent.VK_SPACE )
{
processAfter();
}
}
public static void main(String[] args) {
ImageJ ij = new ImageJ();
IJ.run("Boats (356K)");
SpaceToContinueExample obj = new SpaceToContinueExample( IJ.getImage() );
obj.run();
}
}
@bogovicj
Copy link
Author

bogovicj commented Jan 3, 2017

This java snippet:

  1. starts ImageJ,
  2. opens an image (boats)
  3. blurs it and shows the results
  4. waits for the user to do things, like draw an ROI, pressing space when finished
  5. Then calls "Measure"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment