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.

Save an Image to a file

Thread view: 
Lee - 11 Jun 2006 02:44 GMT
Starting from an Image object, how would I save it to a jpeg or gif?
Is there an object or method to do this or is it complicated...?
Thanks!
Knute Johnson - 11 Jun 2006 05:06 GMT
> Starting from an Image object, how would I save it to a jpeg or gif?
> Is there an object or method to do this or is it complicated...?
> Thanks!

If you are sure that it isn't actually an instance of BufferedImage then
you need to create a BufferedImage and draw your Image on it.  Then save
the BufferedImage to a file with the ImageIO class.  If it is really a
BufferedImage then just save it.

Signature

Knute Johnson
email s/nospam/knute/

Real Gagnon - 11 Jun 2006 15:55 GMT
> Starting from an Image object, how would I save it to a jpeg or gif?
> Is there an object or method to do this or is it complicated...?
> Thanks!

See http://www.rgagnon.com/javadetails/java-0266.html .

Bye.
Signature

Real Gagnon  from  Quebec, Canada
* Looking for Java or PB code examples ? Visit Real's How-to  
* http://www.rgagnon.com/howto.html

Lee - 11 Jun 2006 17:23 GMT
I want to have this in an applet, and so all the clients would have to
have this class, JIMI.  Does JIMI come with basic Java?

//img is an Image object whose height and width are
public void saveJPG(){
     // Write generated image to a file
        BufferedImage buffered =
makeBufferedImage(img,BufferedImage.TYPE_INT_RGB);
    }
  public static BufferedImage makeBufferedImage( Image image, int type
){
     BufferedImage buffered;
     Graphics2D  g2;
      //buffered = new BufferedImage(width,height,type);

      buffered = new BufferedImage( image.getWidth( null ),
                                 image.getHeight( null ),
                                 type );
      g2 = buffered.createGraphics();
      g2.drawImage( image, null, null );

      return( buffered );

    }

> > Starting from an Image object, how would I save it to a jpeg or gif?
> > Is there an object or method to do this or is it complicated...?
[quoted text clipped - 7 lines]
> * Looking for Java or PB code examples ? Visit Real's How-to
> * http://www.rgagnon.com/howto.html
Real Gagnon - 12 Jun 2006 02:19 GMT
"Lee" <lskatz@gmail.com> wrote in news:1150043017.173433.41070
@u72g2000cwu.googlegroups.com:
> I want to have this in an applet, and so all the clients would have to
> have this class, JIMI.  Does JIMI come with basic Java?

JIMI is a old library for 1.1.x .

The more current library is JAI (java image io api). But for your purpose
JIMI should be good enough and simpler to use.

You can download the JIMI package from http://java.sun.com/products/jimi/

Since you are talking about Applet, I hope that you realize that you won't
be able to save the JPG on the client unless the Applet is signed.

Bye.
Signature

Real Gagnon  from  Quebec, Canada
* Looking for Java or PB code examples ? Visit Real's How-to  
* http://www.rgagnon.com/howto.html

Lee - 12 Jun 2006 21:15 GMT
Yes... what I'm realizing now is that I should somehow change the
BufferedImage to a JPG and then upload it to a server-side program to
save it onto the server.  Thanks for talking this through with me.

> "Lee" <lskatz@gmail.com> wrote in news:1150043017.173433.41070
> @u72g2000cwu.googlegroups.com:
[quoted text clipped - 16 lines]
> * Looking for Java or PB code examples ? Visit Real's How-to
> * http://www.rgagnon.com/howto.html
Lee - 12 Jun 2006 21:46 GMT
So if I have an applet that saves a picture...

Do I need to download a separate package?  This would mean that every
client would need to download a package.  Is there any reasonable image
type that comes with the standard java libraries?

