-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageUtil.java
More file actions
53 lines (48 loc) · 1.88 KB
/
Copy pathImageUtil.java
File metadata and controls
53 lines (48 loc) · 1.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
package mchat;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
public class ImageUtil {
// Load icon from classpath resource, e.g. "/images/luigi.png"
public static ImageIcon iconFromResource(String resourcePath, int w, int h) {
try {
java.net.URL url = ImageUtil.class.getResource(resourcePath);
if (url == null) {
System.err.println("Resource not found: " + resourcePath);
return new ImageIcon(new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB));
}
BufferedImage img = ImageIO.read(url);
Image scaled = img.getScaledInstance(w, h, Image.SCALE_SMOOTH);
return new ImageIcon(scaled);
} catch (IOException e) {
e.printStackTrace();
return new ImageIcon(new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB));
}
}
// For images pulled from the DB
public static ImageIcon iconFromBytes(byte[] data, int w, int h) {
if (data == null) return null;
try {
BufferedImage img = ImageIO.read(new ByteArrayInputStream(data));
Image scaled = img.getScaledInstance(w, h, Image.SCALE_SMOOTH);
return new ImageIcon(scaled);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
// Save an AWT image to a file
public static void saveImage(Image img, String path) throws IOException {
BufferedImage b = new BufferedImage(
img.getWidth(null),
img.getHeight(null),
BufferedImage.TYPE_INT_ARGB
);
Graphics2D g2 = b.createGraphics();
g2.drawImage(img, 0, 0, null);
g2.dispose();
ImageIO.write(b, "png", new File(path));
}
}