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 / April 2007

Tip: Looking for answers? Try searching our database.

ImageIO.write - compression

Thread view: 
Rupert Woodman - 08 Apr 2007 16:06 GMT
Hi,

I have the following code:

String inputFilename = "input.jpg";
String outputFilename = "output.jpg";

BufferedImage bi = ImageIO.read(new File(inputFilename()));
ImageIO.write(bi, "jpg", new File(outputFilename));

The original file is about 1.5 meg in size, the written file is 240kb.  What
I don't understand is what changes have been made to the image (I've not
specified any), and how I can control those changes?

Thank you for any thoughts you may have.

rgds

Rupert
Rupert Woodman - 08 Apr 2007 16:52 GMT
Just to ellucidate a little.  I originally had code such as this shown
below.  Whatever I pass into the setCompressionQuality() method (i.e. any of
0.05, 0.75, 0.95), results in a written file of 811kb, so I assumed this
code was bad.  I don't see why tho.

The BufferedImage passed to this method is created with:

BufferedImage bi = ImageIO.read(new File(c.getInputFileName()));

public void myWrite(BufferedImage bi)
{
 Iterator writers = ImageIO.getImageWritersByFormatName("jpg");
 ImageWriter writer = (ImageWriter)writers.next();
 ImageWriteParam param = writer.getDefaultWriteParam();
 param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
 System.err.println("getCompressionType: " + param.getCompressionType());

 System.err.println("1 getCompressionQuality: " +
param.getCompressionQuality());
 param.setCompressionQuality(0.05f);
 System.err.println("2 getCompressionQuality: " +
param.getCompressionQuality());

 try {
   File ff = new File("c:/development/temp/xxx.jpg");
   ImageOutputStream ios = ImageIO.createImageOutputStream(ff);
   writer.setOutput(ios);
   writer.write(bi);
 } catch (IOException e) {
   e.printStackTrace();
 }
}

Many thanks

> Hi,
>
[quoted text clipped - 15 lines]
>
> Rupert
Knute Johnson - 08 Apr 2007 18:01 GMT
> Just to ellucidate a little.  I originally had code such as this shown
> below.  Whatever I pass into the setCompressionQuality() method (i.e. any of
[quoted text clipped - 50 lines]
>>
>> Rupert

Rupert:

I wrote a very similar method a while back with one difference, I delete
the file if it exists.  I think I did that because of the problem that
you are having.  If the file exists and you write over it it doesn't get
smaller.

Signature

Knute Johnson
email s/nospam/knute/

Rupert Woodman - 08 Apr 2007 19:35 GMT
That's interesting - thanks very much for the tip.
I've done that, but changing the compression quality between the 3 allowed
values (0.5, 0.75 & 0.95) results in the same size file - is that a surprise
to you?  I expected that if I changed the compression quality to 0.95, I'd
have a larger file that if I used a value of 0.05 (I got that by reading the
Java doc on ImageWriteParam)

Many thanks

>> Just to ellucidate a little.  I originally had code such as this shown
>> below.  Whatever I pass into the setCompressionQuality() method (i.e. any
[quoted text clipped - 58 lines]
> are having.  If the file exists and you write over it it doesn't get
> smaller.
Knute Johnson - 08 Apr 2007 20:57 GMT
> That's interesting - thanks very much for the tip.
> I've done that, but changing the compression quality between the 3 allowed
[quoted text clipped - 4 lines]
>
> Many thanks

Now that I look at your code more carefully, you look to be missing some
pieces.  Take a look at the code below.

import java.awt.image.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;
import javax.imageio.stream.*;
import javax.imageio.plugins.jpeg.*;

public class JPEGWriter {
    public static void write(RenderedImage image, float quality,
     File file) throws IOException {
        ImageWriter writer = null;
        Iterator iter = ImageIO.getImageWritersByFormatName("JPEG");
        if (!iter.hasNext())
            throw new IOException("No Writers Available");
        writer = (ImageWriter)iter.next();
        if (file.exists())
            file.delete();
        ImageOutputStream ios = ImageIO.createImageOutputStream(file);
        writer.setOutput(ios);
        JPEGImageWriteParam iwp = new JPEGImageWriteParam(null);
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(quality);
        writer.write(null,new IIOImage(image,null,null),iwp);
        ios.flush();
        writer.dispose();
        ios.close();
    }
}

Signature

Knute Johnson
email s/nospam/knute/

Rupert Woodman - 08 Apr 2007 22:40 GMT
Thank you Knute - I'll read and digest.

Many thanks.

Rgds

>> That's interesting - thanks very much for the tip.
>> I've done that, but changing the compression quality between the 3
[quoted text clipped - 36 lines]
>     }
> }


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.