-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageResizer.java
44 lines (35 loc) · 1.17 KB
/
ImageResizer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.company;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.Buffer;
import javax.imageio.ImageIO;
/**
* This program demonstrates how to resize an image.
*
* @author www.codejava.net
*
*/
public class ImageResizer {
/**
* Resizes an image to a absolute width and height (the image may not be
* proportional)
* @param scaledWidth absolute width in pixels
* @param scaledHeight absolute height in pixels
* @throws IOException
*/
public static BufferedImage resize(BufferedImage inputImage, int scaledWidth, int scaledHeight)
throws IOException {
// reads input image
// creates output image
BufferedImage outputImage = new BufferedImage(scaledWidth,
scaledHeight, inputImage.getType());
// scales the input image to the output image
Graphics2D g2d = outputImage.createGraphics();
g2d.drawImage(inputImage, 0, 0, scaledWidth, scaledHeight, null);
g2d.dispose();
// writes to output file
return outputImage;
}
}