# Resizing Raster Image
[TOC]
###### tags: `java` `imaging`
---
In terms of performance:
1. `java.awt.Graphics2D` – 34ms
2. Imgscalr – 143ms
3. `java.awt.Image.getScaledInstance()` – 235ms
4. Marvin – 361ms
5. Thumbnailator – 547ms
## `java.awt.Graphics2D`<SUP>[](https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Graphics2D.html)</SUP>
```java
BufferedImage resizeImage(
BufferedImage originalImage,
int targetWidth,
int targetHeight
) throws IOException {
BufferedImage resizedImage = new BufferedImage(
targetWidth,
targetHeight,
BufferedImage.TYPE_INT_RGB
);
Graphics2D graphics2D = resizedImage.createGraphics();
// Optional: Influencing different image processing aspects and most importantly image quality and processing time
graphics2D.setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR
);
graphics2D.drawImage(
originalImage,
0,
0,
targetWidth,
targetHeight,
null
);
graphics2D.dispose();
return resizedImage;
}
```
## Imgscalr
```xml
<dependency>
<groupId>org.imgscalr</groupId>
<artifactId>imgscalr-lib</artifactId>
<version>4.2</version>
</dependency>
```
### Simplest Way
```java
BufferedImage simpleResizeImage(
BufferedImage originalImage,
int targetWidth
) throws Exception {
return Scalr.resize(originalImage, targetWidth);
}
```
### Full `resize()` method
```java
BufferedImage resizeImage(
BufferedImage originalImage,
int targetWidth,
int targetHeight
) throws Exception {
return Scalr.resize(
originalImage,
Scalr.Method.AUTOMATIC,
Scalr.Mode.AUTOMATIC,
targetWidth,
targetHeight,
Scalr.OP_ANTIALIAS
);
}
```
## `java.awt.Image.getScaledInstance()`<SUP>[](https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Image.html#getScaledInstance(int,int,int))</SUP>
```java
BufferedImage resizeImage(
BufferedImage originalImage,
int targetWidth,
int targetHeight
) throws IOException {
Image resultingImage = originalImage.getScaledInstance(
targetWidth,
targetHeight,
Image.SCALE_DEFAULT // or Image.SCALE_SMOOTH
);
BufferedImage outputImage = new BufferedImage(
targetWidth,
targetHeight,
BufferedImage.TYPE_INT_RGB
);
outputImage.getGraphics().drawImage(
resultingImage,
0,
0,
null
);
return outputImage;
}
```
## Marvin
```xml
<dependency>
<groupId>com.github.downgoon</groupId>
<artifactId>marvin</artifactId>
<version>1.5.5</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.github.downgoon</groupId>
<artifactId>MarvinPlugins</artifactId>
<version>1.5.5</version>
</dependency>
```
```java
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();
}
```
## Thumbnailator
```xml
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.17</version>
</dependency>
```
### Setting Output Quality in Percentage
```java
BufferedImage resizeImage(
BufferedImage originalImage,
int targetWidth,
int targetHeight
) throws Exception {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Thumbnails.
of(originalImage).
size(
targetWidth,
targetHeight
).
outputFormat("JPEG").
outputQuality(1).
toOutputStream(outputStream);
byte[] data = outputStream.toByteArray();
ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
return ImageIO.read(inputStream);
}
```
### Batch Processing
```java
Thumbnails.
of(
new File("path/to/directory").
listFiles()
).
size(
300,
300
).
outputFormat("JPEG").
outputQuality(0.80).
toFiles(Rename.PREFIX_DOT_THUMBNAIL);
```
## 出處
- [How Can I Resize an Image Using Java?](https://www.baeldung.com/java-resize-image)