Image Merger Licensing Plugin
Effortlessly craft photo collages using the Image Merger License plugin, seamlessly combining images in various layouts like horizontal, vertical, tiled, and customizable arrangements. Import image files in diverse formats from the supported lists for seamless merging. Before executing the merge operation, obtain a Metered license by providing your public and private keys through the `setMeteredKey()` method. In the provided Java code example, a new `Image` object with a white background and dimensions equal to the total sum of source images is generated. Subsequently, images are positioned in specified directions using the drawImage() method of the `Graphics` class, and the resulting image is saved in the supported format. Failure to utilize licensed features of the Aspose.Imaging graphic library will lead to a watermark appearing on the output image.
//--------------------------------------------------------------------------------------- | |
// Image merge plug-in use examples | |
//--------------------------------------------------------------------------------------- | |
import com.aspose.imaging.*; | |
import com.aspose.imaging.fileformats.png.PngColorType; | |
import com.aspose.imaging.imageoptions.PngOptions; | |
import com.aspose.imaging.sources.StreamSource; | |
import java.io.File; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.function.Function; | |
enum MergeDirection | |
{ | |
Horizontal, | |
Vertical | |
} | |
void mergeImages(List<Image> images, MergeDirection direction, int totalSize, int maxSize, String outputPath, | |
Function<Image, Boolean> callback) | |
{ | |
int targetWidth, targetHeight; | |
switch (direction) | |
{ | |
case Horizontal: | |
{ | |
targetWidth = totalSize; | |
targetHeight = maxSize; | |
break; | |
} | |
case Vertical: | |
{ | |
targetWidth = maxSize; | |
targetHeight = totalSize; | |
break; | |
} | |
default: | |
throw new UnsupportedOperationException("Unexpected merge direction"); | |
} | |
PngOptions pngOptions = new PngOptions(); | |
pngOptions.setColorType(PngColorType.TruecolorWithAlpha); | |
pngOptions.setSource(new StreamSource()); | |
try (Image image = Image.create(pngOptions, targetWidth, targetHeight)) | |
{ | |
image.setBackgroundColor(Color.getWhite()); | |
Graphics graphics = new Graphics(image); | |
float x = 0, y = 0; | |
for (Image img : images) | |
{ | |
graphics.drawImage(img, new RectangleF(x, y, img.getWidth(), img.getHeight())); | |
if (direction == MergeDirection.Horizontal) | |
{ | |
x += image.getWidth(); | |
} | |
if (direction == MergeDirection.Vertical) | |
{ | |
y += image.getHeight(); | |
} | |
} | |
if (callback != null) | |
{ | |
callback.apply(image); | |
} | |
image.save(outputPath); | |
} | |
} | |
// Valid image merge license use example | |
Metered license = new Metered(); | |
// Only metered plug-in license is supported | |
license.setMeteredKey("<your public key>", "<your private key>"); | |
// 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 outputDirectory = System.getenv("OUT_PATH"); | |
if (outputDirectory == null) | |
{ | |
outputDirectory = templatesFolder; | |
} | |
List<Image> images = new ArrayList<Image>(3); | |
int maxWidth = 0; | |
int maxHeight = 0; | |
int totalWidth = 0; | |
int totalHeight = 0; | |
String[] imagePaths = new String[]{"template.png", "template.jpg", "template.bmp"}; | |
for (String fileName : imagePaths) | |
{ | |
Image image = Image.load(templatesFolder + fileName); | |
totalWidth += image.getWidth(); | |
totalHeight += image.getHeight(); | |
maxWidth = Math.max(image.getWidth(), maxWidth); | |
maxHeight = Math.max(image.getHeight(), maxHeight); | |
images.add(image); | |
} | |
try | |
{ | |
String outputPath = outputDirectory + File.separator + "licensed_merge_horizontal.jpg"; | |
mergeImages(images, MergeDirection.Horizontal, totalWidth, maxHeight, outputPath, null); | |
new File(outputPath).delete(); | |
outputPath = outputDirectory + File.separator + "licensed_merge_vertical.jpg"; | |
mergeImages(images, MergeDirection.Vertical, totalHeight, maxWidth, outputPath, null); | |
new File(outputPath).delete(); | |
// Unlicensed crop with merge plug-in license | |
outputPath = outputDirectory + File.separator + "trial_merge_vertical.jpg"; | |
mergeImages(images, MergeDirection.Vertical, totalHeight, maxWidth, outputPath, image -> { | |
if (!(image instanceof RasterImage)) | |
{ | |
return false; | |
} | |
RasterImage rasterImage = (RasterImage) image; | |
rasterImage.crop(new Rectangle(0, 0, image.getWidth() >> 1, image.getHeight() >> 1)); | |
return true; | |
}); | |
new File(outputPath).delete(); | |
} | |
finally | |
{ | |
images.forEach(Image::close); | |
} |
Explore the functionalities and features available on our free online Aspose.Imaging Merge Application demo site.