473,396 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
{
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
1 8181
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Aaron Davies | last post by:
I'm trying to write some classes for my collaborative whiteboard to transmit images over the network, using the javax.imageio API, but I'm running into an odd problem: my code words fine with PNGs,...
1
by: Patrick | last post by:
Hello all! I am using a BufferedImage object to build an image from scratch. I want it to be a grayscale image with only 8bits of color. I have the color information as a byte, saved in variable...
0
by: Rajarshi Guha | last post by:
Hi, I'm writing some code using PyGTK2 that uses inpu_add_full() to watch a socket. I've included the code below. I have two questions: 1) What does a condition == 16 mean? It does match any of...
0
by: Tomasz Naumowicz | last post by:
Hello Everybody, I have the following problem with Socket: Socket.Available reports data in the buffer (e.g. 1849 bytes) but I can't read it because Socket.Read throws an exception The...
0
by: phplasma | last post by:
Hey, I am currently attempting to implement a multi-threaded C# socket, using SSL (.pem file/certification/private key combo) server using Visual Studio C# Express. I have successfully made...
5
by: Steven Blair | last post by:
I need to write an ASP.NET application which can do the following: 1. Create a socket which will stay alive and continously read data. 2. The data read needs to be displayed on the webpage. 3....
5
by: Jens | last post by:
Hello, I have been looking for some C-code which listens on a user-defined port for incoming data traffic. When data is received, the data is written to a file. I found some C-code (server)...
5
by: Robert Larsen | last post by:
Hi group I have some code that saves an image to a file using ImageIO. And it works for all formats except jpeg. A scaled down version follows: public void write(String filename) throws...
0
by: Jean-Paul Calderone | last post by:
On Sat, 23 Aug 2008 02:25:17 +0800, Leo Jay <python.leojay@gmail.comwrote: No - it's just what I said. create_socket creates one socket and passes it to read_socket and write_socket. ...
0
by: ne0lithic | last post by:
Hey everyone, I'm working on a program based on steganography. For this, i need to access the individual pixels and perform my manipulations. FYI, I do not have the JAI library available with me...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.