How to write JPEG Images in Java

In this article I will show you how to write JPEG Images in Java using imageIO, JAI and our new JDeli Image Library. Firstly I will start with explaining what is JPEG.

What is JPEG?

JPEG stands for "Joint Photographic Experts Group,” and is a file format for compressed images. It uses lossy compression for digital images and is best for saving photographs and paintings of realistic scenes with smooth variations of tone and color (where you do not notice the missing details).

Because JPEG images are not limited to a certain amount of color and contain colorful, high-resolution image data, JPEG is frequently used for storing and transmitting photographic images on the Web and is one of the most common format saved by digital cameras. There is a tradeoff between size and quality.

The file name extensions for JPEG files are: .jpg and .jpeg.

Example Java code to write JPEG images in Java:

Here are some code examples using Image IO, JAI and JDeli to save a BufferedImage to a JPEG

– Using Image IO

Javadoc and included in JDK

//Write Image (can also be OutputStream)
File myNewJPegFile=new File("ImageAsJPeg.jpg");
ImageIO.write(myBufferedImage, "jpg", myNewJPegFile);

– Using JAI
Javadoc and Download

//Write Image
File myNewJPegFile=new File("ImageAsJPeg.jpg");
JAI.create("filestore", myBufferedImage, myNewJPegFile, "JPEG");

– Using JDeli

Javadoc and Download (Supports quality settings)

//Write Image
OutputStream os = new FileOutputStream(new File("ImageAsJPeg.jpg"));
JpegEncoder encoder = new JpegEncoder(); 
encoder.write(myBufferedImage, os);
os.flush();
os.close();

We have also created a useful how to series on how to read and write various Image formats.

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (Twitter, Facebook and Google+) or the Blog RSS.

 

Related Posts:

The following two tabs change content below.
Sylwia is a developer at IDR Solutions working on JavaFX Development and on the NetBeans JavaFX Plugin. She is very passionate about programming and aims to improve constantly.
Sylwia

About Sylwia Dorota Kedzia

Sylwia is a developer at IDR Solutions working on JavaFX Development and on the NetBeans JavaFX Plugin. She is very passionate about programming and aims to improve constantly.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>