-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImageHiding.java
315 lines (220 loc) · 7.37 KB
/
ImageHiding.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* Sean Lachhander
* */
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ImageHiding extends JFrame implements ActionListener
{
BufferedImage hostImage;
//BufferedImage secretImage;
JPanel controlPanel;
JPanel imagePanel;
JTextField encodeBitsText;
JButton encodeBitsPlus;
JButton encodeBitsMinus;
JTextField noChars;
JTextField nBitsText;
JButton nBitsPlus;
JButton nBitsMinus;
ImageCanvas hostCanvas;
ImageCanvas secretCanvas;
Steganography s;
public BufferedImage getHostImage()
{
BufferedImage img = null;
try
{
img = ImageIO.read(new File("host_image.jpg"));
}
catch (IOException ioe) { ioe.printStackTrace(); }
return img;
}
/*public BufferedImage getSecretImage() // Commented out because do not need the secret image.
{
BufferedImage img = null;
try
{
img = ImageIO.read(new File("secret_image.jpg"));
}
catch (IOException ioe) { ioe.printStackTrace(); }
return img;
} */
public byte[] getTextBytes() // Function I implemented into ImageHiding.java
{
byte[] bFile = null; // null byte array
try
{
File file = new File("text.txt"); // text file store in file
// BufferedReader reader = new BufferedReader(new FileReader(file));
FileInputStream FIS = null;
bFile = new byte[(int)file.length()]; // bFile byte length of txt file
FIS = new FileInputStream(file); // in fileinputstream
FIS.read(bFile); // read
FIS.close(); // close file
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
return bFile;
}
public int getBits()
{
return Integer.parseInt(encodeBitsText.getText());
}
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if (source == encodeBitsPlus)
{
int bits = this.getBits() + 1;
if (bits > 8) { bits = 8; }
encodeBitsText.setText(Integer.toString(bits));
int width = this.getHostImage().getWidth();
int height = this.getHostImage().getHeight();
noChars.setText(Integer.toString((bits*(3*width*height))/8)); // number of characters to string
s = new Steganography(this.getHostImage());
s.encode(this.getTextBytes(), bits);
hostCanvas.setImage(s.getImage());
hostCanvas.repaint();
// s = new Steganography(this.getSecretImage());
// s.getMaskedImage(bits);
// secretCanvas.setImage(s.getImage());
// secretCanvas.repaint();
}
else if (source == encodeBitsMinus)
{
int width = this.getHostImage().getWidth();
int height = this.getHostImage().getHeight();
int bits = this.getBits() - 1;
if (bits < 0) { bits = 0; }
noChars.setText(Integer.toString((bits*(3*width*height))/8));
encodeBitsText.setText(Integer.toString(bits));
s = new Steganography(this.getHostImage());
s.encode(this.getTextBytes(), bits);
hostCanvas.setImage(s.getImage());
hostCanvas.repaint();
// s = new Steganography(this.getSecretImage());
// s.getMaskedImage(bits);
// secretCanvas.setImage(s.getImage());
// secretCanvas.repaint();
}
}
public ImageHiding()
{
GridBagLayout layout = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
this.setTitle("Image Hiding Demo");
Container container = this.getContentPane();
this.setLayout(layout);
this.add(new JLabel("Bits to encode into host image:"));
encodeBitsText = new JTextField("0", 5);
encodeBitsText.setEditable(false);
noChars = new JTextField("0", 5);
noChars.setEditable(false);
gbc.weightx = -1.0;
layout.setConstraints(encodeBitsText, gbc);
this.add(encodeBitsText);
gbc.weightx = 1.0;
layout.setConstraints(noChars, gbc);
this.add(noChars);
encodeBitsPlus = new JButton("+");
encodeBitsPlus.addActionListener(this);
encodeBitsMinus = new JButton("-");
encodeBitsMinus.addActionListener(this);
gbc.weightx = 1.0;
layout.setConstraints(encodeBitsPlus, gbc);
this.add(encodeBitsPlus);
gbc.gridwidth = GridBagConstraints.REMAINDER;
layout.setConstraints(encodeBitsMinus, gbc);
this.add(encodeBitsMinus);
GridBagLayout imageGridbag = new GridBagLayout();
GridBagConstraints imageGBC = new GridBagConstraints();
imagePanel = new JPanel();
imagePanel.setLayout(imageGridbag);
JLabel hostImageLabel = new JLabel("Host image:");
JLabel secretImageLabel = new JLabel("No. of characters: ");
imagePanel.add(hostImageLabel);
imageGBC.gridwidth = GridBagConstraints.REMAINDER;
imageGridbag.setConstraints(secretImageLabel, imageGBC);
imagePanel.add(secretImageLabel);
hostCanvas = new ImageCanvas(this.getHostImage());
//secretCanvas = new ImageCanvas(this.getSecretImage());
imagePanel.add(hostCanvas);
// imagePanel.add(secretCanvas);
gbc.gridwidth = GridBagConstraints.REMAINDER;
layout.setConstraints(imagePanel, gbc);
this.add(imagePanel);
Steganography host = new Steganography(this.getHostImage());
host.encode(this.getTextBytes(), this.getBits());
hostCanvas.setImage(host.getImage());
// Steganography secret = new Steganography(this.getSecretImage());
// secret.getMaskedImage(this.getBits());
// secretCanvas.setImage(secret.getImage());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
public static void main(String[] args)
{
ImageHiding frame = new ImageHiding();
frame.setVisible(true);
}
public class ImageCanvas extends JPanel
{
Image img;
public void paintComponent(Graphics g)
{
g.drawImage(img, 0, 0, this);
}
public void setImage(Image img)
{
this.img = img;
}
public ImageCanvas(Image img)
{
this.img = img;
this.setPreferredSize(new Dimension(img.getWidth(this), img.getHeight(this)));
}
}
}
class Steganography
{
BufferedImage image;
public void getMaskedImage(int bits)
{
int[] imageRGB = image.getRGB(0, 0, image.getWidth(null), image.getHeight(null), null, 0, image.getWidth(null));
int maskBits = (int)(Math.pow(2, bits)) - 1 << (8 - bits);
int mask = (maskBits << 24) | (maskBits << 16) | (maskBits << 8) | maskBits;
for (int i = 0; i < imageRGB.length; i++)
{
imageRGB[i] = imageRGB[i] & mask;
}
image.setRGB(0, 0, image.getWidth(null), image.getHeight(null), imageRGB, 0, image.getWidth(null));
}
public void encode(byte[] textByteArray, int encodeBits) // encode will take in a byte array (text) instead of originally (BufferedImage)
{
int[] imageRGB = image.getRGB(0, 0, image.getWidth(null), image.getHeight(null), null, 0, image.getWidth(null));
int encodeByteMask = (int)(Math.pow(2, encodeBits)) - 1 << (8 - encodeBits);
int encodeMask = (encodeByteMask << 24) | (encodeByteMask << 16) | (encodeByteMask << 8) | encodeByteMask;
int decodeByteMask = ~(encodeByteMask >>> (8 - encodeBits)) & 0xFF;
int hostMask = (decodeByteMask << 24) | (decodeByteMask << 16) | (decodeByteMask << 8) | decodeByteMask;
for (int i = 0; i < imageRGB.length; i++)
{
int encodeData = ((char)textByteArray[i] & encodeMask) >>> (8 - encodeBits); // Will Encode the text byte array..
imageRGB[i] = (imageRGB[i] & hostMask) | (encodeData & ~hostMask); // stored into imageRGB[i]...
}
image.setRGB(0, 0, image.getWidth(null), image.getHeight(null), imageRGB, 0, image.getWidth(null));
}
public Image getImage()
{
return image;
}
public Steganography(BufferedImage image)
{
this.image = image;
}
}