Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

problems with ImageIO

Thread view: 
Ron - 23 Jun 2006 02:33 GMT
The following code does not work ("file" and "image" are members of the
class; nextNumber(InputStream) returns the next number in the
InputStream):

public void write(PrintStream stream) throws IOException {
  FileInputStream in = new FileInputStream(file);
  int size = in.available();
  stream.println(size);
  byte[] b = new byte[in.available()];
  in.read(b);
  stream.write(b, 0, size);
  in.close();
}

public void read(InputStream stream) throws IOException {
  int size = (int)nextNumber(stream);
  byte[] buf = new byte[size];
  stream.read(buf, 0, size);
  ByteArrayInputStream imageStream = new ByteArrayInputStream(buf);
  image = ImageIO.read(imageStream);
}

"image" is null when I run this code. Suffice to say, the code
"ImageIO.read(file)" works. Can anyone see a problem with this? Thanks.

Ron
Knute Johnson - 23 Jun 2006 03:18 GMT
> The following code does not work ("file" and "image" are members of the
> class; nextNumber(InputStream) returns the next number in the
[quoted text clipped - 22 lines]
>
> Ron

From the docs:

"ImageIO.read(InputStream is)

Returns:
    a BufferedImage containing the decoded contents of the input, or null."

Signature

Knute Johnson
email s/nospam/knute/

Andrey Kuznetsov - 23 Jun 2006 07:01 GMT
> The following code does not work ("file" and "image" are members of the
> class; nextNumber(InputStream) returns the next number in the
[quoted text clipped - 9 lines]
>   in.close();
> }

what do you want with PrintStream?
note also that available gives you only how much bytes may be read without
blocking, NOT full size of data behind stream.

> public void read(InputStream stream) throws IOException {
>   int size = (int)nextNumber(stream);
[quoted text clipped - 3 lines]
>   image = ImageIO.read(imageStream);
> }

similar problem here: InputStream.read(buf, 0, size) returns how much byte
were read.
Use BufferedInputStream.readFully().

BTW why don't you create image direct from supplied stream?

public void read(InputStream stream) throws IOException {
   image = ImageIO.read(stream);
}

Andrey

Signature

http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

Ron - 23 Jun 2006 12:32 GMT
> note also that available gives you only how much bytes may be read without
> blocking, NOT full size of data behind stream.

I'm sure that it's giving me the full size of the file. It returns 1283
and the size of the file is 1283.

> BTW why don't you create image direct from supplied stream?

Because the stream has more data than that of the image. I'm developing
a graphics program that has the ability to import images. I want the
files that my program to generate to imbed these images within the
file. So, the files look like this:

File header
Object 1 data...
Object 2 data...
Imbedded image header
[imbedded image data]
Object 3 data...
...
Ron - 23 Jun 2006 13:12 GMT
Nevermind... the problem was that I wasn't reading in the newline
character created after stream.println(size) before I did in.read(b).
It works perfectly now. Thanks for the help anyway.

> > The following code does not work ("file" and "image" are members of the
> > class; nextNumber(InputStream) returns the next number in the
[quoted text clipped - 38 lines]
> http://reader.imagero.com Java image reader
> http://jgui.imagero.com Java GUI components and utilities


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.