Are there limitations to images loaded from the web?

ch3ch3
edited April 2016 in JavaScript Mode

I am writing a program in processing 2.2.1 (javaScript mode) that uses data from images. I have most of the functionality I need with images loaded from the data directory. But once I started introducing images from URLs, I've started getting weird behaviors and over the past couple of days I can't pin point the problem.

The first issue appeared when I tried to access the pixels array of a web image. I just couldn't get values. Then a local image would also start to mis-behave by not been accessible. At times the problem seemed to be inconsistent. I put together a test sketch, where I load and display a local and a web image. But when I try to resize them with a keystroke, the program doesn't do anything.

Am I missing something? Should the /* @pjs preload="georgios.jpg"; */ be right above : Img1 = loadImage("georgios.jpg");

How do you preload a web image from a URL that comes from a user textarea field?

And here is my test sketch:

PImage Img1;
PImage Img2;

void setup ()
{
  size( 500, 600, JAVA2D );
  colorMode(RGB,1);
  background(0.18);

// @pjs preload must be used to preload the image 
/* @pjs preload="georgios.jpg"; */
  Img1 = loadImage("georgios.jpg");
/* @pjs preload="http://stylesatlife.com/wp-content/uploads/2015/09/Layered-Hairstyles-for-Round-Faces-4.jpg"; */
  Img2 = loadImage("http://stylesatlife.com/wp-content/uploads/2015/09/Layered-Hairstyles-for-Round-Faces-4.jpg");

  //Img1.resize(200,200);

}


void draw()
{
  background(0.18);


  if( Img1 != null )
    image(Img1, 20, 20);
  else
    text( "Img1 Busted", 20, 20 );

  if( Img2 != null )
    image(Img2, 300, 300);
  else
    text( "Img1 Busted", 320, 320 );


  text( frameRate, 10, 10 );

}


void keyPressed()
{
 if(key=='a')
 {
  Img1.resize(200,200);
 }
 if(key=='z')
 {
  Img1.resize(50,50);
 }

 if(key=='s')
 {
  Img2.resize(200,200);
 } 
 if(key=='x')
 {
  Img2.resize(50,50);
 } 

}

if I remove the comment from Img1.resize(200,200);, the program doesn't run at all, which is weird.

thank you

Answers

Sign In or Register to comment.