-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUtils.java
74 lines (65 loc) · 2.31 KB
/
Utils.java
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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class Utils {
private Utils() {}
/*
* Converts given file image into a gray-scale image
*/
public static BufferedImage toGrayScale(BufferedImage img) throws Exception{
BufferedImage gray = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
gray.getGraphics().drawImage(img, 0 , 0, null);
return gray;
}
/*
* Changes the brightness of an image by the given factor
*/
public static BufferedImage editBrightness(float brightenFactor, BufferedImage image){
RescaleOp rOp = new RescaleOp(brightenFactor, 0, null);
image = rOp.filter(image, image);
return image;
}
/*
* Maps the value between start1 and end1 to a value between start2 and end2
*/
public static double map(double value, double start1, double end1, double start2, double end2) {
double ratio = (end2 - start2) / (end1 - start1);
return ratio * (value - start1) + start2;
}
/*
* Writes on disk the given image
*/
public static void writeImage(BufferedImage img, String path) throws IOException {
File out = new File(path);
ImageIO.write(img, "png", out);
}
/*
* Displays an image given its path
*/
public static void displayImage(String imagePath, String title) throws IOException {
File file = new File(imagePath);
BufferedImage image = ImageIO.read(file);
JFrame f = new JFrame(title);
ImageIcon pic = new ImageIcon(imagePath);
JLabel label;
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
panel.setBackground(Color.WHITE);
label = new JLabel ("", pic, SwingConstants.CENTER);
panel.add(label);
f.setVisible(true);
f.setResizable(false);
f.add(panel);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}