How to let the user select an image?

edited June 2015 in How To...

I want to create a processing app which first asks the user to choose an image he has on his computer, and then executes the program with that image. What want is something like a window to pop when the app starts which lets you select a image of your choice, which then is put in the data folder. How would I do this?

Answers

  • edited May 2015

    https://processing.org/reference/selectInput_.html

    hint

    also set a marker if the image was already loaded

    in draw()

    • if the image was not loaded : wait

    • if the image was already loaded proceed with the program

  • Thanks for the quick response!! Thats exactly what I needed. I'm quite new to processing so I don't understand it completely. How exactly do I "use" the image after I've done this function? Normally I would just have specified the image and could have done something like this

    image(img, 0, 0); filter(GRAY);

    How do I do this with a image selected by the user?

  • @Bob123 you create some var= Image img; and assign it to your selected image (by user) then (some boolean, imageSelected) you can use it in draw() as you have done...

  • img = loadImage( theNameThatHasBeenEntered );

  • edited May 2015

    I mean: selectInput does not return anything (void). How do I assign a variable from that function? I'd be really happy if someone could post an example : )

  • PImage img; 
    boolean imageSelected = false; 
    
    
    void setup() {
      selectInput("Select a file to process:", "fileSelected");
    }
    
    void fileSelected(File selection) {
      if (selection == null) {
        println("Window was closed or the user hit cancel.");
      } else {
        println("User selected " + selection.getAbsolutePath());
        String theNameThatHasBeenEntered  = selection.getAbsolutePath(); 
        img = loadImage( theNameThatHasBeenEntered );
        imageSelected = true; 
      }
    }
    
  • I tried adding these two lines to be to view the image size(300,300); image(img, 0, 0);

    I put them in setup() right under "selectInput". But an error code comes up "NullPointerExeption" What have I done wrong?

  • oh dear

  • edited May 2015

    you lack some basic understanding

    is this exam / homework of some sort?

    I wrote all this in my first post.....

    see above....

    ;-)

  • No its not homework. I usually programm arduino, and I need this for an arduino project. Unfortunatly I haven't got a lot of experience with processing. (As you may have noticed.) Sorry if Im bit slow. So what have I done wrong? Is it the missing "marker"?

  • yes the missing "marker"

    it is just a boolean I called imageSelected in my example

    but you need

    void draw() {
        if (!imageSelected) {
            // wait
        }
        else {
            image(img, 0, 0);
        }
    } 
    
  • we don't have a way to do something when user hits cancel but never mind

  • edited June 2015
    PImage img = null; 
    boolean imageSelected = false;  // marker 
    
    
    void setup() {
      size(660, 660);
      selectInput("Select a file to process:", "fileSelected");
    } // func
    
    void draw() {
      // eval marker 
      if (!imageSelected) {
        // wait
      } else {
        // show it
        image(img, 0, 0);
      }
    } // func 
    
    void fileSelected(File selection) {
      if (selection == null) {
        println("Window was closed or the user hit cancel.");
      } else {
        println("User selected " + selection.getAbsolutePath());
        String theNameThatHasBeenEntered  = selection.getAbsolutePath(); 
        img = loadImage( theNameThatHasBeenEntered );
        imageSelected = true;
      }
    } // func
    //
    
Sign In or Register to comment.