Image Compressor Licensing Plugin
Utilize the Image Compressor license plugin to efficiently reduce the size of your images across various supported formats. Begin by obtaining a Metered license through the `setMeteredKey()` method, applying your public and private keys. Compression methods differ depending on the image format. Below are Java code examples tailored for common image formats such as APNG, BMP, DICOM, JPEG or SVG. Regardless of whether you're compressing raster or vector images, you have numerous options available. For example, in the JPEG format, you can adjust compression settings using JpegOptions(), setting the Quality parameter to 75%.
import com.aspose.imaging.*; | |
import com.aspose.imaging.fileformats.bmp.BitmapCompression; | |
import com.aspose.imaging.fileformats.dicom.ColorType; | |
import com.aspose.imaging.fileformats.dicom.Compression; | |
import com.aspose.imaging.fileformats.dicom.CompressionType; | |
import com.aspose.imaging.fileformats.ico.IcoImage; | |
import com.aspose.imaging.fileformats.jpeg.JpegCompressionColorMode; | |
import com.aspose.imaging.fileformats.jpeg.JpegCompressionMode; | |
import com.aspose.imaging.fileformats.jpeg2000.Jpeg2000Codec; | |
import com.aspose.imaging.fileformats.png.PngColorType; | |
import com.aspose.imaging.fileformats.tiff.enums.TiffCompressions; | |
import com.aspose.imaging.fileformats.tiff.enums.TiffExpectedFormat; | |
import com.aspose.imaging.fileformats.tiff.enums.TiffPhotometrics; | |
import com.aspose.imaging.imageoptions.*; | |
import java.io.File; | |
import java.util.*; | |
import java.util.function.Supplier; | |
class Tuple<T1, T2> | |
{ | |
private final T1 value1; | |
private final T2 value2; | |
public Tuple(T1 value1, T2 value2) | |
{ | |
this.value1 = value1; | |
this.value2 = value2; | |
} | |
public T1 getValue1() | |
{ | |
return value1; | |
} | |
public T2 getValue2() | |
{ | |
return value2; | |
} | |
} | |
void doCompress(String outputFile, Image image, ImageOptionsBase compressOptions) | |
{ | |
image.save(outputFile, compressOptions); | |
} | |
List<Tuple<ImageOptionsBase, String>> getCompressionOptions(long fileFormat, Image image) | |
{ | |
Supplier<VectorRasterizationOptions> rasterizationOptionsFactory = () -> | |
{ | |
final int imageWidth = image.getWidth(); | |
final int imageHeight = image.getHeight(); | |
VectorRasterizationOptions retValue; | |
long format = image.getFileFormat(); | |
if (format == FileFormat.Cdr) | |
{ | |
retValue = new CdrRasterizationOptions(); | |
} | |
else if (format == FileFormat.Cmx) | |
{ | |
return new CmxRasterizationOptions(); | |
} | |
else if (format == FileFormat.Odg) | |
{ | |
return new OdgRasterizationOptions(); | |
} | |
else if (format == FileFormat.Otg) | |
{ | |
return new OtgRasterizationOptions(); | |
} | |
else if (format == FileFormat.Eps) | |
{ | |
return new EpsRasterizationOptions(); | |
} | |
else | |
{ | |
return null; | |
} | |
retValue.setPageWidth(imageWidth); | |
retValue.setPageHeight(imageHeight); | |
return retValue; | |
}; | |
if (fileFormat == FileFormat.Apng) | |
{ | |
return Collections.singletonList( | |
new Tuple<ImageOptionsBase, String>(new ApngOptions() | |
{{ | |
setCompressionLevel(9); | |
setProgressive(true); | |
setColorType(PngColorType.IndexedColor); | |
setPalette(ColorPaletteHelper.getCloseImagePalette((RasterImage) image, 1 << 8)); | |
}}, null)); | |
} | |
else if (fileFormat == FileFormat.Bmp) | |
{ | |
return Arrays.asList( | |
new Tuple<ImageOptionsBase, String>(new BmpOptions() | |
{{ | |
setCompression(BitmapCompression.Rgb); | |
setBitsPerPixel(4); | |
setPalette(ColorPaletteHelper.getCloseImagePalette((RasterImage) image, 1 << 4)); | |
}}, | |
"Rgb4.bmp"), | |
new Tuple<ImageOptionsBase, String>(new BmpOptions() | |
{{ | |
setCompression(BitmapCompression.Rgb); | |
setBitsPerPixel(8); | |
setPalette(ColorPaletteHelper.getCloseImagePalette((RasterImage) image, 1 << 8)); | |
}}, "Rgb8.bmp") | |
); | |
} | |
else if (fileFormat == FileFormat.Cdr || fileFormat == FileFormat.Cmx || fileFormat == FileFormat.Odg || fileFormat == FileFormat.Otg || fileFormat == FileFormat.Eps) | |
{ | |
return Collections.singletonList( | |
new Tuple<ImageOptionsBase, String>(new SvgOptions() | |
{{ | |
setCompress(true); | |
setVectorRasterizationOptions(rasterizationOptionsFactory.get()); | |
}}, ".svgz")); | |
} | |
else if (fileFormat == FileFormat.Svg) | |
{ | |
return Collections.singletonList( | |
new Tuple<ImageOptionsBase, String>(new SvgOptions() | |
{{ | |
setCompress(true); | |
}}, ".svgz")); | |
} | |
else if (fileFormat == FileFormat.Dicom) | |
{ | |
return Collections.singletonList( | |
new Tuple<ImageOptionsBase, String>(new DicomOptions() | |
{{ | |
setColorType(ColorType.Grayscale8Bit); // or ColorType.Rgb24Bit for truecolor images | |
setCompression(new Compression() | |
{{ | |
setType(CompressionType.Jpeg); | |
setJpeg(new JpegOptions() | |
{{ | |
setQuality(75); | |
}}); | |
}}); | |
}}, null)); | |
} | |
else if (fileFormat == FileFormat.Dng || fileFormat == FileFormat.Tiff) | |
{ | |
return Collections.singletonList( | |
new Tuple<ImageOptionsBase, String>(new TiffOptions(TiffExpectedFormat.Default) | |
{{ | |
setPhotometric(TiffPhotometrics.Ycbcr); | |
setCompression(TiffCompressions.Jpeg); | |
setBitsPerSample(new int[]{8, 8, 8}); | |
}}, null)); | |
} | |
else if (fileFormat == FileFormat.Emf) | |
{ | |
return Collections.singletonList( | |
new Tuple<ImageOptionsBase, String>( | |
new EmfOptions() | |
{{ | |
setCompress(true); | |
}}, | |
".emz")); | |
} | |
else if (fileFormat == FileFormat.Wmf) | |
{ | |
return Collections.singletonList( | |
new Tuple<ImageOptionsBase, String>( | |
new WmfOptions() | |
{{ | |
setCompress(true); | |
}}, | |
".wmz")); | |
} | |
else if (fileFormat == FileFormat.Tga) | |
{ | |
return Collections.singletonList( | |
new Tuple<ImageOptionsBase, String>(new PngOptions() | |
{{ | |
setCompressionLevel(9); | |
setProgressive(true); | |
setColorType(PngColorType.IndexedColor); | |
setPalette(ColorPaletteHelper.getCloseImagePalette((RasterImage) image, 1 << 5)); | |
}}, ".png")); | |
} | |
else if (fileFormat == FileFormat.Gif) | |
{ | |
return Collections.singletonList( | |
new Tuple<ImageOptionsBase, String>(new GifOptions() | |
{{ | |
setPaletteSorted(false); | |
setDoPaletteCorrection(false); | |
setMaxDiff(500); | |
setColorResolution((byte) 7); | |
setPalette(ColorPaletteHelper.getCloseImagePalette((RasterImage) image, 1 << 8)); | |
}}, null)); | |
} | |
else if (fileFormat == FileFormat.Ico) | |
{ | |
if (!(image instanceof IcoImage)) | |
{ | |
throw new UnsupportedOperationException("The ico image is expected but " + image.getClass().getSimpleName() + " is found"); | |
} | |
IcoImage ico = (IcoImage) image; | |
final Optional<Image> optional = Arrays.stream(ico.getPages()).max(Comparator.comparingInt(o -> (o.getWidth() * o.getHeight() * o.getBitsPerPixel()))); | |
return optional.map(value -> Collections.singletonList( | |
new Tuple<ImageOptionsBase, String>(new IcoOptions() | |
{{ | |
setFormat(FileFormat.Bmp); | |
setBitsPerPixel(8); | |
setPalette(ColorPaletteHelper.getCloseImagePalette((RasterImage) value, 1 << 8)); | |
}}, null))).orElse(Collections.emptyList()); | |
} | |
else if (fileFormat == FileFormat.Jpeg2000) | |
{ | |
return Collections.singletonList( | |
new Tuple<ImageOptionsBase, String>(new Jpeg2000Options() | |
{{ | |
setCodec(Jpeg2000Codec.J2K); | |
setIrreversible(false); | |
setCompressionRatios(new int[]{100}); | |
}}, null)); | |
} | |
else if (fileFormat == FileFormat.Jpeg) | |
{ | |
return Collections.singletonList( | |
new Tuple<ImageOptionsBase, String>(new JpegOptions() | |
{{ | |
setCompressionType(JpegCompressionMode.Progressive); | |
setColorType(JpegCompressionColorMode.YCbCr); | |
setQuality(75); | |
}}, null)); | |
} | |
else if (fileFormat == FileFormat.Png) | |
{ | |
return Collections.singletonList( | |
new Tuple<ImageOptionsBase, String>(new PngOptions() | |
{{ | |
setCompressionLevel(9); | |
setProgressive(true); | |
setColorType(PngColorType.IndexedColor); | |
setPalette(ColorPaletteHelper.getCloseImagePalette((RasterImage) image, 1 << 5)); | |
}}, null)); | |
} | |
else if (fileFormat == FileFormat.Webp) | |
{ | |
return Collections.singletonList( | |
new Tuple<ImageOptionsBase, String>( | |
new WebPOptions() | |
{{ | |
setLossless(false); | |
setQuality(50); | |
}}, null)); | |
} | |
throw new UnsupportedOperationException("FileFormat " + FileFormat.toString(FileFormat.class, fileFormat) + " is not supported"); | |
} | |
// get path of the input data | |
String templatesFolder = System.getenv("DATA_PATH"); | |
if (templatesFolder == null) | |
{ | |
templatesFolder = "c:\\Users\\USER\\Downloads\\templates\\"; | |
} | |
// get output path | |
String outputFolder = System.getenv("OUT_PATH"); | |
if (outputFolder == null) | |
{ | |
outputFolder = templatesFolder; | |
} | |
//------------------------------------------------------------------------ | |
// Compression plug-in use sample | |
//------------------------------------------------------------------------ | |
// Valid compression plug-in use sample | |
Metered license = new Metered(); | |
// Only metered plug-in license is supported | |
license.setMeteredKey("<your public key>", "<your private key>"); | |
List<String> outFiles = new ArrayList<>(100); | |
String outputFilePath = outputFolder + File.separator +"compressed_"; | |
final File[] listFiles = new File(templatesFolder).listFiles((dir, name) -> name.endsWith(".jpg")); | |
if (listFiles == null) | |
{ | |
return; | |
} | |
for (File inputFilePath : listFiles) | |
{ | |
try (RasterImage image = (RasterImage) Image.load(inputFilePath.getAbsolutePath())) | |
{ | |
for (Tuple<ImageOptionsBase, String> options : getCompressionOptions(FileFormat.Png, image)) | |
{ | |
ImageOptionsBase oBase = options.getValue1(); | |
String currentOutputFile = outputFilePath + inputFilePath.getName(); | |
if (options.getValue2() != null) | |
{ | |
currentOutputFile += options.getValue2(); | |
} | |
doCompress(currentOutputFile, image, oBase); | |
outFiles.add(currentOutputFile); | |
} | |
} | |
} | |
// remove created files | |
for (String outFile : outFiles) | |
{ | |
new File(outFile).delete(); | |
} |
Try out the features of our online image compression demo directly on our Aspose.Imaging Compress Application website for free.