Image Selector

edited June 2017 in How To...

Hi Im trying to make an image selector, which can select images from any destination on the computer. Im trying to use the JFileCHooser.

void chooser() {
 JFileChooser chooser = new JFileChooser();
 selected = loadImage("chooser.showOpenDialog(aComponent)");
 image(selected, width/2,height/2);
}

This is what I tried so far, but it won't work :/ any ideas?

Answers

  • edited February 2014 Answer ✓

    you can use processing's own command

    http://www.processing.org/reference/selectInput_.html

    //void chooser() {
    // JFileChooser chooser = new JFileChooser();
    // selected = loadImage("chooser.showOpenDialog(aComponent)");
    // image(selected, width/2,height/2);
    //}
    //
    //
    
    PImage selected; 
    
    void setup() {
      size (600, 600);
      selectInput("Select an image to process:", "fileSelected");
    }
    
    void draw() {
      if (selected!=null)
        image(selected, 2, 2);
    }
    
    void fileSelected(File selection) {
      if (selection == null) {
        println("Window was closed or the user hit cancel.");
      } 
      else {
        println("User selected " + selection.getAbsolutePath());
        selected = loadImage(selection.getAbsolutePath());
      }
    }
    
  • you can also just do it like this

    import javax.swing.*; 
    PImage selected;
    
    void setup() {
      size(444, 333);
      if (selected==null)
        chooser() ;
    }
    
    void draw() {
      //
    }
    
    void chooser() {
      JFileChooser chooser = new JFileChooser();
    
      int returnVal = chooser.showOpenDialog(this); 
    
      if (returnVal == JFileChooser.APPROVE_OPTION) {
    
        File file = chooser.getSelectedFile();
        // println (file);
        String myInputFile = file.getAbsolutePath();
    
        if (fileIsOK(myInputFile))
          selected = loadImage(myInputFile);
        else 
          println ("not ok");
    
        if (selected!=null)
          image(selected, 2, 2);
      }
      else {
        println("Cancelled.");
      }
    }
    // 
    //
    boolean fileIsOK (String name) {
      // Image ? 
      //
      if (name==null) { 
        return false; // not ok
      }
      // name=trim(name);
      if (name.equals("")) return false;  
      if (name.substring (  name.length()-4 ).equals (".jpg")  ) return true; 
      if (name.substring (  name.length()-4 ).equals (".png")  ) return true; 
      if (name.substring (  name.length()-4 ).equals (".JPG")  ) return true;
      if (name.substring (  name.length()-4 ).equals (".PNG")  ) return true;
      if (name.substring (  name.length()-4 ).equals (".bmp")  ) return true;
      if (name.substring (  name.length()-4 ).equals (".BMP")  ) return true;
      if (name.substring (  name.length()-4 ).equals (".gif")  ) return true;
      if (name.substring (  name.length()-4 ).equals (".GIF")  ) return true;
      // 
      // when no extension matched:   
      return false; // not ok
    } // func 
    //
    
  • Hi Chrisir,

    Your answer here is fantastic! I have one question, can it be possible to set the size of the sketch based on the size of the image? I have tried some different ways, still no clue. I even put the setup function inside the if (selected!=null) and it was funny to find that it cause and infinite loop ;P

    Thanks

  • I don't know how to do this.

    In fact, under the hood there are different inits running for processing when doing size().

    The general idea is that size is the 1st line in setup().

    I tried but I couldn't do it otherwise.

    • You could either scale your images on the hard drive as you need them (all the same size) or even resize them with processing in your program (only in the RAM, not on hard drive).

    • You could set the image in a region of your sketch and leave space for scren buttons or some help texts...

  • maybe there are solutions, sure

    maybe using frames / robots or so....

  • Hi Chrisir!

    thanks for taking the time to think about this... I am trying to find a way to adapt the size of the sketch dynamically because I wont know the size of the picture to be use in the program... Thank you very much for your sugestions

    Regards

Sign In or Register to comment.