> Yes... what I'm realizing now is that I should somehow change the
> BufferedImage to a JPG and then upload it to a server-side program to
[quoted text clipped - 20 lines]
> > * Looking for Java or PB code examples ? Visit Real's How-to
> > * http://www.rgagnon.com/howto.html
Lee - 12 Jun 2006 23:52 GMT
ugh and now I don't know how to install JIMI
> So if I have an applet that saves a picture...
>
[quoted text clipped - 26 lines]
> > > * Looking for Java or PB code examples ? Visit Real's How-to
> > > * http://www.rgagnon.com/howto.html
Knute Johnson - 12 Jun 2006 23:53 GMT
> So if I have an applet that saves a picture...
>
[quoted text clipped - 25 lines]
>>> * Looking for Java or PB code examples ? Visit Real's How-to
>>> * http://www.rgagnon.com/howto.html

What is it you are really trying to do?  Are you trying to get your
Applet to write to the local disk?  This is going to be problematic.
Are you trying to send an image from the Applet to a server on the
machine that the Applet came from?  If so your server should just read a
stream from the applet.  Convert your BufferedImage to a byte array and
write it to the server.  On the server end if you want to save the
BufferedImage as a JPEG file for example, just write it to disk with the
ImageIO.write().

If either of those options aren't what you are trying to do, just let us
know.
Signature


Knute Johnson
email s/nospam/knute/

Lee - 13 Jun 2006 15:06 GMT
I want:
-The applet to create a jpg in memory from either a BufferedImage,
Image, or Graphics or whatever else.
-The applet to upload the jpg to a program on my server maybe in a POST
request
-My program to process the jpg

I don't really know how to program servlets or JSP, so I just want my
php script to handle the server side, and that's why I probably can't
use a stream.  Or maybe they're compatible somehow and I can do a
stream...?
Also, it seems that every client needs a JPG java package if they want
to use such an applet.  Is this a fair assessment?  If so, I am willing
to look into other image file types.

> > So if I have an applet that saves a picture...
> >
[quoted text clipped - 37 lines]
> If either of those options aren't what you are trying to do, just let us
> know.
Knute Johnson - 13 Jun 2006 17:10 GMT
> I want:
> -The applet to create a jpg in memory from either a BufferedImage,
> Image, or Graphics or whatever else.

Create a BufferedImage and draw on it.

> -The applet to upload the jpg to a program on my server maybe in a POST
> request

You can either do the HTTP connection with Sockets and Streams or use
the HTTPURLConnection class.  Write the BufferedImage to your server
with the ImageIO.write() in JPEG format.

> -My program to process the jpg

I've never used PHP but I have processed form data with Perl.  You
should be able to read the data and write it to a file with no problems
as it will already be in JPEG file format.

> I don't really know how to program servlets or JSP, so I just want my
> php script to handle the server side, and that's why I probably can't
> use a stream.  Or maybe they're compatible somehow and I can do a
> stream...?

I don't either but I don't know why you couldn't use a stream.

> Also, it seems that every client needs a JPG java package if they want
> to use such an applet.  Is this a fair assessment?  If so, I am willing
> to look into other image file types.

I don't think so.  JPEG is just a file format.  The file is still an
array of bytes.  If you send your server an array of bytes that is an
image of a JPEG file then you can just write the bytes to a file and
you're done.

Why don't you write your server in Java?

Signature

Knute Johnson
email s/nospam/knute/

Lee - 13 Jun 2006 18:59 GMT
I don't really know any java... this is kind of my first real program
in it.  My whole site is in php though (which is definitely very
similar to perl although easier to use in my opinion).  This is why it
would be great if I could get the image converted right away before it
reaches the server from the applet.

So what I'd like to know is:

-is it actually true that I would have to make clients download a jpg
java package if I wanted the applet to convert the BufferedImage?

-Do you or does anyone have a nice method that converts a BufferedImage
to jpg or other format?

-how exactly would I use the HTTPURLConnection class or ImageIO class
in that manner?

I am so new to Java that small details will be greatly appreciated.
Thanks!

