|
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;
|
|
}
|