Problems reading a byte stream:(
Hi
i am quite new in Java and i have this problem! i am trying to read a remote-sensed image and display it. to make things more clear:
The image i am using is a remote sensed image 512 * 512 . It has three bands, green, red, nir.The data is 8-bit (single byte) and in a binary byte format. The data are written as after the header as a continuous byte stream (band sequential). the header consists of 4 fields
carriage return delineated. The end of the header is recognized by a carriage return, period (.), carriage return - the image data follows as a continuous byte stream band sequential. The format
of the header is as follows:
Bands (int)
Rows (int)
Cols (int)
Bits-per-pixel (int)
therefore i have to read the header separately from the image data (i havent done this before) and then read the image data as one byte at a time and i think i have to store the values (0-255) in 3 arrays because i have to display 3 images ( because of the 3 bands). i think i have to tell the program to read the first 512 bytes and store them into an array and then display it as an image. then read the other 512 and so on. Any ideas about the code? I would really appreciate it ! Please help me !!!!!!!!
Cheers
Maria
[1301 byte] By [
mayra] at [2007-9-26 1:24:31]

Do you get all these numbers as strings or as binary data ? Do you get the end-of-header chars as bytes(8-bit) or as unicode characters(16-bit)
Suppose you have already the InputStream you want to read the image from and you are expected to return an Image object. You could have the following methods:
class ImageData {
int bands;
int rows;
int cols;
int bitsPerPixel;
}
class ImageGetter {
...
public Image readImage( InputStream in ) throws Exception {
ImageData data = readHeader( in );
return extractImage( in, data );
}
private ImageData readHeader( InputStream in ) throws Exception {
ImageData data = new ImageData();
// Read the header data, assuming that the numbers were written in digital format using writeInt().
DataInputStream dis = new DataInputStream( in );
data.bands = dis.readInt();
data.rows = dis.readInt();
data.cols = dis.readInt();
data.bitsPerPixel = dis.readInt();
// Now check if we have a proper end-of-header sequence (".\r.")
// Assuming that the characters were written as unicode(16 bit). You may have to use readByte() instead
if( ( dis.readChar() != '.' ) ||
( dis.readChar() != '\r' ) ||
( dis.readChar() != '.' ) )
throw new Exception( "Bad format: end-of-header expected !" );
return data;
}
private Image extractImage( InputStream in, ImageData data ) throws Exception
{
// I have to invest too much time in order to do this as well, sorry ...
// See in the APIs what an image should do and good luck ...
}
igarn at 2007-6-29 1:04:42 >
![top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...](/library/images/10/top.gif)