Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decrease image resolution in java

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.

like image 261
Mohammad Adil Avatar asked Jun 17 '11 19:06

Mohammad Adil


People also ask

How do I make an image smaller in Java Swing?

Just do: Image newImage = yourImage. getScaledInstance(newWidth, newHeight, Image. SCALE_DEFAULT);

Can you resize an image in Java?

You can resize an image in Java using the getScaledInstance() function, available in the Java Image class. We'll use the BufferedImage class that extends the basic Image class. It stores images as an array of pixels.

What is resize in Java?

Resize(int size) Creates a Resize Transform that resizes to the given size. Resize(int width, int height) Creates a Resize Transform that resizes to the given width and height.


2 Answers

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);
like image 55
ironchefpython Avatar answered Oct 03 '22 08:10

ironchefpython


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);

like image 41
Mohammad Adil Avatar answered Oct 03 '22 08:10

Mohammad Adil