We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Images from google
Page Index Toggle Pages: 1
Images from google (Read 877 times)
Images from google
Dec 3rd, 2005, 12:42pm
 
I'm fairly new to processing and programming in general, and I'm
having some trouble in getting a program to work ( no surprises
there, I suppose. )

The bit of code I'm working on works as it's supposed to: loading
random image urls from google images to strings and using
loadimage to put them in the application pixels buffer.

The damn thing throws ArrayIndexOutOfBoundsException : 1
( jawa.awt.image.PixelGrabber.setPixels )  at me since I put
the loadImage() inside draw.

But for the program to work at all I need to use it, or something
similar inside draw. Any way to around this? Making connections
with the client object and getting the size of the .jpg:s to check when
they're done? I checked dev.processing.org for solutions, but couldn't
really find anything. ( Because I'm new. I also hope this is the right
forum to ask this. )

Suggestions? Help? Help!
Re: Images from google
Reply #1 - Dec 3rd, 2005, 6:30pm
 
that looks like the image isn't loading properly, that you have the url wrong or that it's not really image data that comes back. though it's impossible to say without seeing the code.
Re: Images from google
Reply #2 - Dec 5th, 2005, 1:56pm
 
Well, here's the code, rather sloppy but...

At this point it just finds random images and
puts them on screen. Haven't added the bits
of code that would modify them yet.


Code:
import processing.net.*; 
import java.util.regex.*;
import java.io.*;

private static Pattern pattern;
private static Matcher matcher;

int kuvanro = 0;
String kuva = "0";
String kuvatus;
boolean toimii = false;
Client google;
String myHost = "images.google.fi";
String google1 = "/images?hl=fi&q=dsc0";
String google2 = ".jpg%20filetype%3Ajpg&btnG=Google-haku&sa=N&tab=wi";
String google3;
String google4;
int check = 1;
boolean check2 = false;

PImage vkuva;


void setup() {
size(800, 600);
background(50);
loadPixels();
hae();
}


void hae() {

if(check == 1){
kuvanro = int(random(0, 9999));
if( kuvanro <= 9 ){
kuva = "000" + char(kuvanro);
}
if( kuvanro <= 99 ){
kuva = "00" + str(kuvanro);
}
if( kuvanro <= 999 ){
kuva = "0" + str(kuvanro);
}
if( kuvanro >= 1000 ){
kuva = str(kuvanro);
}

google3 = google1+kuva+google2;
google = new Client(this, myHost, 80);
println("trying to fetch from "+myHost);
// Send the HTTP GET request:
google.write("GET "+google3+" HTTP/1.1\n");
google.write("HOST: "+myHost+"\n\n");
check = 0;
check2 = true;
google4 = "0";

}

while(check2){
if(google.available()>1){
google4 += google.readString();
}

if(google.available()<1 && google4.length() > 20000){
check2 = false;
}

}

pattern = Pattern.compile("<img src=/images.*?>");
matcher = pattern.matcher(google4);

while(matcher.find()){
toimii = false;
kuvatus = matcher.group().substring(36, matcher.group().lastIndexOf(119)-1);
println("getting an image from: "+kuvatus.substring(0, kuvatus.indexOf(47)));
try{
vkuva = loadImage("http://"+kuvatus);
toimii = true;
} catch(RuntimeException e) {
println("failed to load image from: "+kuvatus.substring(0, kuvatus.indexOf(47)));
}

if(toimii){
println("displaying image: "+"http://"+kuvatus);
image(vkuva, 0, 0);
}
}

check = 1;
google.stop();

}
Re: Images from google
Reply #3 - Dec 5th, 2005, 4:49pm
 
These is what the references say:
To load correctly, images must be located in the data directory of the current sketch.
Re: Images from google
Reply #4 - Dec 5th, 2005, 6:09pm
 
try printing the url that you're trying to grab, and then go see if that's a valid url in the browser.

also, loadImage() no longer throws a RuntimeException (it'll just reutnr null) if it doesn't load.
Re: Images from google
Reply #5 - Dec 7th, 2005, 4:06pm
 
The urls are valid.
Re: Images from google
Reply #6 - Dec 14th, 2005, 2:44pm
 
Hi Lauri,

I modified a bit your code, and it works now at least on my computer with Processing 0095. I tweaked mostly other parts of the code and moved the image drawing to draw()-method.

-Janne

import java.util.regex.*;

private static Matcher matcher;

String searchStart = "http://images.google.fi/images?hl=fi&q=dsc0";
String searchEnd   = ".jpg%20filetype%3Ajpg&btnG=Google-haku&sa=N&tab=wi";  

void setup() {  
 size(800, 600);  
 background(50);
 fetchLinks();
}

void draw() {
 drawNextImage();
}

void fetchLinks() {
 NumberFormat formatter = new DecimalFormat("0000");
 String imageName = formatter.format(random(0, 9999));
 String pageContent;

 try {
   URL url = new URL(searchStart+imageName+searchEnd);
   pageContent = webFetch(url);
 } catch(Exception e) {
   println("URL could not be formed: "+e);
   return;
 }
 
 Pattern pattern = Pattern.compile("<img src=/images.*?>");
 matcher = pattern.matcher(pageContent);
}


void drawNextImage() {
 matcher.find();
 String tmpUrl = matcher.group();
 String imageUrl = "http://"+tmpUrl.substring(36, matcher.group().lastIndexOf(119)-1);
   
 println("fetching image: "+imageUrl);
 try {
   PImage currImage = loadImage(imageUrl);
   println("displaying image: "+imageUrl);
   image(currImage,0,0);
 } catch(RuntimeException e) {
   println("error fetching url: "+imageUrl);
 }
}

private static String webFetch(URL url) throws Exception {
 try {
   URLConnection con = url.openConnection();
   con.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 5.21; Mac_PowerPC)" );
   InputStream content = con.getInputStream();
   StringWriter writer = new StringWriter();

   for(int c = content.read(); c!= -1; c = content.read()) {
     writer.write(c);
   }
   return writer.toString();
 } catch( Exception e ) {
   throw e;
 }
}
Page Index Toggle Pages: 1