1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.wikimodel.images; |
21 |
|
|
22 |
|
import java.awt.Graphics2D; |
23 |
|
import java.awt.RenderingHints; |
24 |
|
import java.awt.image.BufferedImage; |
25 |
|
import java.io.IOException; |
26 |
|
import java.io.InputStream; |
27 |
|
import java.io.OutputStream; |
28 |
|
|
29 |
|
import javax.imageio.ImageIO; |
30 |
|
import javax.imageio.stream.ImageInputStream; |
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
@version |
37 |
|
@since |
38 |
|
|
|
|
| 0% |
Uncovered Elements: 44 (44) |
Complexity: 10 |
Complexity Density: 0.3 |
|
39 |
|
public class ImageUtil |
40 |
|
{ |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
@param |
47 |
|
@param |
48 |
|
@param |
49 |
|
@return |
50 |
|
|
51 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
|
52 |
0 |
private static BufferedImage createThumb(... |
53 |
|
BufferedImage image, |
54 |
|
int thumbWidth, |
55 |
|
int thumbHeight) |
56 |
|
{ |
57 |
0 |
int imageWidth = image.getWidth(null); |
58 |
0 |
int imageHeight = image.getHeight(null); |
59 |
0 |
int[] size = getNewSize( |
60 |
|
imageWidth, |
61 |
|
imageHeight, |
62 |
|
thumbWidth, |
63 |
|
thumbHeight); |
64 |
0 |
if (size[0] == imageWidth && size[1] == imageHeight) { |
65 |
0 |
return image; |
66 |
|
} |
67 |
|
|
68 |
0 |
BufferedImage thumbImage = new BufferedImage( |
69 |
|
size[0], |
70 |
|
size[1], |
71 |
|
BufferedImage.TYPE_INT_RGB); |
72 |
0 |
Graphics2D graphics2D = thumbImage.createGraphics(); |
73 |
0 |
graphics2D.setRenderingHint( |
74 |
|
RenderingHints.KEY_INTERPOLATION, |
75 |
|
RenderingHints.VALUE_INTERPOLATION_BILINEAR); |
76 |
0 |
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null); |
77 |
0 |
return thumbImage; |
78 |
|
} |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
@param |
90 |
|
@param |
91 |
|
|
92 |
|
@param |
93 |
|
@param |
94 |
|
@param |
95 |
|
|
96 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
97 |
0 |
public static void createThumb(... |
98 |
|
InputStream input, |
99 |
|
OutputStream output, |
100 |
|
int thumbWidth, |
101 |
|
int thumbHeight, |
102 |
|
String format) throws IOException |
103 |
|
{ |
104 |
0 |
try { |
105 |
0 |
try { |
106 |
0 |
ImageInputStream imageInput = ImageIO |
107 |
|
.createImageInputStream(input); |
108 |
0 |
BufferedImage image = ImageIO.read(imageInput); |
109 |
0 |
BufferedImage thumbImage = createThumb( |
110 |
|
image, |
111 |
|
thumbWidth, |
112 |
|
thumbHeight); |
113 |
0 |
ImageIO.write(thumbImage, format, output); |
114 |
|
} finally { |
115 |
0 |
output.close(); |
116 |
|
} |
117 |
|
} finally { |
118 |
0 |
input.close(); |
119 |
|
} |
120 |
|
} |
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
@param |
127 |
|
@return |
128 |
|
|
129 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
130 |
0 |
public static int[] getImageSize(InputStream input) throws IOException... |
131 |
|
{ |
132 |
0 |
try { |
133 |
0 |
ImageInputStream imageInput = ImageIO.createImageInputStream(input); |
134 |
0 |
BufferedImage image = ImageIO.read(imageInput); |
135 |
0 |
return new int[]{image.getWidth(), image.getHeight()}; |
136 |
|
} finally { |
137 |
0 |
input.close(); |
138 |
|
} |
139 |
|
} |
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
@param |
147 |
|
@param |
148 |
|
@param |
149 |
|
@return |
150 |
|
|
151 |
|
|
152 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
153 |
0 |
public static int[] getImageSize(... |
154 |
|
InputStream input, |
155 |
|
int maxWidth, |
156 |
|
int maxHeight) throws IOException |
157 |
|
{ |
158 |
0 |
int[] size = getImageSize(input); |
159 |
0 |
return getNewSize(size[0], size[1], maxWidth, maxHeight); |
160 |
|
} |
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
@param |
167 |
|
@param |
168 |
|
@param |
169 |
|
@param |
170 |
|
@return |
171 |
|
|
172 |
|
|
173 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
174 |
0 |
public static int[] getNewSize(... |
175 |
|
int width, |
176 |
|
int height, |
177 |
|
int maxWidth, |
178 |
|
int maxHeight) |
179 |
|
{ |
180 |
0 |
if (width <= maxWidth && height <= maxHeight) { |
181 |
0 |
return new int[]{width, height}; |
182 |
|
} |
183 |
0 |
double thumbRatio = (double) maxWidth / (double) maxHeight; |
184 |
0 |
double imageRatio = (double) width / (double) height; |
185 |
0 |
if (thumbRatio < imageRatio) { |
186 |
0 |
maxHeight = (int) (maxWidth / imageRatio); |
187 |
|
} else { |
188 |
0 |
maxWidth = (int) (maxHeight * imageRatio); |
189 |
|
} |
190 |
0 |
return new int[]{maxWidth, maxHeight}; |
191 |
|
} |
192 |
|
} |