14

I need to reduce the size of image(not the width and height) using Java program. Is their any good API available for this?

I need to reduce the size from 1MB to about 50kb - 100 kb's. Of-course the resolution will decrease but that doesn't matter.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111

4 Answers4

10

According to this blog post: http://i-proving.com/2006/07/06/java-advanced-imaging/ you can use the Java Advanced Imaging Library to do what you want. The following sample code should provide you a good starting point. This will resize the image, both in height and width, as well as image quality. Once your image is of the desired file size, you can scale it back up to your desired pixel height and width when you display the image.

// read in the original image from an input stream
SeekableStream s = SeekableStream.wrapInputStream(
  inputStream, true);
RenderedOp image = JAI.create("stream", s);
((OpImage)image.getRendering()).setTileCache(null);

// now resize the image

float scale = newWidth / image.getWidth();

RenderedOp resizedImage = JAI.create("SubsampleAverage", 
    image, scale, scale, qualityHints);


// lastly, write the newly-resized image to an
// output stream, in a specific encoding

JAI.create("encode", resizedImage, outputStream, "PNG", null);
Community
  • 1
  • 1
ironchefpython
  • 3,409
  • 1
  • 19
  • 32
7

This is the working code

public class ImageCompressor {
    public void compress() throws IOException {
        File infile = new File("Y:\\img\\star.jpg");
        File outfile = new File("Y:\\img\\star_compressed.jpg");

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
                infile));
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(outfile));

        SeekableStream s = SeekableStream.wrapInputStream(bis, true);

        RenderedOp image = JAI.create("stream", s);
        ((OpImage) image.getRendering()).setTileCache(null);

        RenderingHints qualityHints = new RenderingHints(
                RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);

        RenderedOp resizedImage = JAI.create("SubsampleAverage", image, 0.9,
                0.9, qualityHints);

        JAI.create("encode", resizedImage, bos, "JPEG", null);

    }

    public static void main(String[] args) throws IOException {

        new ImageCompressor().compress();
    }
}

This code is Working Great for me. if you need to resize the image then you can change the x and y scale here JAI.create("SubsampleAverage", image, xscale,yscale, qualityHints);

Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
  • 1
    Great Answer Young man. But can i increase the dpi level of this image of is there any way to do this ? – Buntylm Jan 28 '13 at 09:11
0

You can use JAI to increase the compression of a written JPEG without doing any scaling. See http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Encode.doc.html

Atrus
  • 788
  • 4
  • 13
  • 34
0

If your image type is supported by an implementation of ImageWriteParam, you can adjust the quality as shown in this example. Other ImageWriteParam methods, such as getBitRate(), may allow you to optimize the result.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I tried that example and its not working. it is creating even bigger image size when i pass any float value except 1.0f. and when i pass 1.0f it is creating a file with low size but the quality is tooooooo low or i can say the image is simply lost. – Adil Shaikh Jun 17 '11 at 21:28
  • You might look at the available quality descriptions and values; the meaning varies by format. BTW, what format are you using? – trashgod Jun 18 '11 at 04:25