By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,034 Members | 819 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,034 IT Pros & Developers. It's quick & easy.

ImageIO.read() from Socket-stream

P: n/a
Hi,

I have an application sending JPEGs via a TCP connection.
By now I used a Delphi program on the other side to receive the pics.

Now I want to create a Java applet that does the work of the Delphi
program, but I have problems reading the pics from the socket's
stream.
Here's my code:

8<--------------------------------------------------

public BufferedImage getImage()
{
BufferedImage image;

try
{
ImageInputStream stream = ImageIO.createImageInputStream(
_cSocket.getInputStream() ); // _cSocket is of class Socket

image = ImageIO.read( stream );
}
catch( Exception e )
{
System.err.println( e );
}

return image;
}

-------------------------------------------------->8

The function returns null. There seems to be no appropriate
ImageReader.
Now, when I add the following lines to the function, it works fine:

8<--------------------------------------------------

while( _cSocket.getInputStream().available() > 0 )
{
byte[] b = new byte[2];

int c = _cSocket.getInputStream().read( b );

if( b[0] == 0xFF && b[1] == 0xD9 ) // FF D) are the last two byte
of a JPEG
break;
}

-------------------------------------------------->8

But that's not exactly what I wanted. I guess it skips every second
pic.
Can anybody explain this phenomenon.

My setup:
Windows 2000
Java 1.4.2_02
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
I've figured out, that ImageIO.read(); takes too many bytes from the
stream.
The first read returns a valid image, but the second time the read
call doesn't find a valid JPEG at the beginning of the stream, because
the previous read took some bytes from the next image. So, when I skip
all bytes until the next FF D9 (End of JPEG) appears, the next read
will succeed too.

Is that a bug in the ImageIO.read() function? Or am I doing something
wrong?
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.