Send Image over Socket

Hi all,

I'm trying to send images over a socket from a laptop to a PDA. So far I've tried converting the image into a two dimensional array of ints, using BufferedImage.getRGB(). I then drew the integer pixel values directly on the PDA display's graphics object using fillRect(). However, transmission speed and display speed were incredibly slow in this situation.

Right now I'm trying to use a FileInputStream to convert a JPEG into a byte[ ]. I send the byte[ ] over the socket, with quick transmission speed, and use the awt Toolkit.createImage() method to generate an Image from the byte[ ]. However, when I call drawImage, nothing shows up on the display.

The code I use to generate the byte[ ] from a JPEG looks like this:

byte[] ImageData =newbyte[*Number ofbyte in jpeg image*];

File imageFile =new File(*Image File Name*);

FileInputStream fileIn =new FileInputStream(imageFile);

fileIn.read(MapImageData);

This code is supposed to read a JPEG image into the byte array ImageData.

The code I use to create the Image object from the byte[ ] on the other side of the socket looks like this:

//The PDA display has received the byte[ ]

Image image = kit.createImage(*Byte[ ] from socket*);

System.out.println("Image synthesized from byte[]");

//Send image to the panel and draw

panel.newImage(image);

panel.paintComponent(panel.getGraphics());

//The panel's paintComponent method calls drawImage()

To test if the byte[ ] was being transmitted correctly, I printed it out on both sides of the socket and saw the same data. The problem is either taking place when I read the JPEG image into the byte[ ] or when I call Toolkit.createImage to generate an Image from the byte[ ].

Does anyone know how to do this correctly?

[2168 byte] By [DannyB314a] at [2007-10-2 10:50:19]
# 1

I'd start by making very sure the image is transferred correctly. In the receiver loop over the bytes and print them to a debugging stream. Take a hex dump of the file somehow and compare each byte and the length of the byte[] vs file size.

How do you read from the socket? Are you aware you need to use some sort of readFully() or loop with read() because read() can deliver the data in pieces? And you are using Streams and not Readers/Writers which are for characters, not bytes?

A quick hack to test the drawing code: take the hex dump of the file, hardcode the data in the receiving program, and draw that:

byte image[] = new byte[] { 11, 32, 0, 14, ... };

sjasjaa at 2007-7-13 3:08:14 > top of Java-index,Archived Forums,Socket Programming...
# 2

Thanks for the help. I read in the image file in bytes, without sending it over a socket, and awt.Toolkit still couldn't generate the image using createImage(byte[ ]).

The next option I tried was putting the image byte[ ] into a ByteArrayInputStream, and used ImageIO.read(InputStream) to generate the image from the byte[ ], which worked fine. I don't think the socket communication was the problem, since I printed out the byte array on both sides of the socket, and saw the same numbers. awt.Toolkit.createImage() wasn't creating the Image object correctly from the byte array. imageio.ImageIO.read(InputStream) works fine with a ByteArrayInputStream. The code for generating an image from a byte[ ] looks like this:

ByteArrayInputStream imStream = new ByteArrayInputStream(imageData); // imageData is the byte[]

Image image = (Image)ImageIO.read(imStream);

To conclude, everything works fine if I use ImageIO to generate the Image. The only problem is that the IBM J9 WEME doesn't contain the javax.imageio package. Do you know if there's a way to add that package to J9 on the PDA? Otherwise, I'm going to have to find some other way to generate the image from the byte[ ], without using ImageIO.

Thanks again.

DannyB314a at 2007-7-13 3:08:14 > top of Java-index,Archived Forums,Socket Programming...