Home » Programming » Java » How to Reduce Image memory size using Java

How to Reduce Image memory size using Java

Image is playing a vital role in websites, web apps, and mobile apps. When we develop any kind of website image is most important to describe things easier than words and making good look and feel of the website.

But when comes to memory size image is on the higher side than the word right!. Here memory size is most important for website development. So You want to use the images with your website at the same time it won't affect your website loading speed.

For this situation, you should reduce the image memory size to increase the loading speed of websites. From an SEO point of view also image plays a vital role in website development. So avoiding images is not a good thing but using images with reduced memory size is good for any kind of website.

Let me explain here how to reduce the image memory size without affecting its quality by using Java.

Java program for reducing memory size of an image

The below snippet is used to reduce the image memory size without affecting its resolution.

package com.tipstocode.packages;
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ResizeImageUtil {	
	
public static BufferedImage resizeImageWidthChange(BufferedImage originalImage,int convertImageWidth, int convertImageHeight, int type){    	
    	BufferedImage resizedImage = new BufferedImage(convertImageWidth, convertImageHeight, type);
    	//Rendering image with reduced memory size
    	Graphics2D g = resizedImage.createGraphics();
    	g.drawImage(originalImage, 0, 0, convertImageWidth, convertImageHeight, null);
    	g.dispose();
    	g.setComposite(AlphaComposite.Src);
    	g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
    	RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    	g.setRenderingHint(RenderingHints.KEY_RENDERING,
    	RenderingHints.VALUE_RENDER_QUALITY);
    	g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    	RenderingHints.VALUE_ANTIALIAS_ON);    	
    	return resizedImage;    	
    }
public static BufferedImage resizeImageWithHint(BufferedImage originalImage, int type){	
	int imageHeight=originalImage.getHeight();
	int imageWidth=originalImage.getWidth();	
	BufferedImage resizedImage = resizeImageWidthChange(originalImage,imageHeight,imageWidth,type);
	return resizedImage;
}
public static void main(String[] args) throws IOException {		
	//Image file location in system
	File file = new File("F:/resize/input.jpg");
 	BufferedImage originalImage =  ImageIO.read(file);					 							 		
 	int typeOfImage = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
 	BufferedImage resizeImageJpg = ResizeImageUtil.resizeImageWithHint(originalImage, typeOfImage);
 	if(resizeImageJpg!=null){
 	//Output location of image
	 ImageIO.write( resizeImageJpg, "jpg", new File("F:/resize/output.jpg") );
	 System.out.println("Image with reduced memory size saved in the location"); 	
 	}
   }
}

Now let me explain how the above java program works. In the above snippet, I have used BufferedImage, ImageIO, and Graphics2D classes from java to reduce the image memory size.

BufferedImage is a class that is used to handle and manipulate image data. It is a subclass of an image and it is fully made of color models of data.

ImageIO is a utility class that is used to processing the image. It has many utility functions. Mostly we can use this to read image data from the file and write image data to the file.

Graphics2D is a class that is used to draw the text, scale the image, set the background color and etc. Actually, we can do the transformation with the x,y coordinates. Here Graphics2D is used to render the image with reduced memory size.

Testing the Java Program for reducing the image memory size

Now lets start to test it with some sample image.

I am having the below image named "input.jpg" at the location of my computer "F:/resize/input.jpg". The memory size of the image is 50.7 KB.

Reduce Memory Size Input Image

Now I am going to execute the java snippet to reduce the memory size of the above image.

This program reads the image from the given location using ImagIO and processes the image with BufferedImage. Finally, it passes through the Graphics2D method which renders the image with reduced memory size.

The output image is given below and the reduced memory size is 13KB.

Reduced Memory Size Output Image

Now the memory size reduced from 50.7 KB to 13KB. Nearly it reduced 1/4th of the original memory size. It's great right. You can check the resolution also. Sure you can't see any major difference in the resolution side also.

You can check the memory size of both the images by right click the image and save it on your computer. Hope this program is very useful for the websites which will be of more images.

Leave a Reply

Your email address will not be published. Required fields are marked *