How to compress Image in Java?
UPDATED: 02 December 2011
Tags:
How to
,
javaQuery API
In Java there is no specific API to compress image. You have to go through bunch of codes to compress an image as per preferred size. Here I came with API that helps you to compress image on single method call and yes this is part of our own javaQuery API.
Features:
- Compress image online / offline
- Compress image as per your preferred size.
- Supported File : *.png, *.jpg
/*
* Image Resize ir = new ImageResize();
* ir.compressImage("FileLink","Saveas", width, height);
*
* Offline : C:\\test.JPG
* Online : Servlet, Struts, etc... String FileLink = getServletContext().getRealPath("/Your_Dir/");
*/
import javaQuery.j2ee.ImageResize;
public class compressImage {
public static void main(String[] args) {
ImageResize ir = new ImageResize();
//compress original image
ir.compressImage("C:\\test1.jpg", "default", 200, 200);
//create another copy of image
ir.compressImage("C:\\test1.jpg", "d:\\abc.jpg", 200, 200);
}
}
Arguments:
- 1st argument - Location of your image
- 2nd argument - If you pass "default" then it will compress the original image. If you don't want to compress original image and want another compressed image from that with other name. say c:\\test1.jpg to c:\\test2.jpg
- 3rd/4th argument - If you don't want to change width and height then set width = 0(zero) and height = 0(zero) . It'll compress image with original width and height (Compress File size).
Download API
Tags:
How to
,
javaQuery API

0 comments :