Skip to content

Instantly share code, notes, and snippets.

@aspose-com-kb
Last active April 27, 2022 17:40
How to Compress Image using Java. For more information, please follow link: https://kb.aspose.com/imaging/java/how-to-compress-image-in-java/
import com.aspose.imaging.Image;
import com.aspose.imaging.License;
import com.aspose.imaging.imageoptions.JpegOptions;
import com.aspose.imaging.fileformats.jpeg.JpegCompressionColorMode;
import com.aspose.imaging.fileformats.jpeg.JpegCompressionMode;
public class CompressImageInJava {
public static void main(String[] args) // Main function to compress image in Java
{
// Instantiate a license to test product without trial version limitations
// and creating output compressed image without a watermark on it
License licForImagingLib = new License();
licForImagingLib.setLicense("Aspose.Imaging.lic");
// Load the desired image that needs to be compressed
Image img = Image.load("SampleJpeg.jpg");
// Create a JpegOptions class object to customize the output compressed image
JpegOptions options = new JpegOptions();
// Set different options for the output image
options.setColorType(JpegCompressionColorMode.Grayscale);
options.setCompressionType(JpegCompressionMode.Progressive);
// Save the compressed image
img.save("Compressed.jpeg",options);
System.out.println("Done");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment