ImageIO.read() from Socket-stream

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jan Schatz

    ImageIO.read() from Socket-stream

    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
    {
    ImageInputStrea m stream = ImageIO.createI mageInputStream (
    _cSocket.getInp utStream() ); // _cSocket is of class Socket

    image = ImageIO.read( stream );
    }
    catch( Exception e )
    {
    System.err.prin tln( 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.getInp utStream().avai lable() > 0 )
    {
    byte[] b = new byte[2];

    int c = _cSocket.getInp utStream().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
  • Jan Schatz

    #2
    Re: ImageIO.read() from Socket-stream

    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?

    Comment

    Working...