ImageIO.read() returns null
Author |
Message |
HappyCamper5
|
Posted: Thu Jan 01, 2009 12:52 pm Post subject: ImageIO.read() returns null |
|
|
Right now I have 2 threads, one that constantly takes screenshots and sends it over to the other thread, and the other thread just displays them.
I have a loop that takes a screenshot every 2 seconds, and stores it in a bufferedimage, then I send the image through
Java: | ImageIO.write(image, "gif", oos);
|
oos is an OutputStream.
On the other side, I have
Java: | currentImage=ImageIO.read(ois);
|
where the ois is an InputStream.
The problem I have is that currentImage is still null even after calling the read().
Before the read method, I have the sender to send a byte of data to the receiver telling it that an image has been sent, and it can call the read(). However, this works for the first screenshot sent, and after the read() returns null, even though the write() has been called prior to the invocation of the read().
The code for the sender is:
Java: |
for(int i = 0;;i++ ){
image = capturer. createScreenCapture(rect );
temp = image;
image = resizeImage (image,last, 800, 600);
last = temp;
file = new File("screen"+i+ ".gif");
//System.out.println("writing! "+image.toString());
ImageIO. write(image, "gif", file );
ImageIO. write(image, "gif", oos );
System. out. println("written");
oss. write(77);
System. out. println("Written signal byte");
for(;; ){
System. out. println("waiting for server to read..");
int seeker = (int)ois. read();
if(seeker== 77){
System. out. println("Data has been read");
oos. flush();
break;
}
if(seeker== 88&ℑ!= null){
System. out. println("Sending again");
oos. flush();
ImageIO. write(image, "gif", oos );
}
}
System. out. println("received image confirmation");
if(exit ){
break;
}
}
|
And the code for the receiver is:
Java: |
while(true){
BufferedImage currentImage;
int seeker = sig. read(); //waiting for the sender to send an image
System. out. println("Ready to read!");
currentImage=ImageIO. read(ois );
if(currentImage== null){
for(;; ){
System. out. println("received nothing.. requesting again");
oos. write(88); //teling the server that nothing has been read
currentImage=ImageIO. read(ois );
if(currentImage!= null)
break;
Thread. sleep(500);
}
}
System. out. println("Message Received: " +currentImage. toString());
oos. write(77); //telling the sender that the image has been received
oos. flush();
File f = new File("1.jpg");
if(currentImage!= null){
ImageIO. write(currentImage, "gif", f );
r. update(currentImage );
}
boolean exit= false;
if(exit ){
break;
}
}
|
Mod Edit: Syntax tags are better then quotes for code :) code: | [syntax="java"]Code Here[/syntax] |
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
OneOffDriveByPoster
|
Posted: Sat Jan 03, 2009 9:54 am Post subject: Re: ImageIO.read() returns null |
|
|
The lack of wait() and notify() concerns me.
Check the API docs for Object. |
|
|
|
|
 |
rizzix
|
Posted: Sat Jan 03, 2009 12:48 pm Post subject: RE:ImageIO.read() returns null |
|
|
Take advantage of the Java Concurrent API: java.util.concurrent.Exchanger |
|
|
|
|
 |
|
|