-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageUtil.java
More file actions
56 lines (46 loc) · 1.73 KB
/
ImageUtil.java
File metadata and controls
56 lines (46 loc) · 1.73 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
package io.spatio;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
public class ImageUtil {
public static void saveAsPng100x100(BufferedImage source, String path) {
try {
int targetWidth = 100;
int targetHeight = 100;
BufferedImage resized = new BufferedImage(
targetWidth,
targetHeight,
BufferedImage.TYPE_INT_ARGB
);
Graphics2D g2d = resized.createGraphics();
// Qualité de redimensionnement (bonne pratique)
g2d.setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR
);
g2d.setRenderingHint(
RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY
);
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON
);
g2d.drawImage(source, 0, 0, targetWidth, targetHeight, null);
g2d.dispose();
ImageIO.write(resized, "png", new File(path));
} catch (IOException e) {
throw new RuntimeException("Erreur sauvegarde PNG 100x100", e);
}
}
public byte[] toBytes( java.awt.image.RenderedImage image) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
byte[] imageBytes = baos.toByteArray();
return imageBytes;
}
}