franta-hg@6
|
1 |
/**
|
franta-hg@6
|
2 |
* copy-image-resizer
|
franta-hg@6
|
3 |
* Copyright © 2014 František Kučera (frantovo.cz)
|
franta-hg@6
|
4 |
*
|
franta-hg@6
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@6
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@6
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
franta-hg@6
|
8 |
* (at your option) any later version.
|
franta-hg@6
|
9 |
*
|
franta-hg@6
|
10 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@6
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@6
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@6
|
13 |
* GNU General Public License for more details.
|
franta-hg@6
|
14 |
*
|
franta-hg@6
|
15 |
* You should have received a copy of the GNU General Public License
|
franta-hg@6
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@6
|
17 |
*/
|
franta-hg@6
|
18 |
package cz.frantovo.copyImageResizer;
|
franta-hg@6
|
19 |
|
franta-hg@6
|
20 |
import java.awt.Graphics2D;
|
franta-hg@6
|
21 |
import java.awt.image.BufferedImage;
|
franta-hg@6
|
22 |
import java.io.IOException;
|
franta-hg@6
|
23 |
import java.io.InputStream;
|
franta-hg@6
|
24 |
import java.io.OutputStream;
|
franta-hg@6
|
25 |
import java.util.regex.Pattern;
|
franta-hg@6
|
26 |
import javax.imageio.ImageIO;
|
franta-hg@6
|
27 |
|
franta-hg@6
|
28 |
/**
|
franta-hg@6
|
29 |
*
|
franta-hg@6
|
30 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@6
|
31 |
*/
|
franta-hg@6
|
32 |
public class SingleImageResizer {
|
franta-hg@6
|
33 |
|
franta-hg@6
|
34 |
public void resize(InputStream input, OutputStream output, ImageFormat outputFormat) throws ResizeException {
|
franta-hg@6
|
35 |
try {
|
franta-hg@6
|
36 |
BufferedImage image = ImageIO.read(input);
|
franta-hg@6
|
37 |
BufferedImage resized = resize(image, 64, 64, image.getType());
|
franta-hg@6
|
38 |
|
franta-hg@6
|
39 |
ImageIO.write(resized, outputFormat.getFormat(), output);
|
franta-hg@6
|
40 |
} catch (IOException e) {
|
franta-hg@6
|
41 |
throw new ResizeException("Error while resizing image", e);
|
franta-hg@6
|
42 |
}
|
franta-hg@6
|
43 |
}
|
franta-hg@6
|
44 |
|
franta-hg@6
|
45 |
private BufferedImage resize(BufferedImage original, int maxWidth, int maxHeight, int type) {
|
franta-hg@6
|
46 |
|
franta-hg@6
|
47 |
int width;
|
franta-hg@6
|
48 |
int height;
|
franta-hg@6
|
49 |
float aspectRatio = (float) original.getWidth() / (float) original.getHeight();
|
franta-hg@6
|
50 |
|
franta-hg@6
|
51 |
if (aspectRatio > 1) {
|
franta-hg@6
|
52 |
width = maxWidth;
|
franta-hg@6
|
53 |
height = Math.max(Math.round(maxWidth / aspectRatio), 1);
|
franta-hg@6
|
54 |
} else {
|
franta-hg@6
|
55 |
width = Math.max(Math.round(maxHeight * aspectRatio), 1);
|
franta-hg@6
|
56 |
height = maxHeight;
|
franta-hg@6
|
57 |
}
|
franta-hg@6
|
58 |
|
franta-hg@6
|
59 |
BufferedImage resized = new BufferedImage(width, height, type);
|
franta-hg@6
|
60 |
Graphics2D g = resized.createGraphics();
|
franta-hg@6
|
61 |
g.drawImage(original, 0, 0, width, height, null);
|
franta-hg@6
|
62 |
g.dispose();
|
franta-hg@6
|
63 |
|
franta-hg@6
|
64 |
return resized;
|
franta-hg@6
|
65 |
}
|
franta-hg@6
|
66 |
|
franta-hg@6
|
67 |
public static enum ImageFormat {
|
franta-hg@6
|
68 |
|
franta-hg@6
|
69 |
JPEG("jpg", Pattern.compile(".*\\.(jpg|jpeg)$", Pattern.CASE_INSENSITIVE)),
|
franta-hg@6
|
70 |
PNG("png", Pattern.compile(".*\\.png$", Pattern.CASE_INSENSITIVE)),
|
franta-hg@6
|
71 |
GIF("gif", Pattern.compile(".*\\.gif$", Pattern.CASE_INSENSITIVE));
|
franta-hg@6
|
72 |
|
franta-hg@6
|
73 |
private final String format;
|
franta-hg@6
|
74 |
private final Pattern regex;
|
franta-hg@6
|
75 |
|
franta-hg@6
|
76 |
private ImageFormat(String format, Pattern regex) {
|
franta-hg@6
|
77 |
this.format = format;
|
franta-hg@6
|
78 |
this.regex = regex;
|
franta-hg@6
|
79 |
}
|
franta-hg@6
|
80 |
|
franta-hg@6
|
81 |
public String getFormat() {
|
franta-hg@6
|
82 |
return format;
|
franta-hg@6
|
83 |
}
|
franta-hg@6
|
84 |
|
franta-hg@6
|
85 |
public static ImageFormat getMatching(String fileName) {
|
franta-hg@6
|
86 |
for (ImageFormat type : values()) {
|
franta-hg@6
|
87 |
if (type.regex.matcher(fileName).matches()) {
|
franta-hg@6
|
88 |
return type;
|
franta-hg@6
|
89 |
}
|
franta-hg@6
|
90 |
}
|
franta-hg@6
|
91 |
return null;
|
franta-hg@6
|
92 |
}
|
franta-hg@6
|
93 |
}
|
franta-hg@6
|
94 |
}
|