Setting Properties on Images
Updating Fonts cache
Using Aspose.Imaging for Java, developers can add possibility to refresh fonts cache. Below is the code demonstration of the said functionality.
// For complete examples and data files, please go to https://github.com/Muhammad-Adnan-Ahmad/Aspose.Imaging-for-Java | |
PsdImage image = (PsdImage)Image.load(dataDir+"input.psd"); | |
try | |
{ | |
image.save("NoFont.psd"); | |
} | |
finally | |
{ | |
image.dispose(); | |
} | |
System.out.println("You have 2 minutes to install the font"); | |
Thread.sleep(2 * 60 * 1000); | |
OpenTypeFontsCache.updateCache(); | |
image = (PsdImage)Image.load("input.psd"); | |
try | |
{ | |
image.save("HasFont.psd"); | |
} | |
finally | |
{ | |
image.dispose(); | |
} |
Setting replacement for missing fonts
Using Aspose.Imaging for Java, developers can replace missing fonts. Using below sample code developers will be able to set default font name when saving PSD documents as raster image (into PNG, JPG and BMP formats). This default font should be used as a replacement for all missing fonts means fonts that are not found in current Operating System. Below is the code demonstration of the said functionality.
// For complete examples and data files, please go to https://github.com/Muhammad-Adnan-Ahmad/Aspose.Imaging-for-Java | |
String fileName = "testReplacementNotAvailableFonts.psd"; | |
PsdImage image = (PsdImage)Image.load(fileName, new PsdLoadOptions(){{ setDefaultReplacementFont ("Arial"); }}); | |
try | |
{ | |
image.save("result.png", new PngOptions() {{ setColorType(PngColorType.TruecolorWithAlpha); }}); | |
} | |
finally | |
{ | |
image.dispose(); | |
} |
Applying Smoothing Mode
Smoothing is often used to reduce noise within an image. VectorRasterizationOptions. setSmoothingMode is a property used for improving the quality of images. This article demonstrates how to apply VectorRasterizationOptions. setSmoothingMode property to images using Aspose.Imaging for Java.
Below is the code demonstration of the said functionality.
// The path to the documents directory. | |
String basePath = Utils.getSharedDataDir(ManualImageMasking.class) + "ModifyingImages/"; | |
String[] files = new String[] { | |
"SmoothingTest.cdr", | |
"SmoothingTest.cmx", | |
"SmoothingTest.emf", | |
"SmoothingTest.wmf", | |
"SmoothingTest.odg", | |
"SmoothingTest.svg" | |
}; | |
int[] smoothingModes = new int[] { | |
SmoothingMode.AntiAlias, SmoothingMode.None | |
}; | |
for (String fileName: files) { | |
Image image = Image.load(basePath + fileName); | |
try { | |
VectorRasterizationOptions vectorRasterizationOptions; | |
if (image instanceof CdrImage) { | |
vectorRasterizationOptions = new CdrRasterizationOptions(); | |
} else if (image instanceof CmxImage) { | |
vectorRasterizationOptions = new CmxRasterizationOptions(); | |
} else if (image instanceof EmfImage) { | |
vectorRasterizationOptions = new EmfRasterizationOptions(); | |
} else if (image instanceof WmfImage) { | |
vectorRasterizationOptions = new WmfRasterizationOptions(); | |
} else if (image instanceof OdgImage) { | |
vectorRasterizationOptions = new OdgRasterizationOptions(); | |
} else if (image instanceof SvgImage) { | |
vectorRasterizationOptions = new SvgRasterizationOptions(); | |
} else { | |
throw new RuntimeException("This is image is not supported in this example"); | |
} | |
vectorRasterizationOptions.setPageSize(Size.to_SizeF(image.getSize())); | |
for (int smoothingMode: smoothingModes) { | |
String outputFileName = basePath + String.format("image_%s%s.png", TextRenderingHint.toString(SmoothingMode.class, smoothingMode), fileName.substring(fileName.lastIndexOf('.'))); | |
vectorRasterizationOptions.setSmoothingMode(smoothingMode); | |
PngOptions pngOptions = new PngOptions(); | |
pngOptions.setVectorRasterizationOptions(vectorRasterizationOptions); | |
image.save(outputFileName, pngOptions); | |
} | |
} finally { | |
image.close(); | |
} | |
} |
Improve Text Quality
VectorRasterizationOptions. setTextRenderingHint is a property used for improving the quality of the text. This article demonstrates how to apply VectorRasterizationOptions. setTextRenderingHint property to images using Aspose.Imaging for Java.
Below is the code demonstration of the said functionality.
// The path to the documents directory. | |
String basePath = Utils.getSharedDataDir(ManualImageMasking.class) + "ModifyingImages/"; | |
String[] files = new String[] { | |
"TextHintTest.cdr", | |
"TextHintTest.cmx", | |
"TextHintTest.emf", | |
"TextHintTest.wmf", | |
"TextHintTest.odg", | |
"TextHintTest.svg" | |
}; | |
int[] textRenderingHints = new int[] { | |
TextRenderingHint.AntiAlias, TextRenderingHint.AntiAliasGridFit, | |
TextRenderingHint.ClearTypeGridFit, TextRenderingHint.SingleBitPerPixel, TextRenderingHint.SingleBitPerPixelGridFit | |
}; | |
for (String fileName: files) { | |
Image image = Image.load(basePath + fileName); | |
try { | |
VectorRasterizationOptions vectorRasterizationOptions; | |
if (image instanceof CdrImage) { | |
vectorRasterizationOptions = new CdrRasterizationOptions(); | |
} else if (image instanceof CmxImage) { | |
vectorRasterizationOptions = new CmxRasterizationOptions(); | |
} else if (image instanceof EmfImage) { | |
vectorRasterizationOptions = new EmfRasterizationOptions(); | |
} else if (image instanceof WmfImage) { | |
vectorRasterizationOptions = new WmfRasterizationOptions(); | |
} else if (image instanceof OdgImage) { | |
vectorRasterizationOptions = new OdgRasterizationOptions(); | |
} else if (image instanceof SvgImage) { | |
vectorRasterizationOptions = new SvgRasterizationOptions(); | |
} else { | |
throw new RuntimeException("This is image is not supported in this example"); | |
} | |
vectorRasterizationOptions.setPageSize(Size.to_SizeF(image.getSize())); | |
for (int textRenderingHint: textRenderingHints) { | |
String outputFileName = basePath + String.format("image_%s%s.png", TextRenderingHint.toString(TextRenderingHint.class, textRenderingHint), fileName.substring(fileName.lastIndexOf('.'))); | |
vectorRasterizationOptions.setTextRenderingHint(textRenderingHint); | |
PngOptions pngOptions = new PngOptions(); | |
pngOptions.setVectorRasterizationOptions(vectorRasterizationOptions); | |
image.save(outputFileName, pngOptions); | |
} | |
} finally { | |
image.close(); | |
} | |
} |
Support of image scoped fonts
Aspose.Imaging Java API allows the custom font source providing to use the specific font(s) for image rendering. Unlike FontSettings.setFontsFolders method works in the image scope and allowing to provide the fonts in multi-user scenarios.
Below is the code demonstration of the said functionality.
import com.aspose.imaging.*; | |
import com.aspose.imaging.customfonthandler.CustomFontData; | |
import com.aspose.imaging.imageoptions.PngOptions; | |
import com.aspose.imaging.imageoptions.VectorRasterizationOptions; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.util.LinkedList; | |
import java.util.List; | |
String dataDir = "C:/folder/"; | |
String[] files = new String[]{"missing-font.emf", "missing-font.odg", "missing-font.wmf", "missing-font.svg"}; | |
for (String file : files) | |
{ | |
String outputPath = dataDir + file + ".png"; | |
customFontSourceTest(dataDir, dataDir, file, dataDir + "Fonts/"); | |
new File(outputPath).delete(); | |
} | |
public static void customFontSourceTest(String inputPath, String outputPath, String fileName, String fontPath) | |
{ | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.addCustomFontSource(new CustomFontSource() | |
{ | |
@Override | |
public CustomFontData[] get(Object... objects) | |
{ | |
return getFontSource(objects); | |
} | |
}, fontPath); | |
try (Image img = Image.load(inputPath + fileName, loadOptions)) | |
{ | |
VectorRasterizationOptions vectorRasterizationOptions = | |
(VectorRasterizationOptions) img.getDefaultOptions(new Object[]{Color.getWhite(), img.getWidth(), img.getHeight()}); | |
vectorRasterizationOptions.setTextRenderingHint(TextRenderingHint.SingleBitPerPixel); | |
vectorRasterizationOptions.setSmoothingMode(SmoothingMode.None); | |
img.save(outputPath + fileName + ".png", new PngOptions() | |
{{ | |
setVectorRasterizationOptions(vectorRasterizationOptions); | |
}}); | |
} | |
} | |
// The custom fonts provider example. | |
public static CustomFontData[] getFontSource(Object... args) | |
{ | |
String fontsPath = ""; | |
if (args.length > 0) | |
{ | |
fontsPath = args[0].toString(); | |
} | |
List<CustomFontData> customFontData = new LinkedList<>(); | |
final File[] files = new File(fontsPath).listFiles(); | |
if (files != null) | |
{ | |
for (File font : files) | |
{ | |
try | |
{ | |
customFontData.add(new CustomFontData(getFileNameWithoutExtension(font.getName()), Files.readAllBytes(font.toPath()))); | |
} | |
catch (IOException e) | |
{ | |
// Hide errors | |
} | |
} | |
} | |
return customFontData.toArray(new CustomFontData[0]); | |
} | |
private static String getFileNameWithoutExtension(String fileName) | |
{ | |
return fileName.substring(0, fileName.lastIndexOf('.')); | |
} |