loading image
in
Programming Questions
•
3 years ago
Hi,
I wrote a prgram that was suposed to take sreenshots and get the avrage RGB value of each shot on the fly.
The sreenshot taking is done in jave and the image loading and RGB calculations in processing.
The RGB calculation is not actualy implemented yet but i have two loops that go trugh the image and get the RGB colors.
I'm stuck at the image loading part.
I got my sreenshot taking set up so the images have names 1,2,3 and so on depending on the seqence they are taken
now the processing code takes the images on by one.
The code works for the first image. it runs perfectly and loads 1.jpg and gets the RGB values.
On the next loop it throws and exeption that 2.jpg is not there or something like that.
But looking at the folder i actualy see the 2.jpg there and i can open it and its fine realy..
Sine the program should be sequential running line after line i don't think i'm trying to load the image before i reated one.
But it seems to happen anyway.
Any ideas why and how to solve it ?
Thanks
code
I wrote a prgram that was suposed to take sreenshots and get the avrage RGB value of each shot on the fly.
The sreenshot taking is done in jave and the image loading and RGB calculations in processing.
The RGB calculation is not actualy implemented yet but i have two loops that go trugh the image and get the RGB colors.
I'm stuck at the image loading part.
I got my sreenshot taking set up so the images have names 1,2,3 and so on depending on the seqence they are taken
now the processing code takes the images on by one.
The code works for the first image. it runs perfectly and loads 1.jpg and gets the RGB values.
On the next loop it throws and exeption that 2.jpg is not there or something like that.
But looking at the folder i actualy see the 2.jpg there and i can open it and its fine realy..
Sine the program should be sequential running line after line i don't think i'm trying to load the image before i reated one.
But it seems to happen anyway.
Any ideas why and how to solve it ?
Thanks
code
- PImage img;
void setup() {
size(900, 500);
int r=0;
int g=0;
int b=0;
int height=700;
int width=500; - //Trying to take 5 sreenshots and calculate the RGB. (5 for testing, it will be a while(true) later)
for(int k =1; k< 5; k++) {
try {
//the code that takes the sreenshot and puts it in the folder naming it k.jpg
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
ImageIO.write(image, "jpg", new File("D:/Processing Projects/Img_workout/"+k+".jpg"));
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
img = loadImage(k+".jpg");
img.loadPixels(); /*the part where the program throws an exception saying it can't read the sorce or the data is not valid. I presume that the image was sucesfuly loaded since the ode above was procesed */
//going trugh the image loaded (every 20 pixels otherwise it takes too much time)
for (int y = 0; y < height; y=y+20) {
for(int x = 0; x <width; x=x+20) { - //will later calculate the avrage RGB value of the image
int col = img.pixels[y*width+x];
r = (col >> 16)&0xff;
g = (col >> 8) &0xff;
b = (col)&0xff;
}
}
System.out.println("RED: "+r+" GREEN "+g+" BLUE "+b);
System.out.println("done!");
}
}
1