Loading...
Logo
Processing Forum
- I have a program that grabs images from a server that other people submit to.
- It grabs an image from the server & displays that image, then sequentially displays a random image from the web.
- It goes A1, B1, A2, B2, A3, B3, ... " A" being SUBMITTED images, and " B" being random images fetched from google.

The PROBLEM is that if only one " A" image is submitted, ( cat.jpg), the program will display " cat.jpg", then " random1.jpg".
When the program is finished, it starts again, skips past " cat.jpg" as it knows not to display duplicates, and then displays " random2.jpg". It will then just keep on starting over and over again, skipping " cat.jpg" (which it is supposed to do) and displaying random google images, so it ends up being more like A1, B1, B2, B3, B4, etc.

How can I tell the program ONCE IT REACHES THE END, TO STILL RUN, BUT WAIT UNTIL it gets a new A image, and then continue to display a B.

there is allot of code so I guess I will save anyone the headache of reading through it all, but a pseudo mock up of what i want to happen I think would look like this? :

void draw() {
      - add images from server into an array
      - display an image if its not already in the array (avoiding duplicates)

if there are any new images in that array compared to last time{
      - search google for an image
      - display the image from google
}
else just wait until there is a new image
}


..
The part I want to add is in purple...



Replies(2)

Ok I caved, here is the code...
Copy code
  1. //FTP Server grabbing Stuff, Submitted Images
  2. import it.sauronsoftware.ftp4j.*;
  3. import processing.core.PApplet;
  4. import processing.core.PImage;
  5. import javax.imageio.ImageIO;
  6. import java.awt.*;
  7. import java.io.IOException;
  8. import java.net.URL;
  9. import java.util.ArrayList;

  10. //Google Search Stuff, Finding Response Images
  11. String api = "https://www.googleapis.com/customsearch/v1?key=AIzaSyCKUq8jswwqSUbMudpRAyd29CPXJi7xomI&cx=005952954122751681421:cejjy-f9emq&searchType=image&alt=atom&q=";
  12. String[] querys;
  13. String search = api + querys;
  14. PImage webImg;
  15. import processing.pdf.*;

  16. //Printing
  17. import deadpixel.command.*;
  18. long timeToWait = 20000;// in miliseconds
  19. long lastTime;



  20. String imgURL = null;
  21. ArrayList<String> imgURLs = new ArrayList<String>();

  22. FTPClient client;

  23. int checkImg = 0;

  24. ArrayList<PImage> pimages = new ArrayList<PImage>();

  25. public void setup() {
  26.   size(610, 790);
  27.   imageMode(CENTER);
  28.   frameRate(30);
  29.   background(255);
  30.   lastTime = millis();
  31.   
  32.   querys = loadStrings("query.txt");

  33.   client = new FTPClient();

  34.   try {

  35.     client.connect("connorcrawford.com");
  36.     client.login("connorcr", "secrethosting");

  37.   } 
  38.   catch (IOException e) {
  39.     e.printStackTrace();
  40.   } 
  41.   catch (FTPIllegalReplyException e) {
  42.     e.printStackTrace();
  43.   } 
  44.   catch (FTPException e) {
  45.     e.printStackTrace();
  46.   }
  47.   
  48. }

  49. public void draw() {  
  50.   if (checkImg == 100) {
  51.     System.out.println("*******************************find it");
  52.       try {
  53.           FTPFile[] list = client.list("/public_html/upload/*.*");
  54.           System.out.println(list.length);
  55.           for (FTPFile file : list) {
  56.                   if (imgURLs.size() == 0) {
  57.                       imgURLs.add(file.getName().substring(13));
  58.                       }
  59.                       println("dir file: "+file.getName().substring(13));
  60.                       for (int i = 0; i < imgURLs.size(); i++) {
  61.                       String url = (String) imgURLs.get(i);
  62.                       println("array file: "+url);
  63.                               if (!url.equals(file.getName().substring(13))) {
  64.                               // imgURLs.add(file.getName().substring(13)); // 13 is relative to the path we destroy
  65.                               // draw it right away 
  66.                               PImage img =loadImage("http://connorcrawford.com/"+file.getName().substring(13));
  67.                               pimages.add(img);
  68.                               // begin record PDF --> ftp.pdf
  69.                               beginRecord(PDF, "ftp.pdf");
  70.                               background(255);
  71.                               image(img,0, 0); //width/2 height/2 isnt working, WHY
  72.                               endRecord();
  73.                               }
  74.                       }
  75.                  }
  76.       } 
  77.     catch (FTPDataTransferException e) {
  78.       e.printStackTrace();  
  79.     } 
  80.     catch (FTPListParseException e) {
  81.       e.printStackTrace();
  82.     } 
  83.     catch (FTPIllegalReplyException e) {
  84.       e.printStackTrace();
  85.     } 
  86.     catch (IOException e) {
  87.       e.printStackTrace();
  88.     } 
  89.     catch (FTPAbortedException e) {
  90.       e.printStackTrace();
  91.     } 
  92.     catch (FTPException e) {
  93.       e.printStackTrace();
  94.     }
  95.     checkImg = 0;
  96.   }
  97.     checkImg+=1;
  98.     
  99. // send to printer
  100. String fullpath = sketchPath("ftp.pdf");
  101. String yourPrinter = "HP_Deskjet_3000_J310_series"; 
  102. Command c = new Command("lp -o media=letter -o fitplot -d " + yourPrinter + " " + fullpath);
  103. c.run();
  104. println("<><>Ok got an image from server, WAIT to print<><>");
  105. // make a delay here for about 20 seconds...          
  106.              
  107. // do the google search ONCE
  108. try {
  109.      int index =  (int)random(0, querys.length);
  110.      String query = querys[index];
  111.      println(query);

  112.      String combine=api+query;
  113.      println(combine);
  114.      XML data = loadXML(combine);
  115.      XML[] items = data.getChildren("entry/id");
  116.      String urlg = items[0].getContent();//gets what is inside the <tag></tag> 
  117.      // begin record PDF --> google.pdf
  118.      beginRecord(PDF, "google.pdf");
  119.      background(255);
  120.      webImg = loadImage(urlg);
  121.      image(webImg,0,0); // again, why wont the image ever center, top left is the best I can do...
  122.      //translate(610,790);
  123.      endRecord();
  124.      // send to printer
  125.      String fullpath = sketchPath("ftp.pdf");
  126.      String yourPrinter = "HP_Deskjet_3000_J310_series"; 
  127.      Command c = new Command("lp -o media=letter -o fitplot -d " + yourPrinter + " " + fullpath);
  128.      c.run();
  129.     }   
  130.           catch (Exception e) {
  131.           println("FOUND A BAD LINK OR GOOGLE ISNT HAPPY SOMEHOW...");
  132.           int index =  (int)random(0, querys.length);
  133.           String query = querys[index];
  134.           println(query);

  135.           String combine=api+query;
  136.           println(combine);
  137.           XML data = loadXML(combine);
  138.           XML[] items = data.getChildren("entry/id");
  139.           String urlg = items[0].getContent();//gets what is inside the <tag></tag> 
  140.           // begin record PDF --> google.pdf
  141.           beginRecord(PDF, "google.pdf");
  142.           background(255);
  143.           webImg = loadImage(urlg);
  144.           image(webImg,0,0); // again, why wont the image ever center, top left is the best I can do...
  145.           //translate(610,790);
  146.           endRecord();
  147.           // send to printer
  148.           String fullpath = sketchPath("ftp.pdf");
  149.           String yourPrinter = "HP_Deskjet_3000_J310_series"; 
  150.           Command c = new Command("lp -o media=letter -o fitplot -d " + yourPrinter + " " + fullpath);
  151.           c.run();
  152.           }
  153. }           
i'd do work with a boolean timer combination something like, so you don't check every frame:


boolean isNew = false;
timer=0;

in the draw():

if(isNew)
{
also add random image
isNew=false;
}

if(timer>1000){
checkFor new Image. and set the boolean true if you got one.
timer=0;
}

timer++