diff --git a/image-processing/pom.xml b/image-processing/pom.xml index 8fe161337c..f2551598d5 100644 --- a/image-processing/pom.xml +++ b/image-processing/pom.xml @@ -60,6 +60,27 @@ tesseract-platform ${tesseract-platform.version} + + org.imgscalr + imgscalr-lib + ${imgscalr-version} + + + net.coobird + thumbnailator + ${thumbnailator-version} + + + com.github.downgoon + marvin + ${marvin-version} + pom + + + com.github.downgoon + MarvinPlugins + ${marvin-version} + @@ -69,6 +90,9 @@ 4.5.1 4.1.0-1.5.2 3.4.2-0 + 4.2 + 0.4.11 + 1.5.5 \ No newline at end of file diff --git a/image-processing/src/main/java/com/baeldung/image/resize/core/Graphics2DExample.java b/image-processing/src/main/java/com/baeldung/image/resize/core/Graphics2DExample.java new file mode 100644 index 0000000000..d52a2dac1b --- /dev/null +++ b/image-processing/src/main/java/com/baeldung/image/resize/core/Graphics2DExample.java @@ -0,0 +1,25 @@ +package com.baeldung.image.resize.core; + +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; + +public class Graphics2DExample { + + static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) { + BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB); + Graphics2D graphics2D = resizedImage.createGraphics(); + graphics2D.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null); + graphics2D.dispose(); + return resizedImage; + } + + public static void main(String[] args) throws IOException { + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + BufferedImage outputImage = resizeImage(originalImage, 200, 200); + ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-graphics2d.jpg")); + } +} diff --git a/image-processing/src/main/java/com/baeldung/image/resize/core/ImageScaledInstanceExample.java b/image-processing/src/main/java/com/baeldung/image/resize/core/ImageScaledInstanceExample.java new file mode 100644 index 0000000000..a6e252f3eb --- /dev/null +++ b/image-processing/src/main/java/com/baeldung/image/resize/core/ImageScaledInstanceExample.java @@ -0,0 +1,24 @@ +package com.baeldung.image.resize.core; + +import java.awt.Image; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; + +public class ImageScaledInstanceExample { + static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) { + Image resultingImage = originalImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_DEFAULT); + BufferedImage bufferedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB); + bufferedImage.getGraphics() + .drawImage(resultingImage, 0, 0, null); + return bufferedImage; + } + + public static void main(String[] args) throws IOException { + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + BufferedImage outputImage = resizeImage(originalImage, 200, 200); + ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-scaledinstance.jpg")); + } +} diff --git a/image-processing/src/main/java/com/baeldung/image/resize/imgscalr/ImgscalrExample.java b/image-processing/src/main/java/com/baeldung/image/resize/imgscalr/ImgscalrExample.java new file mode 100644 index 0000000000..10d4217de6 --- /dev/null +++ b/image-processing/src/main/java/com/baeldung/image/resize/imgscalr/ImgscalrExample.java @@ -0,0 +1,24 @@ +package com.baeldung.image.resize.imgscalr; + +import java.awt.image.BufferedImage; +import java.io.File; + +import javax.imageio.ImageIO; + +import org.imgscalr.Scalr; + +public class ImgscalrExample { + public static BufferedImage simpleResizeImage(BufferedImage originalImage, int targetWidth) { + return Scalr.resize(originalImage, targetWidth); + } + + public static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) { + return Scalr.resize(originalImage, Scalr.Method.AUTOMATIC, Scalr.Mode.AUTOMATIC, targetWidth, targetHeight, Scalr.OP_ANTIALIAS); + } + + public static void main(String[] args) throws Exception { + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + BufferedImage outputImage = resizeImage(originalImage, 200, 200); + ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-imgscalr.jpg")); + } +} diff --git a/image-processing/src/main/java/com/baeldung/image/resize/marvin/MarvinExample.java b/image-processing/src/main/java/com/baeldung/image/resize/marvin/MarvinExample.java new file mode 100644 index 0000000000..9a7233c4f4 --- /dev/null +++ b/image-processing/src/main/java/com/baeldung/image/resize/marvin/MarvinExample.java @@ -0,0 +1,29 @@ +package com.baeldung.image.resize.marvin; + +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; + +import org.marvinproject.image.transform.scale.Scale; + +import marvin.image.MarvinImage; + +public class MarvinExample { + static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) { + MarvinImage image = new MarvinImage(originalImage); + Scale scale = new Scale(); + scale.load(); + scale.setAttribute("newWidth", targetWidth); + scale.setAttribute("newHeight", targetHeight); + scale.process(image.clone(), image, null, null, false); + return image.getBufferedImageNoAlpha(); + } + + public static void main(String args[]) throws IOException { + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + BufferedImage outputImage = resizeImage(originalImage, 200, 200); + ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-marvin.jpg")); + } +} diff --git a/image-processing/src/main/java/com/baeldung/image/resize/thumbnailator/ThumbnailatorExample.java b/image-processing/src/main/java/com/baeldung/image/resize/thumbnailator/ThumbnailatorExample.java new file mode 100644 index 0000000000..2296c1649d --- /dev/null +++ b/image-processing/src/main/java/com/baeldung/image/resize/thumbnailator/ThumbnailatorExample.java @@ -0,0 +1,31 @@ +package com.baeldung.image.resize.thumbnailator; + +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; + +import net.coobird.thumbnailator.Thumbnails; + +public class ThumbnailatorExample { + static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws IOException { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + Thumbnails.of(originalImage) + .size(targetWidth, targetHeight) + .outputFormat("JPEG") + .outputQuality(0.90) + .toOutputStream(outputStream); + byte[] data = outputStream.toByteArray(); + ByteArrayInputStream inputStream = new ByteArrayInputStream(data); + return ImageIO.read(inputStream); + } + + public static void main(String[] args) throws Exception { + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + BufferedImage outputImage = resizeImage(originalImage, 200, 200); + ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-thumbnailator.jpg")); + } +} diff --git a/image-processing/src/test/java/com/baeldung/image/resize/core/Graphics2DExampleUnitTest.java b/image-processing/src/test/java/com/baeldung/image/resize/core/Graphics2DExampleUnitTest.java new file mode 100644 index 0000000000..675a17366d --- /dev/null +++ b/image-processing/src/test/java/com/baeldung/image/resize/core/Graphics2DExampleUnitTest.java @@ -0,0 +1,71 @@ +package com.baeldung.image.resize.core; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; + +import org.junit.Test; + +public class Graphics2DExampleUnitTest { + + @Test(expected = Test.None.class) + public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException { + int targetWidth = 200; + int targetHeight = 200; + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + BufferedImage outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight); + + assertNotNull(outputImage); + } + + @Test(expected = Test.None.class) + public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException { + int targetWidth = 200; + int targetHeight = 200; + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + assertNotEquals(originalImage.getWidth(), targetWidth); + assertNotEquals(originalImage.getHeight(), targetHeight); + BufferedImage outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight); + + assertEquals(outputImage.getWidth(), targetWidth); + assertEquals(outputImage.getHeight(), targetHeight); + } + + @Test(expected = Exception.class) + public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException { + int targetWidth = 0; + int targetHeight = 200; + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + BufferedImage outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight); + + assertNull(outputImage); + } + + @Test(expected = Exception.class) + public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException { + int targetWidth = 200; + int targetHeight = 0; + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + BufferedImage outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight); + + assertNull(outputImage); + } + + @Test(expected = Test.None.class) + public void whenOriginalImageDoesNotExist_thenErrorIsNotThrownAndImageIsGenerated() throws IOException { + int targetWidth = 200; + int targetHeight = 200; + BufferedImage outputImage = Graphics2DExample.resizeImage(null, targetWidth, targetHeight); + + assertNotNull(outputImage); + assertEquals(outputImage.getWidth(), targetWidth); + assertEquals(outputImage.getHeight(), targetHeight); + } +} diff --git a/image-processing/src/test/java/com/baeldung/image/resize/core/ImageScaledInstanceExampleUnitTest.java b/image-processing/src/test/java/com/baeldung/image/resize/core/ImageScaledInstanceExampleUnitTest.java new file mode 100644 index 0000000000..b0ac5a7787 --- /dev/null +++ b/image-processing/src/test/java/com/baeldung/image/resize/core/ImageScaledInstanceExampleUnitTest.java @@ -0,0 +1,69 @@ +package com.baeldung.image.resize.core; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; + +import org.junit.Test; + +public class ImageScaledInstanceExampleUnitTest { + + @Test(expected = Test.None.class) + public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException { + int targetWidth = 200; + int targetHeight = 200; + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight); + + assertNotNull(outputImage); + } + + @Test(expected = Test.None.class) + public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException { + int targetWidth = 200; + int targetHeight = 200; + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + assertNotEquals(originalImage.getWidth(), targetWidth); + assertNotEquals(originalImage.getHeight(), targetHeight); + BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight); + + assertEquals(outputImage.getWidth(), targetWidth); + assertEquals(outputImage.getHeight(), targetHeight); + } + + @Test(expected = Exception.class) + public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException { + int targetWidth = 0; + int targetHeight = 200; + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight); + + assertNull(outputImage); + } + + @Test(expected = Exception.class) + public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException { + int targetWidth = 200; + int targetHeight = 0; + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight); + + assertNull(outputImage); + } + + @Test(expected = Exception.class) + public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException { + int targetWidth = 200; + int targetHeight = 200; + BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(null, targetWidth, targetHeight); + + assertNull(outputImage); + } +} diff --git a/image-processing/src/test/java/com/baeldung/image/resize/imgscalr/ImgscalrExampleUnitTest.java b/image-processing/src/test/java/com/baeldung/image/resize/imgscalr/ImgscalrExampleUnitTest.java new file mode 100644 index 0000000000..e38850da21 --- /dev/null +++ b/image-processing/src/test/java/com/baeldung/image/resize/imgscalr/ImgscalrExampleUnitTest.java @@ -0,0 +1,69 @@ +package com.baeldung.image.resize.imgscalr; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; + +import org.junit.Test; + +public class ImgscalrExampleUnitTest { + + @Test(expected = Test.None.class) + public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException { + int targetWidth = 200; + int targetHeight = 200; + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + BufferedImage outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight); + + assertNotNull(outputImage); + } + + @Test(expected = Test.None.class) + public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException { + int targetWidth = 200; + int targetHeight = 200; + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + assertNotEquals(originalImage.getWidth(), targetWidth); + assertNotEquals(originalImage.getHeight(), targetHeight); + BufferedImage outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight); + + assertEquals(outputImage.getWidth(), targetWidth); + assertEquals(outputImage.getHeight(), targetHeight); + } + + @Test(expected = Test.None.class) + public void whenTargetWidthIsZero_thenImageIsCreated() throws IOException { + int targetWidth = 0; + int targetHeight = 200; + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + BufferedImage outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight); + + assertNotNull(outputImage); + } + + @Test(expected = Test.None.class) + public void whenTargetHeightIsZero_thenImageIsCreated() throws IOException { + int targetWidth = 200; + int targetHeight = 0; + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + BufferedImage outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight); + + assertNotNull(outputImage); + } + + @Test(expected = Exception.class) + public void whenOriginalImageDoesNotExist_thenErrorIsThrown() { + int targetWidth = 200; + int targetHeight = 200; + BufferedImage outputImage = ImgscalrExample.resizeImage(null, targetWidth, targetHeight); + + assertNull(outputImage); + } +} diff --git a/image-processing/src/test/java/com/baeldung/image/resize/marvin/MarvinExampleUnitTest.java b/image-processing/src/test/java/com/baeldung/image/resize/marvin/MarvinExampleUnitTest.java new file mode 100644 index 0000000000..3133a71035 --- /dev/null +++ b/image-processing/src/test/java/com/baeldung/image/resize/marvin/MarvinExampleUnitTest.java @@ -0,0 +1,68 @@ +package com.baeldung.image.resize.marvin; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; + +import org.junit.Test; + +public class MarvinExampleUnitTest { + @Test(expected = Test.None.class) + public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException { + int targetWidth = 200; + int targetHeight = 200; + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + BufferedImage outputImage = MarvinExample.resizeImage(originalImage, targetWidth, targetHeight); + + assertNotNull(outputImage); + } + + @Test(expected = Test.None.class) + public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException { + int targetWidth = 200; + int targetHeight = 200; + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + assertNotEquals(originalImage.getWidth(), targetWidth); + assertNotEquals(originalImage.getHeight(), targetHeight); + BufferedImage outputImage = MarvinExample.resizeImage(originalImage, targetWidth, targetHeight); + + assertEquals(outputImage.getWidth(), targetWidth); + assertEquals(outputImage.getHeight(), targetHeight); + } + + @Test(expected = Exception.class) + public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException { + int targetWidth = 0; + int targetHeight = 200; + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + BufferedImage outputImage = MarvinExample.resizeImage(originalImage, targetWidth, targetHeight); + + assertNull(outputImage); + } + + @Test(expected = Exception.class) + public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException { + int targetWidth = 200; + int targetHeight = 0; + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + BufferedImage outputImage = MarvinExample.resizeImage(originalImage, targetWidth, targetHeight); + + assertNull(outputImage); + } + + @Test(expected = Exception.class) + public void whenOriginalImageDoesNotExist_thenErrorIsThrown() { + int targetWidth = 200; + int targetHeight = 200; + BufferedImage outputImage = MarvinExample.resizeImage(null, targetWidth, targetHeight); + + assertNull(outputImage); + } +} diff --git a/image-processing/src/test/java/com/baeldung/image/resize/thumbnailator/ThumbnailatorExampleUnitTest.java b/image-processing/src/test/java/com/baeldung/image/resize/thumbnailator/ThumbnailatorExampleUnitTest.java new file mode 100644 index 0000000000..383ac1c7af --- /dev/null +++ b/image-processing/src/test/java/com/baeldung/image/resize/thumbnailator/ThumbnailatorExampleUnitTest.java @@ -0,0 +1,68 @@ +package com.baeldung.image.resize.thumbnailator; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; + +import org.junit.Test; + +public class ThumbnailatorExampleUnitTest { + @Test(expected = Test.None.class) + public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException { + int targetWidth = 200; + int targetHeight = 200; + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + BufferedImage outputImage = ThumbnailatorExample.resizeImage(originalImage, targetWidth, targetHeight); + + assertNotNull(outputImage); + } + + @Test(expected = Test.None.class) + public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException { + int targetWidth = 200; + int targetHeight = 200; + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + assertNotEquals(originalImage.getWidth(), targetWidth); + assertNotEquals(originalImage.getHeight(), targetHeight); + BufferedImage outputImage = ThumbnailatorExample.resizeImage(originalImage, targetWidth, targetHeight); + + assertEquals(outputImage.getWidth(), targetWidth); + assertEquals(outputImage.getHeight(), targetHeight); + } + + @Test(expected = Exception.class) + public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException { + int targetWidth = 0; + int targetHeight = 200; + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + BufferedImage outputImage = ThumbnailatorExample.resizeImage(originalImage, targetWidth, targetHeight); + + assertNull(outputImage); + } + + @Test(expected = Exception.class) + public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException { + int targetWidth = 200; + int targetHeight = 0; + BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + BufferedImage outputImage = ThumbnailatorExample.resizeImage(originalImage, targetWidth, targetHeight); + + assertNull(outputImage); + } + + @Test(expected = Exception.class) + public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException { + int targetWidth = 200; + int targetHeight = 200; + BufferedImage outputImage = ThumbnailatorExample.resizeImage(null, targetWidth, targetHeight); + + assertNull(outputImage); + } +}