Skip to content

Instantly share code, notes, and snippets.

@ESidenko
Forked from aspose-com-gists/readme.md
Last active September 20, 2021 09:35
Show Gist options
  • Save ESidenko/663f168b27207403193ebcfd7f1b22a5 to your computer and use it in GitHub Desktop.
Save ESidenko/663f168b27207403193ebcfd7f1b22a5 to your computer and use it in GitHub Desktop.
HowTo : add resize of images or photos using more than 10 resampling types to your java application or Web service.

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

You can:

  • resize raster images;
  • resize vector images;
  • use set of various resample algorithms.

Interested ?

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

import com.aspose.imaging.Image;
import com.aspose.imaging.ResizeType;
import com.aspose.imaging.fileformats.tiff.enums.TiffExpectedFormat;
import com.aspose.imaging.imageoptions.TiffOptions;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
// Folder that contains images to process
final String templatesFolder = "c:\\Users\\USER\\Downloads\\";
List<String> rasterFormats = Arrays.asList("dng", "djvu");
rasterFormats.forEach(new Consumer<String>()
{
@Override
public void accept(String formatExt)
{
String inputFile = templatesFolder + "template." + formatExt;
String outputFile = templatesFolder + "resized.tiff";
int newWidth = 300;
int newHeight = 800;
try (Image image = Image.load(inputFile))
{
// Resize operation supports 16 possible types:
// LeftTopToLeftTop, RightTopToRightTop, RightBottomToRightBottom,
// LeftBottomToLeftBottom, CenterToCenter, LanczosResample,
// NearestNeighbourResample, AdaptiveResample, BilinearResample,
// HighQualityResample, CatmullRom, CubicConvolution,
// CubicBSpline, Mitchell, SinC
// More information available at https://apireference.aspose.com/imaging/net/aspose.imaging/resizetype
// and https://apireference.aspose.com/imaging/net/aspose.imaging.image/resize/methods/2
image.resize(newWidth, newHeight, ResizeType.HighQualityResample);
image.save(outputFile, new TiffOptions(TiffExpectedFormat.TiffJpegRgb));
}
}
});
import com.aspose.imaging.Image;
import com.aspose.imaging.ResizeType;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
// Folder that contains images to process
final String templatesFolder = "c:\\Users\\USER\\Downloads\\";
List<String> rasterFormats = Arrays.asList("png", "bmp", "apng", "dicom",
"jpg", "jp2", "j2k", "tga", "webp", "tif", "gif");
final int[] i = {0};
final Long[] resizeTypes = ResizeType.getValues(ResizeType.class);
rasterFormats.forEach(new Consumer<String>()
{
@Override
public void accept(String formatExt)
{
String inputFile = templatesFolder + "template." + formatExt;
String outputFile = templatesFolder + "resized" + formatExt;
int newWidth = 300;
int newHeight = 800;
try (Image image = Image.load(inputFile))
{
// Resize operation supports 16 possible types:
// LeftTopToLeftTop, RightTopToRightTop, RightBottomToRightBottom,
// LeftBottomToLeftBottom, CenterToCenter, LanczosResample,
// NearestNeighbourResample, AdaptiveResample, BilinearResample,
// HighQualityResample, CatmullRom, CubicConvolution,
// CubicBSpline, Mitchell, SinC
// More information available at https://apireference.aspose.com/imaging/net/aspose.imaging/resizetype
// and https://apireference.aspose.com/imaging/net/aspose.imaging.image/resize/methods/2
image.resize(newWidth, newHeight, resizeTypes[i[0]].intValue());
i[0] = (i[0] + 1) % resizeTypes.length; // take the next ResizeType
image.save(outputFile);
}
}
});
import com.aspose.imaging.Image;
import com.aspose.imaging.ResizeType;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
// Folder that contains images to process
final String templatesFolder = "c:\\Users\\USER\\Downloads\\";
List<String> rasterFormats = Arrays.asList("svg", "svgz");
rasterFormats.forEach(new Consumer<String>()
{
@Override
public void accept(String formatExt)
{
String inputFile = templatesFolder + "template." + formatExt;
String outputFile = templatesFolder + "resized." + formatExt;
int newWidth = 300;
int newHeight = 800;
try (Image image = Image.load(inputFile))
{
// Resize operation supports 16 possible types:
// LeftTopToLeftTop, RightTopToRightTop, RightBottomToRightBottom,
// LeftBottomToLeftBottom, CenterToCenter, LanczosResample,
// NearestNeighbourResample, AdaptiveResample, BilinearResample,
// HighQualityResample, CatmullRom, CubicConvolution,
// CubicBSpline, Mitchell, SinC
// More information available at https://apireference.aspose.com/imaging/net/aspose.imaging/resizetype
// and https://apireference.aspose.com/imaging/net/aspose.imaging.image/resize/methods/2
image.resize(newWidth, newHeight, ResizeType.CatmullRom);
image.save(outputFile);
}
}
});
import com.aspose.imaging.FileFormat;
import com.aspose.imaging.Image;
import com.aspose.imaging.imageoptions.*;
import com.aspose.ms.System.NotSupportedException;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
// Folder that contains images to process
final String templatesFolder = "c:\\Users\\USER\\Downloads\\";
List<String> vectorFormats = Arrays.asList("emf", "emz", "wmf", "wmz", "svg", "svgz", "cdr", "cmx", "odg", "otg");
vectorFormats.forEach(new Consumer<String>()
{
@Override
public void accept(String formatExt)
{
String inputFile = templatesFolder + "template." + formatExt;
String outputFile = templatesFolder + "resized_" + formatExt.toUpperCase() + ".png";
int newWidth = 500;
int newHeight = 400;
try (Image image = Image.load(inputFile))
{
PngOptions exportOptions = new PngOptions();
switch ((int) image.getFileFormat())
{
case (int) FileFormat.Wmf:
exportOptions.setVectorRasterizationOptions(new WmfRasterizationOptions());
break;
case (int) FileFormat.Emf:
exportOptions.setVectorRasterizationOptions(new EmfRasterizationOptions());
break;
case (int) FileFormat.Svg:
exportOptions.setVectorRasterizationOptions(new SvgRasterizationOptions());
break;
case (int) FileFormat.Cdr:
exportOptions.setVectorRasterizationOptions(new CdrRasterizationOptions());
break;
case (int) FileFormat.Cmx:
exportOptions.setVectorRasterizationOptions(new CmxRasterizationOptions());
break;
case (int) FileFormat.Odg:
exportOptions.setVectorRasterizationOptions(new OdgRasterizationOptions());
break;
case (int) FileFormat.Otg:
exportOptions.setVectorRasterizationOptions(new OtgRasterizationOptions());
break;
default:
throw new NotSupportedException();
}
exportOptions.getVectorRasterizationOptions().setPageWidth(newWidth);
exportOptions.getVectorRasterizationOptions().setPageHeight(newHeight);
image.save(outputFile, exportOptions);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment