Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active March 27, 2023 13:58
Show Gist options
  • Save aspose-com-gists/74b9cf26c193cd103f69194c2bbe0984 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/74b9cf26c193cd103f69194c2bbe0984 to your computer and use it in GitHub Desktop.

Aspose.Imaging Java API allows to easy convert your images or photos in your Java application or Web service.

You can:

  • Convert single page files;
  • Convert multi-page images;
  • Convert set of raster and vector images.

Interested ?

You may go further at : https://products.aspose.com/imaging/java/

import com.aspose.imaging.Image;
import com.aspose.imaging.ImageOptionsBase;
import com.aspose.imaging.fileformats.jpeg2000.Jpeg2000Codec;
import com.aspose.imaging.fileformats.png.PngColorType;
import com.aspose.imaging.fileformats.tiff.enums.TiffExpectedFormat;
import com.aspose.imaging.imageoptions.*;
//This example demonstrates how to convert all supported file formats from one to another
String templatesFolder = "D:\\WorkDir\\";
//Formats that support both - save and load
HashMap<String, ImageOptionsBase> formatsThatSupportExportAndImport = new HashMap<String, ImageOptionsBase>();
formatsThatSupportExportAndImport.put("bmp", new BmpOptions());
formatsThatSupportExportAndImport.put("gif", new GifOptions());
formatsThatSupportExportAndImport.put("dicom", new DicomOptions());
formatsThatSupportExportAndImport.put("emf", new EmfOptions());
formatsThatSupportExportAndImport.put("jpg", new JpegOptions());
formatsThatSupportExportAndImport.put("jpeg", new JpegOptions());
formatsThatSupportExportAndImport.put("jpeg2000", new Jpeg2000Options() );
formatsThatSupportExportAndImport.put("j2k", new Jpeg2000Options() {{ setCodec(Jpeg2000Codec.J2K); }} );
formatsThatSupportExportAndImport.put("jp2", new Jpeg2000Options() {{ setCodec(Jpeg2000Codec.Jp2); }} );
formatsThatSupportExportAndImport.put("png",new PngOptions() {{ setColorType(PngColorType.TruecolorWithAlpha); }});
formatsThatSupportExportAndImport.put("apng", new ApngOptions());
formatsThatSupportExportAndImport.put("svg", new SvgOptions());
formatsThatSupportExportAndImport.put("tiff", new TiffOptions(TiffExpectedFormat.Default));
formatsThatSupportExportAndImport.put("tif", new TiffOptions(TiffExpectedFormat.Default));
formatsThatSupportExportAndImport.put("wmf", new WmfOptions());
formatsThatSupportExportAndImport.put("emz", new EmfOptions() {{ setCompress(true); }});
formatsThatSupportExportAndImport.put("wmz", new WmfOptions() {{ setCompress(true); }});
formatsThatSupportExportAndImport.put("svgz", new SvgOptions(){{ setCompress(true); }});
formatsThatSupportExportAndImport.put("tga", new TgaOptions());
formatsThatSupportExportAndImport.put("webp", new WebPOptions());
formatsThatSupportExportAndImport.put("ico", new IcoOptions());
//Formats that can be only saved
HashMap<String, ImageOptionsBase> formatsOnlyForExport = new HashMap<String, ImageOptionsBase>();
formatsOnlyForExport.put("psd", new PsdOptions());
formatsOnlyForExport.put("dxf", new DxfOptions() {{ setTextAsLines(true); setConvertTextBeziers(true); }} );
formatsOnlyForExport.put("pdf", new PdfOptions());
formatsOnlyForExport.put("html", new Html5CanvasOptions());
//Formats that can be only loaded
List<String> formatsOnlyForImport = Arrays.asList("djvu", "dng", "dib", "eps", "cdr", "cmx", "otg", "odg");
//Get total formats that can be saved
HashMap<String, ImageOptionsBase> exportToFormats = new HashMap<String, ImageOptionsBase>(formatsOnlyForExport);
exportToFormats.putAll(formatsThatSupportExportAndImport);
//Get total formats that can be loaded
List<String> importFormats = new LinkedList<>(formatsOnlyForImport);
importFormats.addAll(formatsThatSupportExportAndImport.keySet());
importFormats.forEach((formatExt) -> {
String inputFile = templatesFolder + "template." + formatExt;
for (Map.Entry<String, ImageOptionsBase> exportFormat : exportToFormats.entrySet())
{
String outputFile = String.format("%s\\%s\\%s-%s-to-%s.%s", templatesFolder, "convert", "convert-", formatExt, exportFormat.getKey(), exportFormat.getKey());
System.out.println(outputFile);
// More about load method can be found at
// https://apireference.aspose.com/imaging/java/com.aspose.imaging/Image#load-java.lang.String-
try (Image image = Image.load(inputFile))
{
ImageOptionsBase exportOptions = exportFormat.getValue().deepClone();
if ((formatExt.equals("emf") || formatExt.equals("emz")) && (exportFormat.getValue() instanceof WmfOptions))
{
EmfRasterizationOptions rasterizationOptions = new EmfRasterizationOptions();
rasterizationOptions.setPageWidth(image.getWidth());
rasterizationOptions.setPageHeight(image.getHeight());
exportOptions.setVectorRasterizationOptions(rasterizationOptions);
}
image.save(outputFile, exportOptions);
}
}
});
import com.aspose.imaging.Image;
import com.aspose.imaging.ImageOptionsBase;
import com.aspose.imaging.VectorImage;
import com.aspose.imaging.fileformats.jpeg2000.Jpeg2000Codec;
import com.aspose.imaging.fileformats.png.PngColorType;
import com.aspose.imaging.fileformats.tiff.enums.TiffExpectedFormat;
import com.aspose.imaging.imageoptions.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//Folder that contains set of test template files for all formats
String templatesFolder = "c:\\Users\\USER\\Downloads\\templates";
viewImage(templatesFolder);
static void viewImage(String templatesFolder)
{
//Get list of supported formats in
//Aspose.Imaging for loading and saving images
HashMap<String, ImageOptionsBase> importFormats = getAvailableImageFormats();
//Export image to png format for preview
HashMap<String, ImageOptionsBase> exportFormats = new HashMap<String, ImageOptionsBase>();
exportFormats.put("png", new PngOptions()
{{
setColorType(PngColorType.TruecolorWithAlpha);
}});
//Process each raster and vector format that can be loaded
for (String formatExt : importFormats.keySet())
{
String inputFile = templatesFolder + "template." + formatExt;
//Process each raster and vector format
//to which we can save imported image
for (String exportKey : exportFormats.keySet())
{
String outputFile = templatesFolder + "convert-" + formatExt + "-to-" + exportKey + "." + exportKey;
System.out.println("Processing conversion:" + outputFile);
//More about load method can be found at
//https://apireference.aspose.com/imaging/java/com.aspose.imaging/Image#load-java.lang.String-
//Load imported image
try (Image image = Image.load(inputFile))
{
//Obtain default saving options defined for each image
ImageOptionsBase exportOptions = exportFormats.get(exportKey).deepClone();
//If loaded image is vector, need to specify vector rasterization options
//for export to another vector
if (image instanceof VectorImage)
{
VectorRasterizationOptions rasterizationOptions = (VectorRasterizationOptions) importFormats.get(formatExt);
rasterizationOptions.setPageWidth(image.getWidth());
rasterizationOptions.setPageHeight(image.getHeight());
exportOptions.setVectorRasterizationOptions(rasterizationOptions);
}
image.save(outputFile, exportOptions);
}
}
}
}
static HashMap<String, ImageOptionsBase> getAvailableImageFormats()
{
////////////////////////////////
///Raster and vector formats to that we can export images
////////////////////////////////
//Raster image formats that support both - save and load and their default save options
HashMap<String, ImageOptionsBase> rasterFormatsThatSupportExportAndImport = new HashMap<String, ImageOptionsBase>();
rasterFormatsThatSupportExportAndImport.put("bmp", new BmpOptions());
rasterFormatsThatSupportExportAndImport.put("gif", new GifOptions());
rasterFormatsThatSupportExportAndImport.put("dicom", new DicomOptions());
rasterFormatsThatSupportExportAndImport.put("jpg", new JpegOptions());
rasterFormatsThatSupportExportAndImport.put("jpeg", new JpegOptions());
rasterFormatsThatSupportExportAndImport.put("jpeg2000", new Jpeg2000Options());
rasterFormatsThatSupportExportAndImport.put("j2k", new Jpeg2000Options()
{{
setCodec(Jpeg2000Codec.J2K);
}});
rasterFormatsThatSupportExportAndImport.put("jp2", new Jpeg2000Options()
{{
setCodec(Jpeg2000Codec.Jp2);
}});
rasterFormatsThatSupportExportAndImport.put("png", new PngOptions()
{{
setColorType(PngColorType.TruecolorWithAlpha);
}});
rasterFormatsThatSupportExportAndImport.put("apng", new ApngOptions());
rasterFormatsThatSupportExportAndImport.put("tiff", new TiffOptions(TiffExpectedFormat.Default));
rasterFormatsThatSupportExportAndImport.put("tif", new TiffOptions(TiffExpectedFormat.Default));
rasterFormatsThatSupportExportAndImport.put("tga", new TgaOptions());
rasterFormatsThatSupportExportAndImport.put("webp", new WebPOptions());
rasterFormatsThatSupportExportAndImport.put("ico", new IcoOptions());
//Vector image formats that support both - save and load, their default save options
//and their rasterization options when exporting to another vector image
HashMap<String, ImageOptionsBase> vectorFormatsThatSupportExportAndImport
= new HashMap<String, ImageOptionsBase>();
vectorFormatsThatSupportExportAndImport.put("emf", new EmfRasterizationOptions());
vectorFormatsThatSupportExportAndImport.put("svg", new SvgRasterizationOptions());
vectorFormatsThatSupportExportAndImport.put("wmf", new WmfRasterizationOptions());
vectorFormatsThatSupportExportAndImport.put("emz", new EmfRasterizationOptions());
vectorFormatsThatSupportExportAndImport.put("wmz", new WmfRasterizationOptions());
vectorFormatsThatSupportExportAndImport.put("svgz", new SvgRasterizationOptions());
////////////////////////////////
///Raster and vector formats from which we can load images
////////////////////////////////
//Raster formats that can be only loaded
List<String> formatsOnlyForImport = Arrays.asList("djvu", "dng", "dib");
//Vector formats only for loading and their rasterization options when exporting to another vector format
HashMap<String, ImageOptionsBase> vectorFormatsOnlyForImport = new HashMap<String, ImageOptionsBase>();
{
vectorFormatsOnlyForImport.put("eps", new EpsRasterizationOptions());
vectorFormatsOnlyForImport.put("cdr", new CdrRasterizationOptions());
vectorFormatsOnlyForImport.put("cmx", new CmxRasterizationOptions());
vectorFormatsOnlyForImport.put("otg", new OtgRasterizationOptions());
vectorFormatsOnlyForImport.put("odg", new OdgRasterizationOptions());
}
//Get total set of formats that can be loaded
HashMap<String, ImageOptionsBase> importFormats = new HashMap<>(vectorFormatsOnlyForImport);
for (String key : formatsOnlyForImport)
{
importFormats.put(key, new OdgRasterizationOptions());
}
importFormats.putAll(vectorFormatsThatSupportExportAndImport);
importFormats.putAll(rasterFormatsThatSupportExportAndImport);
return importFormats;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment