Skip to content

Commit 95a8acb

Browse files
committed
initial commit
1 parent 0f44cc6 commit 95a8acb

11 files changed

Lines changed: 502 additions & 0 deletions

File tree

image-processing/pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,27 @@
6060
<artifactId>tesseract-platform</artifactId>
6161
<version>${tesseract-platform.version}</version>
6262
</dependency>
63+
<dependency>
64+
<groupId>org.imgscalr</groupId>
65+
<artifactId>imgscalr-lib</artifactId>
66+
<version>${imgscalr-version}</version>
67+
</dependency>
68+
<dependency>
69+
<groupId>net.coobird</groupId>
70+
<artifactId>thumbnailator</artifactId>
71+
<version>${thumbnailator-version}</version>
72+
</dependency>
73+
<dependency>
74+
<groupId>com.github.downgoon</groupId>
75+
<artifactId>marvin</artifactId>
76+
<version>${marvin-version}</version>
77+
<type>pom</type>
78+
</dependency>
79+
<dependency>
80+
<groupId>com.github.downgoon</groupId>
81+
<artifactId>MarvinPlugins</artifactId>
82+
<version>${marvin-version}</version>
83+
</dependency>
6384
</dependencies>
6485

6586
<properties>
@@ -69,6 +90,9 @@
6990
<tess4j.version>4.5.1</tess4j.version>
7091
<tesseract-platform.version>4.1.0-1.5.2</tesseract-platform.version>
7192
<opencv.version>3.4.2-0</opencv.version>
93+
<imgscalr-version>4.2</imgscalr-version>
94+
<thumbnailator-version>0.4.11</thumbnailator-version>
95+
<marvin-version>1.5.5</marvin-version>
7296
</properties>
7397

7498
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.image.resize.core;
2+
3+
import java.awt.Graphics2D;
4+
import java.awt.image.BufferedImage;
5+
import java.io.File;
6+
import java.io.IOException;
7+
8+
import javax.imageio.ImageIO;
9+
10+
public class Graphics2DExample {
11+
12+
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
13+
BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
14+
Graphics2D graphics2D = resizedImage.createGraphics();
15+
graphics2D.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null);
16+
graphics2D.dispose();
17+
return resizedImage;
18+
}
19+
20+
public static void main(String[] args) throws IOException {
21+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
22+
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
23+
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-graphics2d.jpg"));
24+
}
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.image.resize.core;
2+
3+
import java.awt.Image;
4+
import java.awt.image.BufferedImage;
5+
import java.io.File;
6+
import java.io.IOException;
7+
8+
import javax.imageio.ImageIO;
9+
10+
public class ImageScaledInstanceExample {
11+
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
12+
Image resultingImage = originalImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_DEFAULT);
13+
BufferedImage bufferedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
14+
bufferedImage.getGraphics()
15+
.drawImage(resultingImage, 0, 0, null);
16+
return bufferedImage;
17+
}
18+
19+
public static void main(String[] args) throws IOException {
20+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
21+
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
22+
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-scaledinstance.jpg"));
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.image.resize.imgscalr;
2+
3+
import java.awt.image.BufferedImage;
4+
import java.io.File;
5+
6+
import javax.imageio.ImageIO;
7+
8+
import org.imgscalr.Scalr;
9+
10+
public class ImgscalrExample {
11+
public static BufferedImage simpleResizeImage(BufferedImage originalImage, int targetWidth) {
12+
return Scalr.resize(originalImage, targetWidth);
13+
}
14+
15+
public static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
16+
return Scalr.resize(originalImage, Scalr.Method.AUTOMATIC, Scalr.Mode.AUTOMATIC, targetWidth, targetHeight, Scalr.OP_ANTIALIAS);
17+
}
18+
19+
public static void main(String[] args) throws Exception {
20+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
21+
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
22+
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-imgscalr.jpg"));
23+
}
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.image.resize.marvin;
2+
3+
import java.awt.image.BufferedImage;
4+
import java.io.File;
5+
import java.io.IOException;
6+
7+
import javax.imageio.ImageIO;
8+
9+
import org.marvinproject.image.transform.scale.Scale;
10+
11+
import marvin.image.MarvinImage;
12+
13+
public class MarvinExample {
14+
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
15+
MarvinImage image = new MarvinImage(originalImage);
16+
Scale scale = new Scale();
17+
scale.load();
18+
scale.setAttribute("newWidth", targetWidth);
19+
scale.setAttribute("newHeight", targetHeight);
20+
scale.process(image.clone(), image, null, null, false);
21+
return image.getBufferedImageNoAlpha();
22+
}
23+
24+
public static void main(String args[]) throws IOException {
25+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
26+
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
27+
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-marvin.jpg"));
28+
}
29+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.image.resize.thumbnailator;
2+
3+
import java.awt.image.BufferedImage;
4+
import java.io.ByteArrayInputStream;
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.File;
7+
import java.io.IOException;
8+
9+
import javax.imageio.ImageIO;
10+
11+
import net.coobird.thumbnailator.Thumbnails;
12+
13+
public class ThumbnailatorExample {
14+
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws IOException {
15+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
16+
Thumbnails.of(originalImage)
17+
.size(targetWidth, targetHeight)
18+
.outputFormat("JPEG")
19+
.outputQuality(0.90)
20+
.toOutputStream(outputStream);
21+
byte[] data = outputStream.toByteArray();
22+
ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
23+
return ImageIO.read(inputStream);
24+
}
25+
26+
public static void main(String[] args) throws Exception {
27+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
28+
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
29+
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-thumbnailator.jpg"));
30+
}
31+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.baeldung.image.resize.core;
2+
3+
import static org.junit.Assert.assertNotNull;
4+
import static org.junit.Assert.assertNull;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
7+
8+
import java.awt.image.BufferedImage;
9+
import java.io.File;
10+
import java.io.IOException;
11+
12+
import javax.imageio.ImageIO;
13+
14+
import org.junit.Test;
15+
16+
public class Graphics2DExampleUnitTest {
17+
18+
@Test(expected = Test.None.class)
19+
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
20+
int targetWidth = 200;
21+
int targetHeight = 200;
22+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
23+
BufferedImage outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
24+
25+
assertNotNull(outputImage);
26+
}
27+
28+
@Test(expected = Test.None.class)
29+
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException {
30+
int targetWidth = 200;
31+
int targetHeight = 200;
32+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
33+
assertNotEquals(originalImage.getWidth(), targetWidth);
34+
assertNotEquals(originalImage.getHeight(), targetHeight);
35+
BufferedImage outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
36+
37+
assertEquals(outputImage.getWidth(), targetWidth);
38+
assertEquals(outputImage.getHeight(), targetHeight);
39+
}
40+
41+
@Test(expected = Exception.class)
42+
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
43+
int targetWidth = 0;
44+
int targetHeight = 200;
45+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
46+
BufferedImage outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
47+
48+
assertNull(outputImage);
49+
}
50+
51+
@Test(expected = Exception.class)
52+
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
53+
int targetWidth = 200;
54+
int targetHeight = 0;
55+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
56+
BufferedImage outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
57+
58+
assertNull(outputImage);
59+
}
60+
61+
@Test(expected = Test.None.class)
62+
public void whenOriginalImageDoesNotExist_thenErrorIsNotThrownAndImageIsGenerated() throws IOException {
63+
int targetWidth = 200;
64+
int targetHeight = 200;
65+
BufferedImage outputImage = Graphics2DExample.resizeImage(null, targetWidth, targetHeight);
66+
67+
assertNotNull(outputImage);
68+
assertEquals(outputImage.getWidth(), targetWidth);
69+
assertEquals(outputImage.getHeight(), targetHeight);
70+
}
71+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.baeldung.image.resize.core;
2+
3+
import static org.junit.Assert.assertNotNull;
4+
import static org.junit.Assert.assertNull;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
7+
8+
import java.awt.image.BufferedImage;
9+
import java.io.File;
10+
import java.io.IOException;
11+
12+
import javax.imageio.ImageIO;
13+
14+
import org.junit.Test;
15+
16+
public class ImageScaledInstanceExampleUnitTest {
17+
18+
@Test(expected = Test.None.class)
19+
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
20+
int targetWidth = 200;
21+
int targetHeight = 200;
22+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
23+
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
24+
25+
assertNotNull(outputImage);
26+
}
27+
28+
@Test(expected = Test.None.class)
29+
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException {
30+
int targetWidth = 200;
31+
int targetHeight = 200;
32+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
33+
assertNotEquals(originalImage.getWidth(), targetWidth);
34+
assertNotEquals(originalImage.getHeight(), targetHeight);
35+
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
36+
37+
assertEquals(outputImage.getWidth(), targetWidth);
38+
assertEquals(outputImage.getHeight(), targetHeight);
39+
}
40+
41+
@Test(expected = Exception.class)
42+
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
43+
int targetWidth = 0;
44+
int targetHeight = 200;
45+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
46+
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
47+
48+
assertNull(outputImage);
49+
}
50+
51+
@Test(expected = Exception.class)
52+
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
53+
int targetWidth = 200;
54+
int targetHeight = 0;
55+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
56+
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
57+
58+
assertNull(outputImage);
59+
}
60+
61+
@Test(expected = Exception.class)
62+
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException {
63+
int targetWidth = 200;
64+
int targetHeight = 200;
65+
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(null, targetWidth, targetHeight);
66+
67+
assertNull(outputImage);
68+
}
69+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.baeldung.image.resize.imgscalr;
2+
3+
import static org.junit.Assert.assertNotNull;
4+
import static org.junit.Assert.assertNull;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
7+
8+
import java.awt.image.BufferedImage;
9+
import java.io.File;
10+
import java.io.IOException;
11+
12+
import javax.imageio.ImageIO;
13+
14+
import org.junit.Test;
15+
16+
public class ImgscalrExampleUnitTest {
17+
18+
@Test(expected = Test.None.class)
19+
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
20+
int targetWidth = 200;
21+
int targetHeight = 200;
22+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
23+
BufferedImage outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
24+
25+
assertNotNull(outputImage);
26+
}
27+
28+
@Test(expected = Test.None.class)
29+
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException {
30+
int targetWidth = 200;
31+
int targetHeight = 200;
32+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
33+
assertNotEquals(originalImage.getWidth(), targetWidth);
34+
assertNotEquals(originalImage.getHeight(), targetHeight);
35+
BufferedImage outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
36+
37+
assertEquals(outputImage.getWidth(), targetWidth);
38+
assertEquals(outputImage.getHeight(), targetHeight);
39+
}
40+
41+
@Test(expected = Test.None.class)
42+
public void whenTargetWidthIsZero_thenImageIsCreated() throws IOException {
43+
int targetWidth = 0;
44+
int targetHeight = 200;
45+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
46+
BufferedImage outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
47+
48+
assertNotNull(outputImage);
49+
}
50+
51+
@Test(expected = Test.None.class)
52+
public void whenTargetHeightIsZero_thenImageIsCreated() throws IOException {
53+
int targetWidth = 200;
54+
int targetHeight = 0;
55+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
56+
BufferedImage outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
57+
58+
assertNotNull(outputImage);
59+
}
60+
61+
@Test(expected = Exception.class)
62+
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() {
63+
int targetWidth = 200;
64+
int targetHeight = 200;
65+
BufferedImage outputImage = ImgscalrExample.resizeImage(null, targetWidth, targetHeight);
66+
67+
assertNull(outputImage);
68+
}
69+
}

0 commit comments

Comments
 (0)