saving as JPEG

I load a JPEG image, then manipulate it, similar to Adobe Photoshop, but I was wondering how would you save that image after I edit it into a JPEG so it would be nice, also is there any way in JFrame to use my mouse and select an area and have that area be cropped?
[272 byte] By [oesa] at [2007-10-2 17:36:08]
# 1

Enjoy

/*

* Created on Jun 22, 2005 by @author Tom Jacobs

*

*/

package tjacobs.jpeg;

import java.awt.Image;

import java.awt.image.BufferedImage;

import java.awt.image.RenderedImage;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;

import com.sun.image.codec.jpeg.JPEGEncodeParam;

import com.sun.image.codec.jpeg.JPEGImageDecoder;

import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class JPEGUtils {

private JPEGUtils() {

super();

}

public static void saveJPEG(BufferedImage thumbImage, File file, double compression) throws IOException {

BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));

saveJPEG(thumbImage, out, compression);

out.flush();

out.close();

}

public static void saveJPEG(BufferedImage thumbImage, OutputStream out, double compression) throws IOException {

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);

compression = Math.max(0, Math.min(compression, 100));

param.setQuality((float)compression / 100.0f, false);

encoder.setJPEGEncodeParam(param);

encoder.encode(thumbImage);

}

public static BufferedImage getJPEG(InputStream in) throws IOException {

try {

JPEGImageDecoder decode = JPEGCodec.createJPEGDecoder(in);

BufferedImage im = decode.decodeAsBufferedImage();

return im;

} finally {

in.close();

}

}

public static BufferedImage getJPEG(File f) throws IOException {

InputStream in = new BufferedInputStream(new FileInputStream(f));

return getJPEG(in);

}

public static void saveGif(RenderedImage image, File f) throws IOException {

saveGif(image, new FileOutputStream(f));

}

public static void saveGif(RenderedImage image, OutputStream out) throws IOException {

//Last time I checked, the J2SE 1.4.2 shipped with readers for gif, jpeg and png, but writers only for jpeg and png. If you haven't downloaded the whole JAI, and you want a writer for gif (as well as readers and writers for several other formats) download:

//

//http://java.sun.com/products/java-media/jai/downloads/download-iio.html

ImageIO.write(image, "gif", out);

}

public static void main(String[] args) {

// TODO Auto-generated method stub

}

}

tjacobs01a at 2007-7-13 18:53:25 > top of Java-index,Java Essentials,Java Programming...
# 2
well this is really interesting, I really don't understand though how anything is working
oesa at 2007-7-13 18:53:25 > top of Java-index,Java Essentials,Java Programming...
# 3

Image IO is done via the [url=http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/package-frame.html]javax.imageio [/url] package.

For basic operations [url=http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageIO.html]javax.imageio.ImageIO[/url] is all you need, it has a [url=http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageIO.html#read(java.io.File)]read[/url] and a [url=http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageIO.html#write(java.awt.image.RenderedImage,%20java.lang.String,%20java.io.File)]write[/url]operation, for reading it works out the Image type (e.g. jpeg, gif). For writing, the write methods take a formatName param. This can be "jpeg" or "png" (etc, the full list depends on what is installed, and can be retrived from the ImageIO class).

> JFrame to use my mouse and select an area

> and have that area be cropped?

To open.

Do you know how to display an image on a frame?

Do you know how to capture mouse events?

Do you know how to edit images? (for this I would guess it would involve creating a new image, setting the size to the cropped size, and drawing the old image, with the required offsets).

mlka at 2007-7-13 18:53:25 > top of Java-index,Java Essentials,Java Programming...
# 4
Also take a look at http://www.universalwebservices.net/web-programming-resources/java/adjust-jpeg-image-compression-quality-when-saving-images-in-java/
lan99a at 2007-7-13 18:53:25 > top of Java-index,Java Essentials,Java Programming...