jpeg issues

A

angelochen960

Hi,
I'd like to do following with a jpeg file:

1) obtains the width and height of a jpeg image.
2) reduce the size of a jpeg image, example, its file size is 1MB, i'd
like to reduce it to 500k

any ideas how to achieve these? thanks.

Angelo
 
K

Knute Johnson

Hi,
I'd like to do following with a jpeg file:

1) obtains the width and height of a jpeg image.
2) reduce the size of a jpeg image, example, its file size is 1MB, i'd
like to reduce it to 500k

any ideas how to achieve these? thanks.

Angelo

I have a little utility that I use to save JPEGs with more compression
than you get with ImageIO.write(). It allows you to select the amount
of compression so here's what I suggest.

1) Read the image file with ImageIO you can get the dimensions from the
BufferedImage
2) Use my utility to write the image to a file
3) Check the size of the written file, if it is small enough
you're done if not increase the compression do #2 again

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 {
if (quality < 0.0f || quality > 1.0f)
throw new IllegalArgumentException("0.0 < Quality < 1.0");
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();
}
}
 
T

Tom Anderson

1) obtains the width and height of a jpeg image.

import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class ImageSize {
public static void main(String args) throws IOException{
BufferedImage img = ImageIO.read(new File(args[0])) ;
int width = img.getWidth() ;
int height = img.getHeight() ;
System.out.println("width: " + width, ", height: " + height) ;
}
}

(with thanks to Mark!)
2) reduce the size of a jpeg image, example, its file size is 1MB, i'd
like to reduce it to 500k

I assume that by 'size', you mean file size, rather than image dimensions.
What you need to do is to try writing it out with various compression
levels, until you find one that gives you the size you want. Doing that is
not hugely easy, unfortunately. But, for some reason, i've worked out how
to do it - see here:

http://urchin.earth.li/~twic/Recompress.java

This uses a binary chop to find the compression level. I tried being
clever and doing an interpolation search based on the logarithm of the
output size, but that actually more iterations to find the solution.

You could probably make this faster by doing the test compressions to an
in-memory buffer, which wouldn't be too hard.

tom
 

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
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top