> > I want:
> > -The applet to create a jpg in memory from either a BufferedImage,
[quoted text clipped - 32 lines]
>
> Why don't you write your server in Java?
Knute Johnson - 13 Jun 2006 23:26 GMT
> I don't really know any java... this is kind of my first real program
> in it.  My whole site is in php though (which is definitely very
[quoted text clipped - 6 lines]
> -is it actually true that I would have to make clients download a jpg
> java package if I wanted the applet to convert the BufferedImage?

No.  They will however have to have a modern version of the Java Runtime
Environment.  Version 1.4 or later.

> -Do you or does anyone have a nice method that converts a BufferedImage
> to jpg or other format?

The ImageIO class has methods that will write a BufferedImage to a file
or stream in JPEG format.

> -how exactly would I use the HTTPURLConnection class or ImageIO class
> in that manner?

You need to look at the docs.  I would download a copy but you can look
at them on the web here:

http://java.sun.com/j2se/1.5.0/docs/index.html

> I am so new to Java that small details will be greatly appreciated.
> Thanks!

This is a pretty sophisticated undertaking for not knowing much about
the language.  You could write your client in C++ too.  I think Java is
easier but that is just my opinion.

Is your Applet going to be some sort of drawing program or ?  I would
start with that part first.  Start writing your Applet and get it to the
point that you can create your JPEG image then worry about how to get it
to your server.  Look at the docs.

Signature

Knute Johnson
email s/nospam/knute/

Lee - 14 Jun 2006 05:14 GMT
Amazingly, I have already created the drawing program at
lskatz.com/java, and I have a "save" button connected to a save method,
which I hope to take in a bufferedimage and upload a jpg to my server
in POST.

Would I use something similar to this function I found?  I already have
a bufferedImage, so I can pick it up from there

public imageConvert() {
       try {
           BufferedImage b = ImageIO.read(new File("img1.gif"));
           ImageIO.write(b,"JPEG",new File("img1.jpg"));
       } catch (IOException e) {
           System.out.println(e);
       }
   }

So I would just use ImageIO.write(b,"JPEG",new File(filename));
And then I will figure out the httpconnection thing.

> > I don't really know any java... this is kind of my first real program
> > in it.  My whole site is in php though (which is definitely very
[quoted text clipped - 35 lines]
> point that you can create your JPEG image then worry about how to get it
> to your server.  Look at the docs.
Oliver Wong - 14 Jun 2006 22:47 GMT
> Amazingly, I have already created the drawing program at
> lskatz.com/java

   Rather than drawing a dot at the mouse location when the button is down,
how about drawing a line from the previous location to the current location,
so there's less of a "leaky, drippy pen" effect?

   - Oliver
Lee - 14 Jun 2006 23:57 GMT
I might come up with other drawing tools on it later to add to the
current three that I have, but I'm leaving it basic for now.  It's a
good idea.

> > Amazingly, I have already created the drawing program at
> > lskatz.com/java
[quoted text clipped - 4 lines]
>
>     - Oliver
jcsnippets.atspace.com - 16 Jun 2006 16:09 GMT
> Starting from an Image object, how would I save it to a jpeg or gif?
> Is there an object or method to do this or is it complicated...?
> Thanks!

Hi Lee,

Don't know whether or not you still need it, but here are two options for
saving an image to jpeg or gif.
http://jcsnippets.atspace.com/java/gui-graphics/create-thumbnail.html
http://jcsnippets.atspace.com/java/gui-graphics/create-thumbnail-with-option
s.html

Best regards,

JayCee
--
http://jcsnippets.atspace.com/
a collection of source code, tips and tricks
Lee - 16 Jun 2006 22:19 GMT
Thanks JayCee, but I am looking to upload the jpg before actually
saving it onto a local hard drive.  The route I am currently trying is
an outputstream and then converting/writing the jpg.
The other option I have considered is creating the jpg in memory,
converting it to a String, and then making a POST request.

> > Starting from an Image object, how would I save it to a jpeg or gif?
> > Is there an object or method to do this or is it complicated...?
[quoted text clipped - 14 lines]
> http://jcsnippets.atspace.com/
> a collection of source code, tips and tricks


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.