-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphics2DTools.java
More file actions
275 lines (247 loc) · 9.88 KB
/
Graphics2DTools.java
File metadata and controls
275 lines (247 loc) · 9.88 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* Original Licensing Copyright
*
* Image manipulation methods for financial dashboards and displays.
* Copyright (C) 2021 DZ-FSDev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.dz_fs_dev.common;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import javax.imageio.IIOException;
import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageOutputStream;
import javax.imageio.stream.ImageOutputStream;
import javax.imageio.stream.MemoryCacheImageOutputStream;
import jesino.GifSequenceWriter;
/**
* Utility class containing different image manipulative methods for financial dashboards and displays.
*
* @author DZ-FSDev
* @since 17.0.1
* @version 0.0.8
*/
public final class Graphics2DTools {
private Graphics2DTools() {}
/**
* Scales an image retaining lines and parallelism at the cost of distances and angles between objects.
*
* @param sourceImg The image to be scaled.
* @param scaledWidth The width of scaled image.
* @param scaledHeight The height of scaled image.
* @return The affine scaled image.
* @since 0.0.1
*/
public static BufferedImage scaleImageEuclidianAffine(BufferedImage sourceImg, int scaledWidth, int scaledHeight) {
BufferedImage scaledImg = null;
scaledImg = new BufferedImage(scaledWidth, scaledHeight, sourceImg.getType());
Graphics2D g2d = scaledImg.createGraphics();
AffineTransform affineTransformer = AffineTransform.getScaleInstance(
(double)scaledWidth / sourceImg.getWidth(), (double)scaledHeight / sourceImg.getHeight());
g2d.drawRenderedImage(sourceImg, affineTransformer);
g2d.dispose();
return scaledImg;
}
/**
* Scales an image quickly at the cost of output quality.
*
* @param sourceImg The image to be scaled.
* @param scaledWidth The width of scaled image.
* @param scaledHeight The height of scaled image.
* @return The scaled image.
* @since 0.0.1
*/
public static BufferedImage scaleImageFast(BufferedImage sourceImg, int scaledWidth, int scaledHeight) {
BufferedImage scaledImg = new BufferedImage(scaledWidth, scaledHeight, sourceImg.getType());
Graphics2D g2d = scaledImg.createGraphics();
g2d.drawImage(sourceImg, 0, 0, scaledWidth, scaledHeight, null);
g2d.dispose();
return scaledImg;
}
/**
* Saves a BufferedImage to a specified file as a portable network graphic.
*
* @param bufferedImage The image to be saved.
* @param fileName The name of the file to be saved.
* @throws IOException Thrown if the file cannot be written.
* @since 0.0.2
*/
public static void saveAsPNG(BufferedImage bufferedImage, String fileName) throws IOException {
File outputfile = new File(fileName + ".png");
ImageIO.write(bufferedImage, "png", outputfile);
}
/**
* Saves a BufferedImage to a specified file as a Joint Photographic Experts Group digital image.
*
* @param bufferedImage The image to be saved.
* @param fileName The name of the file to be saved.
* @throws IOException Thrown if the file cannot be written.
* @since 0.0.2
*/
public static void saveAsJPG(BufferedImage bufferedImage, String fileName) throws IOException {
File outputfile = new File(fileName + ".jpg");
ImageIO.write(bufferedImage, "jpg", outputfile);
}
/**
* Saves a BufferedImage to a specified file as a portable network graphic.
*
* @param bufferedImage The image to be saved.
* @param outputFile The file to save the BufferedImage to.
* @throws IOException Thrown if the file cannot be written.
* @since 0.0.3
*/
public static void saveAsPNG(BufferedImage bufferedImage, File outputFile) throws IOException {
if(!outputFile.getName().endsWith(".png"))outputFile = new File(outputFile.getPath() + ".png");
ImageIO.write(bufferedImage, "png", outputFile);
}
/**
* Saves a list of BufferedImages as a single animated GIF. The frame delay will be constant through the animation.
*
* @param images The list of buffered images to be compiled.
* @param frameDdelay The uniform delay in milliseconds between frames.
* @param outputFile The file to write the output.
* @throws IOException
* @throws FileNotFoundException Thrown if file cannot be written to.
* @throws IIOException Thrown if the there are issues with reads or writes fail.
* @since 0.0.5
*/
public static void saveAsAnimatedGif(List<BufferedImage> images, int frameDdelay, File outputFile) throws IIOException, FileNotFoundException, IOException {
FileImageOutputStream fios;
GifSequenceWriter gifWriter = new GifSequenceWriter(
fios = new FileImageOutputStream(outputFile),
BufferedImage.TYPE_INT_ARGB, frameDdelay, true);
for(BufferedImage image : images) {
gifWriter.writeToSequence(image);;
}
gifWriter.close();
fios.close();
}
/**
* Returns a byte array representing an animated GIF with frames from a list of BufferedImages.
* The frame delay will be constant through the animation.
*
* @param images The list of buffered images be compiled.
* @param frameDelay The uniform delay in milliseconds between frames.
* @throws IOException
* @throws IIOException Thrown if the there are issues with reads or writes fail.
* @since 0.0.6
*/
public static byte[] toAnimatedGifBytes(List<BufferedImage> images, int frameDelay) throws IIOException, IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream mcos = new MemoryCacheImageOutputStream(baos);
GifSequenceWriter gifWriter = new GifSequenceWriter(
mcos, BufferedImage.TYPE_INT_ARGB, frameDelay, true);
for(BufferedImage image : images) {
gifWriter.writeToSequence(image);
}
gifWriter.close();
mcos.close();
return baos.toByteArray();
}
/**
* Saves a BufferedImage to a specified file as a Joint Photographic Experts Group digital image.
*
* @param bufferedImage The image to be saved.
* @param outputFile The file to save the BufferedImage to.
* @throws IOException Thrown if the file cannot be written.
* @since 0.0.3
*/
public static void saveAsJPG(BufferedImage bufferedImage, File outputFile) throws IOException {
if(!outputFile.getName().endsWith(".jpg"))outputFile = new File(outputFile.getPath() + ".jpg");
ImageIO.write(bufferedImage, "jpg", outputFile);
}
/**
* Returns a png byte array containing the data from a specified buffered image.
*
* @param bufferedImage The buffered image to be converted to a byte array.
* @return The png byte array.
* @throws IOException Thrown if the file cannot be written.
* @since 0.0.4
*/
public static byte[] toPNGbytes(BufferedImage bufferedImage) throws IOException {
ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", byteArrayStream);
return byteArrayStream.toByteArray();
}
/**
* Utility method for drawing text to the client. Functionality limited to fonts with uniform character width.
*
* @param g The Graphics object tied to the image to draw the text on.
* @param str The text to be drawn.
* @param font The font that will be used to draw the text.
* @param w The width of the boundaries for the text to be drawn on.
* @param h The height of the boundaries for the text to be drawn on.
* @param ox The offset in the x direction of the text to be drawn on.
* @param oy The offset in the y direction of the text to be drawn on.
* @return The height of the text drawn.
* @since 0.0.7
*/
public static int drawText(Graphics g, String str, Font font, int w, int h, int ox, int oy){
int init_oy = oy;
//Build the str
g.setFont(font);
FontMetrics metrics = g.getFontMetrics(font);
int fh = metrics.getHeight(), fw = metrics.stringWidth("ABC abc")/7, cpw = w/fw - 1;
//Debugging
//g.drawRect(ox, oy, w, h);
for(int i = 0; i < str.length();){
String seg = str.substring(i, Math.min(i+cpw,str.length()));
if(seg.contains("\n")){
i += seg.indexOf("\n")+1;
continue;
}else if(seg.length() == cpw){
int ii = Math.max(Math.max(Math.max(seg.lastIndexOf(" "), seg.lastIndexOf(";")), seg.lastIndexOf(",")), seg.lastIndexOf("."));
if(ii > 0){
i += ii;
str = str.substring(0,i) + "\n" + str.substring(i+1,str.length());
}
}
i++;
}
//Draw the str
String[] lines = str.split("\n");
for(String l : lines){
g.setColor(Color.WHITE);
g.drawString(l, ox, oy += fh);
//System.out.println(l);
}
return oy - init_oy;
}
/**
* Rotates an image retaining lines and parallelism at the cost of distances and angles between objects.
*
* @param bi The specified buffered image to undergo rotation.
* @param degrees The specified rotation to be applied.
* @since 0.0.8
*/
public static void rotateImageEuclidianAffine(BufferedImage bi, double degrees) {
double rotationRequired = Math.toRadians(degrees);
double locationX = bi.getWidth() / 2;
double locationY = bi.getHeight() / 2;
AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BICUBIC);
bi.getGraphics().drawImage(op.filter(bi, null), 0, 0, null);
}
}