Save an Image to a file

L

Lee

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!
 
K

Knute Johnson

Lee said:
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.
 
L

Lee

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 );

}
 
R

Real Gagnon

Lee said:
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.
 
L

Lee

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.
 
L

Lee

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?
 
L

Lee

ugh and now I don't know how to install JIMI
Lee said:
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?
 
K

Knute Johnson

Lee said:
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?

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.
 
L

Lee

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.

 
K

Knute Johnson

Lee said:
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?
 
L

Lee

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!
 
K

Knute Johnson

Lee said:
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?

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.
 
L

Lee

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.
 
L

Lee

Also, for whatever object type the jpg is, would I do the toString()
method in order to make the post request?
 
K

Knute Johnson

Lee said:
Also, for whatever object type the jpg is, would I do the toString()
method in order to make the post request?
No.

If you looked at the docs you would have seen the
ImageIO.write(RenderedImage im, String formatName, OutputStream output).

You need to look at the URL class, the URLConnection class and the
HTTPURLConnection class. There are methods to get the InputStream and
OutputStream (this is what you want) and to set the request method.
Once you have things set up just write the image to the OutputStream.
 
O

Oliver Wong

Lee said:
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
 
L

Lee

OK so I have the following method, and it gives no errors. I will have
to set up the PHP script on the server and will get back to you guys
later on. Thanks for your help so far!

One quick question: why is it not giving an error when I use a
BufferedImage instead of a RenderedImage? Is it casting it
automatically?

//create JPG in memory and upload it
public void saveJPG(){
BufferedImage buffered =
makeBufferedImage(backbuffer,BufferedImage.TYPE_INT_RGB);
String domain = "www.lskatz.com";
String file = "xxxx/xxxx/upload.php";
try{
URL uploadPageURL = new URL("http", domain, 80, file);
HttpURLConnection urlConnection =
(HttpURLConnection)uploadPageURL.openConnection();
urlConnection.setRequestMethod("POST");
OutputStream output = urlConnection.getOutputStream();
ImageIO.write(buffered, "jpg", output); //need RenderedImage
}
catch (IOException e) {}
}
 
L

Lee

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.
 
K

Knute Johnson

Lee said:
OK so I have the following method, and it gives no errors. I will have
to set up the PHP script on the server and will get back to you guys
later on. Thanks for your help so far!

One quick question: why is it not giving an error when I use a
BufferedImage instead of a RenderedImage? Is it casting it
automatically?

//create JPG in memory and upload it
public void saveJPG(){
BufferedImage buffered =
makeBufferedImage(backbuffer,BufferedImage.TYPE_INT_RGB);
String domain = "www.lskatz.com";
String file = "xxxx/xxxx/upload.php";
try{
URL uploadPageURL = new URL("http", domain, 80, file);
HttpURLConnection urlConnection =
(HttpURLConnection)uploadPageURL.openConnection();
urlConnection.setRequestMethod("POST");
OutputStream output = urlConnection.getOutputStream();
ImageIO.write(buffered, "jpg", output); //need RenderedImage
}
catch (IOException e) {}
}

Lee:

That's what I had in mind, yes. I don't know if POST requires any other
header information or not. You appear to know how that works though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top