using open()

I start an external .exe with open() but starts an external flat bed scanner scan that takes about 20 seconds

The program continues and before the scan finishes and puts a file in the directory I use to put up image, it gets to the load file statement and can't find it , it's not there yet.

I am trying to use this code to test the file exists before letting the program continue but it complains "unexpected token boolean"

boolean fileExists(String "test.png")

File file = new File("test.png");

while(!file.exists());

Am I on the right tract?

Answers

  • Don't instantiate a File unless you specify the full path. Prefer sketchFile() & dataFile() instead.

    Take notice that exec(), launch() and the like don't block the sketch.
    They create a separate Thread instead.

  • so would this work to test if the file is in the directory yet?

    while(!dataFile("test.png").exists());

    The while would loop until the file existed?

  • I tryed it but it just hangs at the while loop.

  • actually while contains full path

    while(!dataFile("c:\processing\test.png").exists());

    If i use delay(9000) to wait for scanner, it works without above at all. But different scanner setting take more or less time and I don't wanna just set a brute force delay.

  • edited April 2016

    You can invoke a separate function to deal w/ the checking using thread():
    https://Processing.org/reference/thread_.html

    boolean ok;
    
    void setup() {
      thread("fileCheck");
    }
    

    Wrap up the whole check inside an infinite loop + delay():

    void fileCheck() {
      while(!dataFile("c:\processing\test.png").exists())  delay(50);
      ok = true;
    }
    
  • tried this.

    boolean ok; is above setup()

    thread("fileCheck"); is in draw loop, executed when mouse pressed once

    The void FileCheck() is at the end of the sketch file.

    It gives unexpected char:'p' error

  • thread("fileCheck"); is in draw() loop, executed when mouse pressed once.

    If you want that behavior, you're better off using mousePressed() or mouseClicked().

  • In Draw strscan=("c:/acquire/acquire.exe /SCAN/HIDE/GAMMA1.0/C0/GRAY/H6/W8"+strdpi+strxoff+stryoff+" epsproc.bmp");

    open(strscan);

    thread("fileCheck");

    Outside Draw

    void fileCheck() { while(!dataFile("c:\processing\test.png").exists()) delay(50); ok=true; }

    Still don't understand why i get error , unexpected char:'p' error

  • OK got this to run without error. slashes in quotes were wrong "c:/processing/test.png" not "c:\processing\test.png"

    but when thread is called , draw loop continues without waiting for thread call to fileCheck.

    fileCheck finishes and ok=true but program reaches image("test.png",0,0); before file is present still.

    I thought thread would pass control to the thread process draw would wait for it to finish before continuing.

    Am i missing something?

  • A while loop in the draw loop doesn't seem to stop any execution.

    thread(fileCheck); while(ok==false);

    Shouldn't this just loop until ok=true? Then allow the draw function to continue?

  • If i put delay(9000); after the scan, it makes the draw loop wait . This works as a brute force constant way to delay the program but there has to be a more elegant way that will only make it wait until something becomes true right.

  • edited April 2016

    How about going w/ the same approach I did in this forum thread?: *-:)

    https://forum.Processing.org/two/discussion/16097/launch-won-t-work-inside-selectinput-function#Item_3

    /**
     * Await Scanned Image (v1.1)
     * by GoToLoop (2016-Apr-25)
     *
     * forum.Processing.org/two/discussion/16142/using-open
     *
     * forum.Processing.org/two/discussion/16097/
     * launch-won-t-work-inside-selectinput-function 
     */
    
    static final String SCANNED_IMG_PATH = "c:/processing/test.png";
    static final int DELAY = 100, FPS = 1;
    
    PImage  scannedImage;
    boolean imageReady;
    
    void setup() {
      size(200, 150);
      frameRate(FPS);
      thread("checkImageArrived");
    }
    
    void draw() {
      if (scannedImage != null && scannedImage.width == width)
        background(scannedImage);
      else
        background((color) random(#000000));
    
      surface.setTitle("Frame: " + frameCount);
    
      if (imageReady)  loadScannedImage(); // Needs to be last in setup().
    }
    
    void checkImageArrived() { // Happens in another thread.
      final File f = dataFile(SCANNED_IMG_PATH);
      while (!f.exists())  delay(DELAY); // Avoids frying CPU!
      imageReady = true;
    }
    
    void loadScannedImage() { // Happens in the animation thread.
      imageReady = false;
      scannedImage = loadImage(SCANNED_IMG_PATH);
    
      if (scannedImage.width > 0) // Checks whether image is valid.
        surface.setSize(scannedImage.width, scannedImage.height);
      else
        thread("checkImageArrived"); // Bad image! Restart check...
    }
    
  • edited April 2016

    I thought thread would pass control to the thread process draw would wait for it to finish before continuing.

    • Threads aren't aware of other threads! @-) They're completely independent from each other.
    • Although in Java they share the memory heap where objects live, and hence access everything.
    • Only when 1 Thread has access to the reference of some1 else's Thread, it can try to remote-control it though. >-)

    http://Tutorials.Jenkov.com/java-concurrency/index.html

  • Only problem is I don't wanna go out of draw() once its started. The scan is initiated within draw and somehow i need to check when the file arrives on the hard drive so i can access it with other software.

    then the next event does some image processing and shows a report.

    Then draw loops waiting for a mouse pressed event to start the sequence over.

  • Answer ✓
    • You're gonna need to establish some variable whose value specifies which state your sketch is in currently.
    • My example got only 3 states:
      1. Check whether image file has arrived in a separate thread().
      2. loadImage() + setSize() the canvas for it.
      3. Finally display it via background().
    • As you just said above, you're gonna need to add more states like process the image.
    • Then await for reset the whole cycle via mousePressed().
    • Just check out some older forum threads about it for examples:
      https://forum.Processing.org/two/discussions/tagged?Tag=#state
  • using this solution but now i need to try to make the path for SCANNED_IMG_PATH universal to work with any directory path instead of c:\processing\test.png I just want to use test.png and have it find it in the directory the program is started in.

    what would be the syntax for that ? Or can i only specify a specific path with a drive letter? I need it to be drive letter independent.

    Thanks

  • edited March 2017

    Re-read my very 1st reply of almost 1 year ago: Use dataFile() or dataPath().

  • useing datafile() now with SCANNED_IMG_PATH set to just test.png but it does not find it in the starting directory. Windows puts the file in the Virtual Store under AppData automatically it works if i specify a path like c:\processing\test.png so guess i will just have to do that.

  • edited March 2017

    Functions dataFile() & dataPath() point to the subfolder "data/" where the sketch is run.

    For using Processing's own loading functions, we don't need them.
    That is, if you have loadImage("test.png"); and "test.png" is inside subfolder "data/", loadImage() is gonna find it.

    This is how we proceed to have our sketches to be able to load resources no matter its actual path running location.

    Read more about it at loadImage()'s reference: :-B https://Processing.org/reference/loadImage_.html

Sign In or Register to comment.