Login  Register

Re: Pointing to a fixed directory structure

Posted by dlraby on Oct 14, 2009; 12:35am
URL: http://imagej.273.s1.nabble.com/Pointing-to-a-fixed-directory-structure-tp3690781p3690783.html

Thank you both, the ("\\") fixed the path and the short test plugin
generates dialog windows at points through the code and also displays the
path to the test1.bmp. However, i have been stumped by my inability to read
and display the image to verify that i have successfully imported the image
into my function. Hopefully you can help me again.

my simple test code is:

public class FileGrabTest_ implements PlugIn {

    private String title;

    ImagePlus ip1;
    private String imagePath1 = "";
    private String imagePath2 = "";
    private String imagePath3 = "";


    public void run(String arg) {
            IJ.showMessage(title, "Running the first Test");

            // line 29 in error report
            test_file_grab();
        }

    public void test_file_grab() {

         String imagePath1 = IJ.getDirectory("C:\\Users\\draby\\Documents");

         imagePath2 = imagePath1+"test1.bmp";
         imagePath3 = IJ.openAsString(imagePath2);

         ImagePlus rgbBackGround = IJ.openImage(imagePath3);

         IJ.showMessage(title, imagePath2);

         IJ.showMessage(title, "Running the second Test");

         // extract image information and pixel data
         // line 47 in error report
         ImageProcessor bg_ip = rgbBackGround.getProcessor();
         int[] bg_pixels = (int[]) bg_ip.getPixels();

         rgbBackGround.show();
         rgbBackGround.updateAndDraw();
    }
}

and the error message is:
    java.lang.NullPointerException
    at HandDet_.test_file_grab(HandDet_.java:47)
    at HandDet_.run(HandDet_.java:29)
    at ij.IJ.runUserPlugIn(IJ.java:179)
    at ij.IJ.runPlugIn(IJ.java:146)
    at ij.Executer.runCommand(Executer.java:122)
    at ij.Executer.run(Executer.java:59)
    at java.lang.Thread.run(Unknown Source)

I am sorry to bother you with what is probably trivial but i have confused
myself over the past 8 hours to the point where i cant see the "plug for the
wires". Any further assistance would be greatly appreciated.
Thank you, Dean


On Tue, Oct 13, 2009 at 12:24 PM, Curtis Rueden <[hidden email]> wrote:

> Hi Dean,
>
> I have tried to read 3 bitmap images from the same directory and have been
> > unable to set the correct path. Could you let me know from which location
> > the path should be set relative to (i am running Eclipse on my PC):
> >
>
> Backslash literals in Java must be escaped with another backslash ("\\"),
> because backslash is used to represent other special characters (tab is
> "\t", newline is "\n", etc.).
>
> So in your code, try:
>
>  String imagePath =
> IJ.getDirectory("C:\\Code\\Java\\MyPlugin\\images\\test1.bmp");
>
> It would probably also work with forward slashes.
>
> -Curtis
>
> On Tue, Oct 13, 2009 at 1:52 PM, Dean Raby <[hidden email]> wrote:
>
> > I am new to writing Java (2 months), but find the ImageJ a very
> interesting
> > tool, and hope to write a few plugins to "quickly" test some imaging
> > concepts. This work requires me to compare three images. The available
> > examples, i have found, show the image information being taken from the
> > image selected via the GUI however i need my plugin to read the contents
> > from a fixed directory on the host PC. The image names will also be hard
> > coded by the camera attached to the PC. If you have a pointer to a
> similar
> > plugin or example it would be greatly appreciated.
> >
> > I have tried to read 3 bitmap images from the same directory and have
> been
> > unable to set the correct path. Could you let me know from which location
> > the path should be set relative to (i am running Eclipse on my PC):
> >
> > plugin directory path: C:\Code\Java\MyPlugin\src\test_.java
> > image directory path: C:\Code\Java\MyPlugin\images\xxx.bmp
> >
> > I have tried using the full system path and the relative path and neither
> > one seems to work.
> >
> > I am trying to use, to point to and open a test image:
> >
> > String imagePath =
> > IJ.getDirectory("C:\Code\Java\MyPlugin\images\test1.bmp");
> > dirPath = IJ.openAsString(imagePath);
> > ImagePlus imp2 = IJ.openImage(dirPath);
> >
> > In the above code, the "slash" direction reflects that shown on the
> Eclipse
> > tool, please let me know if is need to parse this and change the "slash"
> > direction.
> >
> > thank you, Dean
> >
>