initial commit

This commit is contained in:
Maja Joksovic 2020-07-07 16:58:20 +02:00
parent 5248012a8a
commit a30aea24a0
11 changed files with 502 additions and 0 deletions

View File

@ -60,6 +60,27 @@
<artifactId>tesseract-platform</artifactId>
<version>${tesseract-platform.version}</version>
</dependency>
<dependency>
<groupId>org.imgscalr</groupId>
<artifactId>imgscalr-lib</artifactId>
<version>${imgscalr-version}</version>
</dependency>
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>${thumbnailator-version}</version>
</dependency>
<dependency>
<groupId>com.github.downgoon</groupId>
<artifactId>marvin</artifactId>
<version>${marvin-version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.github.downgoon</groupId>
<artifactId>MarvinPlugins</artifactId>
<version>${marvin-version}</version>
</dependency>
</dependencies>
<properties>
@ -69,6 +90,9 @@
<tess4j.version>4.5.1</tess4j.version>
<tesseract-platform.version>4.1.0-1.5.2</tesseract-platform.version>
<opencv.version>3.4.2-0</opencv.version>
<imgscalr-version>4.2</imgscalr-version>
<thumbnailator-version>0.4.11</thumbnailator-version>
<marvin-version>1.5.5</marvin-version>
</properties>
</project>

View File

@ -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"));
}
}

View File

@ -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"));
}
}

View File

@ -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"));
}
}

View File

@ -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"));
}
}

View File

@ -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"));
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}