diff --git a/image-processing/src/main/java/com/baeldung/imageprocessing/imagetobufferedimage/ImageToBufferedImage.java b/image-processing/src/main/java/com/baeldung/imageprocessing/imagetobufferedimage/ImageToBufferedImage.java new file mode 100644 index 0000000000..f3a4491adf --- /dev/null +++ b/image-processing/src/main/java/com/baeldung/imageprocessing/imagetobufferedimage/ImageToBufferedImage.java @@ -0,0 +1,41 @@ +package com.baeldung.imageprocessing.imagetobufferedimage; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +public class ImageToBufferedImage { + + // Method 1: Using BufferedImage Constructor + public BufferedImage convertUsingConstructor(Image image) throws IllegalArgumentException { + int width = image.getWidth(null); + int height = image.getHeight(null); + if (width <= 0 || height <= 0) { + throw new IllegalArgumentException("Image dimensions are invalid"); + } + BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + bufferedImage.getGraphics().drawImage(image, 0, 0, null); + return bufferedImage; + } + + // Method 2: Casting Image to BufferedImage + public BufferedImage convertUsingCasting(Image image) throws ClassCastException { + if (image instanceof BufferedImage) { + return (BufferedImage) image; + } else { + throw new ClassCastException("Image type is not compatible with BufferedImage"); + } + } + + // Method 3: Using ImageIO Class + public BufferedImage convertUsingImageIO(String filePath) throws IOException { + try { + File file = new File(filePath); + return ImageIO.read(file); + } catch (Exception e) { + throw new IOException("Error reading image file: " + e.getMessage()); + } + } +} diff --git a/image-processing/src/test/java/com/baeldung/image/resize/imagetobufferedimage/ImageToBufferedImageUnitTest.java b/image-processing/src/test/java/com/baeldung/image/resize/imagetobufferedimage/ImageToBufferedImageUnitTest.java new file mode 100644 index 0000000000..74b16e2501 --- /dev/null +++ b/image-processing/src/test/java/com/baeldung/image/resize/imagetobufferedimage/ImageToBufferedImageUnitTest.java @@ -0,0 +1,69 @@ +package com.baeldung.image.resize.imagetobufferedimage; + +import com.baeldung.imageprocessing.imagetobufferedimage.ImageToBufferedImage; +import org.junit.Test; + +import javax.imageio.ImageIO; +import javax.swing.*; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class ImageToBufferedImageUnitTest { + Image image = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + + public ImageToBufferedImageUnitTest() throws IOException { + } + + @Test + public void whenConvertUsingConstructorWithValidDimensions_thenImageGeneratedWithoutError() { + ImageToBufferedImage converter = new ImageToBufferedImage(); + BufferedImage bufferedImage = converter.convertUsingConstructor(image); + assertNotNull(bufferedImage); + assertEquals(image.getWidth(null), bufferedImage.getWidth()); + assertEquals(image.getHeight(null), bufferedImage.getHeight()); + } + + @Test(expected = IllegalArgumentException.class) + public void whenConvertUsingConstructorWithInvalidDimensions_thenImageGeneratedWithError() { + ImageToBufferedImage converter = new ImageToBufferedImage(); + converter.convertUsingConstructor(new BufferedImage(-100, -100, BufferedImage.TYPE_INT_ARGB)); + } + + @Test + public void whenConvertUsingCastingWithCompatibleImageType_thenImageGeneratedWithoutError() { + ImageToBufferedImage converter = new ImageToBufferedImage(); + BufferedImage bufferedImage = converter.convertUsingCasting(image); + assertNotNull(bufferedImage); + assertEquals(image.getWidth(null), bufferedImage.getWidth()); + assertEquals(image.getHeight(null), bufferedImage.getHeight()); + } + + @Test(expected = ClassCastException.class) + public void whenConvertUsingCastingWithIncompatibleImageType_thenImageGeneratedWithError() { + ImageToBufferedImage converter = new ImageToBufferedImage(); + // PNG format is not directly supported by BufferedImage + Image image = new ImageIcon("src/main/resources/images/baeldung.png").getImage(); + converter.convertUsingCasting(image); + } + + @Test + public void whenConvertUsingImageIOWithValidFile_thenImageGeneratedWithoutError() throws IOException { + ImageToBufferedImage converter = new ImageToBufferedImage(); + BufferedImage bufferedImage = converter.convertUsingImageIO("src/main/resources/images/sampleImage.jpg"); + assertNotNull(bufferedImage); + assertEquals(image.getWidth(null), bufferedImage.getWidth()); + assertEquals(image.getHeight(null), bufferedImage.getHeight()); + } + + @Test(expected = IOException.class) + public void whenConvertUsingImageIOWithInvalidFile_thenImageGeneratedWithError() throws IOException { + ImageToBufferedImage converter = new ImageToBufferedImage(); + converter.convertUsingImageIO("invalid_file.jpg"); + } +} +