Re: jpeg issues
On Tue, 22 Jul 2008, angelochen960@gmail.com wrote:
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
--
Sometimes it takes a madman like Iggy Pop before you can SEE the logic
really working.