-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageConverter.java
123 lines (99 loc) · 4.65 KB
/
ImageConverter.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
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
import java.awt.image.BufferedImage;
import java.awt.Color;
import java.awt.*;
import javax.swing.*;
/*******************************************************************************************************************
* ImageConverter will turn a normal image to a greyscale image, and a greyscale image to a binary image.
*******************************************************************************************************************/
class ImageConverter {
/*******************************************************************************************************************
* colorToRGB will be used in GreyscaleImage to operate on each pixel.
* @param alpha
* @param red
* @param green
* @param blue
*******************************************************************************************************************/
private static int colorToRGB(int alpha, int red, int green, int blue) {
int nPixel = 0;
nPixel += alpha;
nPixel = nPixel << 8;
nPixel += red;
nPixel = nPixel << 8;
nPixel += green;
nPixel = nPixel << 8;
nPixel += blue;
return nPixel;
}
/*******************************************************************************************************************
* GreyscaleImage will take in an image and turn it to greyscale.
* @param imageToRead - Image to greyscale.
*******************************************************************************************************************/
public static BufferedImage GreyscaleImage(BufferedImage imageToRead) {
int alpha, red, green, blue;
int nPixel;
BufferedImage lum = new BufferedImage(imageToRead.getWidth(), imageToRead.getHeight(), imageToRead.getType());
for (int i = 0; i < imageToRead.getWidth(); i++) {
for (int j = 0; j < imageToRead.getHeight(); j++) {
// Get pixels by R, G, B
alpha = new Color(imageToRead.getRGB(i, j)).getAlpha();
red = new Color(imageToRead.getRGB(i, j)).getRed();
green = new Color(imageToRead.getRGB(i, j)).getGreen();
blue = new Color(imageToRead.getRGB(i, j)).getBlue();
red = (int) (0.21 * red + 0.71 * green + 0.07 * blue);
// Return back to original format
nPixel = colorToRGB(alpha, red, red, red);
// Write pixels into image
lum.setRGB(i, j, nPixel);
}
}
imageToRead = lum;
lum = binarize(lum);
BufferedImage newImage = new BufferedImage(
imageToRead.getWidth() + lum.getWidth(),
imageToRead.getHeight(),
imageToRead.getType());
Graphics2D g = (Graphics2D) newImage.getGraphics();
g.drawImage(imageToRead, 0, 0, null);
g.drawImage(lum, imageToRead.getWidth(), 0, null);
JFrame editorFrame = new JFrame();
ImageIcon imageIcon = new ImageIcon(newImage);
JLabel jLabel = new JLabel();
jLabel.setIcon(imageIcon);
editorFrame.getContentPane().add(jLabel, BorderLayout.CENTER);
editorFrame.pack();
editorFrame.setLocationRelativeTo(null);
editorFrame.setVisible(true);
return lum;
}
/*******************************************************************************************************************
* Binarize will take in a greyscale image and turn it into a binary image.
* @param imageToRead - Must be a greyscale image. It will turn it into a binary image.
*******************************************************************************************************************/
private static BufferedImage binarize(BufferedImage imageToRead) {
int red;
int newPixel;
int threshold = 100;
BufferedImage binarized = new BufferedImage(imageToRead.getWidth(), imageToRead.getHeight(), imageToRead.getType());
for (int i = 0; i < imageToRead.getWidth(); i++)
{
for (int j = 0; j < imageToRead.getHeight(); j++)
{
// Get pixels
red = new Color(imageToRead.getRGB(i, j)).getRed();
int alpha = new Color(imageToRead.getRGB(i, j)).getAlpha();
if (red > threshold)
{
newPixel = 255;
}
else
{
newPixel = 0;
}
newPixel = colorToRGB(alpha, newPixel, newPixel, newPixel);
binarized.setRGB(i, j, newPixel);
}
}
imageToRead = binarized;
return imageToRead;
}
}