Setting the scaleBar With JAVA

imagej

#1

Hello all,

I want to develop an Windows Forms Application developed with JAVA (pure Java. Required for external reasons: no plugin, no macro) to use ImageJ library.

This application opens an image from hard disk and the user has to draw a line in the image (it would ve the knowed distance). Now, I want from java code to set the scale and to show the scaleBar in the image. The problem is that scalebar is not showing in the image. I have this code:

ImageJ imageJApplication = new ImageJ(2);
this.imp = IJ.openImage(file);
IJ.setTool(Toolbar.LINE);
imp.show();
new ij.gui.WaitForUserDialog(“Draw a Line”).show();
IJ.run( “Set Scale…”, “known=5 pixel=1 unit=cm”);
IJ.run( “Scale Bar…”, “width=6 height=4 font=14 color=White background=None location=[Lower Right] bold”);
imp.show();

The scaleBar never showing…But if i test the code like a macro it works!!

Please can you help me? I am new in ImageJ

Sorry for my English…


HELP- How to set scale and get Length of line
#2

Here is a reproducible example in Java as an ImageJ plugin:

import ij.IJ;
import ij.ImagePlus;
import ij.gui.Toolbar;
import ij.plugin.PlugIn;
public class My_Plugin implements PlugIn {

	private ImagePlus imp;

	public void run(String arg) {
		this.imp = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
		IJ.setTool(Toolbar.LINE);
		imp.show();
		new ij.gui.WaitForUserDialog("Draw a Line").show();
		IJ.run(imp, "Set Scale...", "");
		IJ.run(imp, "Scale Bar...", "");
		imp.show();
	}
}

Use the macro recorder (set Java) to get the commands. In your special case the quotes (third argument) have to be empty to open the dedicated dialogs which needs to be set.


#3

Dear,

please note that the OP likes to create a stand-alone application that uses the ImageJ-API.
He writes explicitely:

no plugin, no macro

In a previous thread, I’ve already pointed the OP to what you’ve just posted …

Greetings

Herbie


#4

Hi @dmacias,

I just tried your code and actually, it almost works. If I execute this main method:

public class DummyMain {
    public static void main(String... args) {
        String file = "C:/structure/data/blobs.gif";

        ImageJ imageJApplication = new ImageJ(2);
        ImagePlus imp;
        imp = IJ.openImage(file);
        IJ.setTool(Toolbar.LINE);
        imp.show();
        new ij.gui.WaitForUserDialog("Draw a Line").show();
        IJ.run( "Set Scale...", "known=5 pixel=1 unit=cm");
        IJ.run( "Scale Bar...", "width=6 height=4 font=14 color=White background=None location=[Lower Right] bold");
        imp.show();
    }
}

It finally opens this window:
image \

The only thing I had to do was removing the this. in front of one of the imps (line 2 in your example).

The question is now, why doesn’t it work in your case? Could you maybe share a fully running minimal code example for example as github repository, so that we can check?

Cheers,
Robert


#5

@Herbie Already to late but here is mine which loads the ImageJ frame, too:

import ij.IJ;
import ij.ImageJ;
import ij.ImagePlus;
import ij.gui.Toolbar;

public class Main {
	
	private ImagePlus imp;

	public static void main(String[] args) {

		new Main();

	}

	public Main() {
		ImageJ imagej = new ImageJ(0);
		imp = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
		IJ.setTool(Toolbar.LINE);
		imp.show();
		new ij.gui.WaitForUserDialog("Draw a Line").show();
		IJ.run("Set Scale...", "");
		IJ.run("Scale Bar...", "");
		imp.show();

	}
}

#6

Thank you Robert,

I have pasted your code and i have same problem, the scaleBar is not showing…

Print


#7

ImageJ(0) throws an exception: Cannot cast sun.awt.image.ToolkitImage to java.awt.image.BufferedImage

Thank you.


#8

Please show your full code so that we can see the imports. Which IDE do you use? What is your Java classpath?
Which operating system? Do you use my example?


#9

I am developing with .NET Framework. I have recompiled ij.jar to ij.dll. Apparently all is working…

My code is:

String file = @“C:\Temp\blobs.gif”;
ImageJ imageJApplication = new ImageJ(2);
ImagePlus imp;
imp = IJ.openImage(file);
IJ.setTool(Toolbar.LINE);
imp.show();
new ij.gui.WaitForUserDialog(“Draw a Line”).show();
IJ.run(imp, “Set Scale…”, “known=5 pixel=1 unit=cm”);
IJ.run(imp, “Scale Bar…”, “width=6 height=4 font=14 color=White background=None location=[Lower Right] bold”);
imp.show();

I attach the result of the code and ImageJ Versión… I am using Windows7:

Print Print2 Print1


#10

Can you find out in which line this is happening?

Again, sharing the full code (Visual studio project folder?) would be nice. We can otherwise not execute your code on our computers. That’s the goal of having a minimal working example. It makes our life easier while helping you :wink:


#11

The exception thwowsin:
ImageJ imageJApplication = new ImageJ(0);

It works with new ImageJ (2);

I will upload the Proyect as soon as posible…

Thanks!


#12

Attach the proyect file…

Thank you…ImageJ.rar (8.8 MB)