1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.github.jinahya.imgscalr;
19
20
21 import java.awt.Graphics2D;
22 import java.awt.image.BufferedImage;
23 import java.awt.image.BufferedImageOp;
24 import static org.imgscalr.Scalr.Method;
25 import static org.imgscalr.Scalr.Mode;
26
27
28
29
30
31
32 public strictfp class Scalr extends org.imgscalr.Scalr {
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public static BufferedImage resize(final BufferedImage sourceImage,
54 final Method scalingMethod,
55 final int targetWidth,
56 final int targetHeight,
57 final float magnificationFactor,
58 final float horizontalWeigth,
59 final float verticalWeight,
60 final BufferedImageOp... ops) {
61
62 final int sourceWidth = sourceImage.getWidth();
63 final int sourceHeight = sourceImage.getHeight();
64
65 final double widthRatio = targetWidth / (double) sourceWidth;
66 final double heightRatio = targetHeight / (double) sourceHeight;
67
68 final double minRatio = Math.min(widthRatio, heightRatio);
69 final double maxRatio = Math.max(widthRatio, heightRatio);
70
71 final int auxiliaryWidth
72 = (int) (magnificationFactor == .0f
73 ? sourceWidth * minRatio
74 : (sourceWidth * maxRatio) * magnificationFactor);
75 final int auxiliaryHeight
76 = (int) (magnificationFactor == .0f
77 ? sourceHeight * minRatio
78 : (sourceHeight * maxRatio) * magnificationFactor);
79
80
81
82
83 final int imageType = BufferedImage.TYPE_INT_ARGB;
84 final BufferedImage targetImage = new BufferedImage(
85 targetWidth, targetHeight, imageType);
86
87 final BufferedImage auxiliaryImage = resize(
88 sourceImage, scalingMethod, Mode.FIT_EXACT, auxiliaryWidth,
89 auxiliaryHeight);
90
91 final int x = (int) ((targetWidth - auxiliaryWidth) * horizontalWeigth);
92 final int y = (int) ((targetHeight - auxiliaryHeight) * verticalWeight);
93 final Graphics2D graphics = targetImage.createGraphics();
94 graphics.drawImage(auxiliaryImage, x, y, null);
95 graphics.dispose();
96
97 auxiliaryImage.flush();
98
99 return targetImage;
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 public static BufferedImage resizeInbox(final BufferedImage sourceImage,
119 final Method scalingMethod,
120 final int targetWidth,
121 final int targetHeight,
122 final float horizontalWeigth,
123 final float verticalWeight,
124 final BufferedImageOp... ops) {
125
126 return resize(sourceImage, scalingMethod, targetWidth, targetHeight,
127 .0f, horizontalWeigth, verticalWeight, ops);
128 }
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145 public static BufferedImage resizeInboxUpperLeft(
146 final BufferedImage sourceImage, final Method scalingMethod,
147 final int targetWidth, final int targetHeight,
148 final BufferedImageOp... ops) {
149
150 return resizeInbox(sourceImage, scalingMethod, targetWidth,
151 targetHeight, .0f, .0f, ops);
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169 public static BufferedImage resizeInboxCenter(
170 final BufferedImage sourceImage, final Method scalingMethod,
171 final int targetWidth, final int targetHeight,
172 final BufferedImageOp... ops) {
173
174 return resizeInbox(sourceImage, scalingMethod, targetWidth,
175 targetHeight, .5f, .5f, ops);
176 }
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193 public static BufferedImage resizeInboxLowerRight(
194 final BufferedImage sourceImage, final Method scalingMethod,
195 final int targetWidth, final int targetHeight,
196 final BufferedImageOp... ops) {
197
198 return resizeInbox(sourceImage, scalingMethod, targetWidth,
199 targetHeight, 1.0f, 1.0f, ops);
200 }
201
202
203 public static BufferedImage resizeOutbox(final BufferedImage sourceImage,
204 final Method scalingMethod,
205 final int targetWidth,
206 final int targetHeight,
207 final float horizontalWeigth,
208 final float verticalWeight,
209 final BufferedImageOp... ops) {
210
211 return resize(sourceImage, scalingMethod, targetWidth, targetHeight,
212 1.0f, horizontalWeigth, verticalWeight, ops);
213 }
214
215
216 public static BufferedImage resizeOutboxUpperLeft(
217 final BufferedImage sourceImage, final Method scalingMethod,
218 final int targetWidth, final int targetHeight,
219 final BufferedImageOp... ops) {
220
221 return resizeOutbox(sourceImage, scalingMethod, targetWidth,
222 targetHeight, .0f, .0f, ops);
223 }
224
225
226 public static BufferedImage resizeOutboxCenter(
227 final BufferedImage sourceImage, final Method scalingMethod,
228 final int targetWidth, final int targetHeight,
229 final BufferedImageOp... ops) {
230
231 return resizeOutbox(sourceImage, scalingMethod, targetWidth,
232 targetHeight, .5f, .5f, ops);
233 }
234
235
236 public static BufferedImage resizeOutboxLowerRight(
237 final BufferedImage sourceImage, final Method scalingMethod,
238 final int targetWidth, final int targetHeight,
239 final BufferedImageOp... ops) {
240
241 return resizeOutbox(sourceImage, scalingMethod, targetWidth,
242 targetHeight, 1.0f, 1.0f, ops);
243 }
244
245
246 private Scalr() {
247
248 super();
249 }
250
251
252 }
253