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@15
|
23 |
import java.io.InputStream;
|
franta-hg@6
|
24 |
import java.io.OutputStream;
|
franta-hg@7
|
25 |
import java.util.logging.Level;
|
franta-hg@7
|
26 |
import java.util.logging.Logger;
|
franta-hg@6
|
27 |
import java.util.regex.Pattern;
|
franta-hg@6
|
28 |
import javax.imageio.ImageIO;
|
franta-hg@6
|
29 |
|
franta-hg@6
|
30 |
/**
|
franta-hg@6
|
31 |
*
|
franta-hg@6
|
32 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@6
|
33 |
*/
|
franta-hg@17
|
34 |
public class JavaSingleImageResizer implements SingleImageResizer {
|
franta-hg@6
|
35 |
|
franta-hg@7
|
36 |
private static final Logger log = Logger.getLogger(SingleImageResizer.class.getName());
|
franta-hg@7
|
37 |
|
franta-hg@17
|
38 |
@Override
|
franta-hg@15
|
39 |
public boolean resize(InputStream input, OutputStream output, SizeSpecification size, ImageFormat outputFormat) throws ResizeException {
|
franta-hg@6
|
40 |
try {
|
franta-hg@15
|
41 |
BufferedImage image = readImage(input);
|
franta-hg@15
|
42 |
if (shouldResize(image, size)) {
|
franta-hg@15
|
43 |
BufferedImage resized = resize(image, size.getWidth(), size.getHeight(), image.getType());
|
franta-hg@15
|
44 |
ImageIO.write(resized, outputFormat.getFormat(), output);
|
franta-hg@15
|
45 |
return true;
|
franta-hg@15
|
46 |
} else {
|
franta-hg@15
|
47 |
return false;
|
franta-hg@15
|
48 |
}
|
franta-hg@6
|
49 |
} catch (IOException e) {
|
franta-hg@6
|
50 |
throw new ResizeException("Error while resizing image", e);
|
franta-hg@6
|
51 |
}
|
franta-hg@6
|
52 |
}
|
franta-hg@6
|
53 |
|
franta-hg@6
|
54 |
private BufferedImage resize(BufferedImage original, int maxWidth, int maxHeight, int type) {
|
franta-hg@6
|
55 |
|
franta-hg@6
|
56 |
int width;
|
franta-hg@6
|
57 |
int height;
|
franta-hg@19
|
58 |
float originalAspectRatio = (float) original.getWidth() / (float) original.getHeight();
|
franta-hg@19
|
59 |
float requestedAspectRatio = (float) maxWidth / (float) maxHeight;
|
franta-hg@6
|
60 |
|
franta-hg@19
|
61 |
if (originalAspectRatio > requestedAspectRatio) {
|
franta-hg@6
|
62 |
width = maxWidth;
|
franta-hg@19
|
63 |
height = Math.max(Math.round(maxWidth / originalAspectRatio), 1);
|
franta-hg@6
|
64 |
} else {
|
franta-hg@19
|
65 |
width = Math.max(Math.round(maxHeight * originalAspectRatio), 1);
|
franta-hg@6
|
66 |
height = maxHeight;
|
franta-hg@6
|
67 |
}
|
franta-hg@6
|
68 |
|
franta-hg@7
|
69 |
if (type == BufferedImage.TYPE_CUSTOM) {
|
franta-hg@10
|
70 |
log.log(Level.FINER, "Setting default image type: from TYPE_CUSTOM to TYPE_INT_ARGB");
|
franta-hg@7
|
71 |
type = BufferedImage.TYPE_INT_ARGB;
|
franta-hg@7
|
72 |
}
|
franta-hg@7
|
73 |
|
franta-hg@6
|
74 |
BufferedImage resized = new BufferedImage(width, height, type);
|
franta-hg@6
|
75 |
Graphics2D g = resized.createGraphics();
|
franta-hg@6
|
76 |
g.drawImage(original, 0, 0, width, height, null);
|
franta-hg@6
|
77 |
g.dispose();
|
franta-hg@6
|
78 |
|
franta-hg@6
|
79 |
return resized;
|
franta-hg@6
|
80 |
}
|
franta-hg@6
|
81 |
|
franta-hg@15
|
82 |
private static BufferedImage readImage(InputStream input) throws ResizeException {
|
franta-hg@15
|
83 |
try {
|
franta-hg@15
|
84 |
return ImageIO.read(input);
|
franta-hg@15
|
85 |
} catch (IOException e) {
|
franta-hg@15
|
86 |
throw new ResizeException("Unable to read image from stream", e);
|
franta-hg@15
|
87 |
}
|
franta-hg@15
|
88 |
}
|
franta-hg@15
|
89 |
|
franta-hg@15
|
90 |
private static boolean shouldResize(BufferedImage input, SizeSpecification requested) {
|
franta-hg@15
|
91 |
if (requested.isResizeSmaller()) {
|
franta-hg@15
|
92 |
return input.getHeight() != requested.getHeight() || input.getWidth() != requested.getWidth();
|
franta-hg@15
|
93 |
} else {
|
franta-hg@15
|
94 |
return input.getHeight() > requested.getHeight() || input.getWidth() > requested.getWidth();
|
franta-hg@15
|
95 |
}
|
franta-hg@15
|
96 |
}
|
franta-hg@15
|
97 |
|
franta-hg@6
|
98 |
public static enum ImageFormat {
|
franta-hg@6
|
99 |
|
franta-hg@6
|
100 |
JPEG("jpg", Pattern.compile(".*\\.(jpg|jpeg)$", Pattern.CASE_INSENSITIVE)),
|
franta-hg@6
|
101 |
PNG("png", Pattern.compile(".*\\.png$", Pattern.CASE_INSENSITIVE)),
|
franta-hg@6
|
102 |
GIF("gif", Pattern.compile(".*\\.gif$", Pattern.CASE_INSENSITIVE));
|
franta-hg@6
|
103 |
|
franta-hg@6
|
104 |
private final String format;
|
franta-hg@6
|
105 |
private final Pattern regex;
|
franta-hg@6
|
106 |
|
franta-hg@6
|
107 |
private ImageFormat(String format, Pattern regex) {
|
franta-hg@6
|
108 |
this.format = format;
|
franta-hg@6
|
109 |
this.regex = regex;
|
franta-hg@6
|
110 |
}
|
franta-hg@6
|
111 |
|
franta-hg@7
|
112 |
/**
|
franta-hg@7
|
113 |
* @return format name for {@linkplain ImageIO#write(java.awt.image.RenderedImage, java.lang.String, java.io.File)
|
franta-hg@7
|
114 |
*/
|
franta-hg@6
|
115 |
public String getFormat() {
|
franta-hg@6
|
116 |
return format;
|
franta-hg@6
|
117 |
}
|
franta-hg@6
|
118 |
|
franta-hg@6
|
119 |
public static ImageFormat getMatching(String fileName) {
|
franta-hg@6
|
120 |
for (ImageFormat type : values()) {
|
franta-hg@6
|
121 |
if (type.regex.matcher(fileName).matches()) {
|
franta-hg@6
|
122 |
return type;
|
franta-hg@6
|
123 |
}
|
franta-hg@6
|
124 |
}
|
franta-hg@6
|
125 |
return null;
|
franta-hg@6
|
126 |
}
|
franta-hg@6
|
127 |
}
|
franta-hg@6
|
128 |
}
|