Skip to content

Commit 3e386c9

Browse files
author
Evgeniy Sidenko
committed
Updated up to 24.2
Added an example of using different filters
1 parent 8c2aa30 commit 3e386c9

File tree

3 files changed

+118
-3
lines changed

3 files changed

+118
-3
lines changed

Examples/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.aspose</groupId>
55
<artifactId>imaging-java-examples</artifactId>
6-
<version>23.12</version>
6+
<version>24.2</version>
77
<packaging>jar</packaging>
88
<properties>
99
<maven.compiler.source>1.8</maven.compiler.source>
@@ -15,14 +15,14 @@
1515
<dependency>
1616
<groupId>com.aspose</groupId>
1717
<artifactId>aspose-imaging</artifactId>
18-
<version>23.12</version>
18+
<version>24.2</version>
1919
<classifier>jdk16</classifier>
2020
<type>jar</type>
2121
</dependency>
2222
<dependency>
2323
<groupId>com.aspose</groupId>
2424
<artifactId>aspose-imaging</artifactId>
25-
<version>23.12</version>
25+
<version>24.2</version>
2626
<classifier>javadoc</classifier>
2727
<type>jar</type>
2828
</dependency>
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.aspose.imaging.examples.ModifyingImages;
2+
3+
import com.aspose.imaging.Image;
4+
import com.aspose.imaging.RasterImage;
5+
import com.aspose.imaging.VectorImage;
6+
import com.aspose.imaging.examples.Logger;
7+
import com.aspose.imaging.examples.Utils;
8+
import com.aspose.imaging.imagefilters.complexutils.Complex;
9+
import com.aspose.imaging.imagefilters.convolution.ConvolutionFilter;
10+
import com.aspose.imaging.imagefilters.filteroptions.*;
11+
12+
import java.io.File;
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
import java.util.Random;
16+
17+
public class KernelFilters
18+
{
19+
public static void main(String[] args)
20+
{
21+
Logger.startExample();
22+
23+
final int Size = 5;
24+
final double Sigma = 1.5, Angle = 45;
25+
26+
double[][] customKernel = getRandomKernel(Size, 7, new Random());
27+
Complex[][] customComplex = ConvolutionFilter.toComplex(customKernel);
28+
FilterOptionsBase[] kernelFilters = new FilterOptionsBase[]
29+
{
30+
// convolution filters
31+
new ConvolutionFilterOptions(ConvolutionFilter.getEmboss3x3()),
32+
new ConvolutionFilterOptions(ConvolutionFilter.getEmboss5x5()),
33+
new ConvolutionFilterOptions(ConvolutionFilter.getSharpen3x3()),
34+
new ConvolutionFilterOptions(ConvolutionFilter.getSharpen5x5()),
35+
new ConvolutionFilterOptions(ConvolutionFilter.getBlurBox(Size)),
36+
new ConvolutionFilterOptions(ConvolutionFilter.getBlurMotion(Size, Angle)),
37+
new ConvolutionFilterOptions(ConvolutionFilter.getGaussian(Size, Sigma)),
38+
new ConvolutionFilterOptions(customKernel),
39+
new GaussianBlurFilterOptions(Size, Sigma),
40+
new SharpenFilterOptions(Size, Sigma),
41+
new MedianFilterOptions(Size),
42+
// deconvolution filters
43+
new DeconvolutionFilterOptions(ConvolutionFilter.getGaussian(Size, Sigma)),
44+
new DeconvolutionFilterOptions(customKernel),
45+
new DeconvolutionFilterOptions(customComplex),
46+
new GaussWienerFilterOptions(Size, Sigma),
47+
new MotionWienerFilterOptions(Size, Sigma, Angle),
48+
};
49+
50+
// The path to the documents' directory.
51+
String dataDir = Utils.getSharedDataDir() + "Png/";
52+
String outDir = Utils.getOutDir("Png/");
53+
String[] inputPaths =
54+
{
55+
"template.png"
56+
};
57+
58+
List<String> outputs = new ArrayList<>();
59+
for (String inputPath : inputPaths)
60+
{
61+
for (int i = 0; i < kernelFilters.length; i++)
62+
{
63+
FilterOptionsBase options = kernelFilters[i];
64+
try (Image image = Image.load(dataDir + inputPath))
65+
{
66+
String outputPath = String.format("%s%c%s-%d.png", outDir, File.separatorChar, inputPath, i);
67+
68+
if (image instanceof RasterImage)
69+
{
70+
filter((RasterImage) image, options, outputPath);
71+
}
72+
else if (image instanceof VectorImage)
73+
{
74+
String vectorAsPng = inputPath + ".png";
75+
if (!new File(vectorAsPng).exists())
76+
{
77+
image.save(vectorAsPng);
78+
outputs.add(vectorAsPng);
79+
}
80+
81+
try (Image png = Image.load(vectorAsPng))
82+
{
83+
filter((RasterImage) png, options, outputPath);
84+
}
85+
}
86+
}
87+
}
88+
}
89+
90+
outputs.forEach(p -> new File(p).delete());
91+
92+
Logger.endExample();
93+
}
94+
95+
static void filter(RasterImage raster, FilterOptionsBase options, String outputPath)
96+
{
97+
raster.filter(raster.getBounds(), options);
98+
raster.save(outputPath);
99+
}
100+
101+
static double[][] getRandomKernel(int cols, int rows, Random random)
102+
{
103+
double[][] customKernel = new double[cols][rows];
104+
for (int y = 0; y < customKernel.length; y++)
105+
{
106+
for (int x = 0; x < customKernel[0].length; x++)
107+
{
108+
customKernel[y][x] = random.nextDouble();
109+
}
110+
}
111+
return customKernel;
112+
}
113+
114+
}

Examples/src/main/java/com/aspose/imaging/examples/RunExamples.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public static void main(String[] args) throws IOException, InterruptedException,
114114
//// =====================================================
115115

116116
Logger.println("Running modifying and converting images tests:");
117+
KernelFilters.main(args);
117118
RemoveBackgroundVectors.main(args);
118119
AddAlphaBlendingForImage.main(args);
119120
RemoveWatermarkFilter.main(args);

0 commit comments

Comments
 (0)