-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathView.java
More file actions
406 lines (385 loc) · 13.2 KB
/
View.java
File metadata and controls
406 lines (385 loc) · 13.2 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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.event.*;
import javax.imageio.*;
import java.io.*;
import java.awt.image.BufferedImage;
/**
*
* Description
*
* @version 1.0 from 26/02/2018
* @author
*/
public class View extends JFrame implements ActionListener {
private final String imageS = "";//"Image: ";
private final String fileS = "";//"File: ";
private final int space = 8;
private int type = 0; //0: nothing, 1: text, 2: image
private int foundType = 0;
private String foundText = "";
private BufferedImage foundImg;
//private CardLayout layout = new CardLayout();
//private JPanel cardPanel = new JPanel(layout);
private Container cp;
private JMenuBar menuBar = new JMenuBar();
private JMenu menu = new JMenu("Menu");
private JMenuItem mIEncry = new JMenuItem("Encryption");
private JMenuItem mIDecry = new JMenuItem("Decryption");
private JButton btContainerChoose = new JButton("Choose");
private JButton btHideChoose = new JButton("Choose");
private JButton btEncry = new JButton("Encrypt");
private JButton btImageChooser = new JButton("Choose");
private JButton btDecry = new JButton("Decrypt");
private JButton btSave = new JButton("Save");
private JLabel lbCPath = new JLabel(imageS);
private JLabel lbHPath = new JLabel(fileS);
private JLabel lbContainer = new JLabel("Container:");
private JLabel lbHide = new JLabel("Hide:");
private JLabel lbOr = new JLabel("Or:");
private JLabel lbImage = new JLabel("Image:");
private JLabel lbResult = new JLabel();
private JTextArea taHide = new JTextArea();
private JFileChooser containerChooser = new JFileChooser();
private JFileChooser hideChooser = new JFileChooser();
private JFileChooser decryChooser = new JFileChooser();
private JFileChooser imgSaver = new JFileChooser();
private JFileChooser txtSaver = new JFileChooser();
private File containerFile;
private File hideFile;
private File decryFile;
private File directory = containerChooser.getCurrentDirectory();
private Steganography steg = new Steganography();
//private Object[] order = {lbContainer, btContainerChoose, lbHide, btHideChoose};
public View() {
super();
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
int frameWidth = 506;
int frameHeight = 335;
setSize(frameWidth, frameHeight);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2;
setLocation(x, y);
setTitle("Steganography");
setResizable(false);
cp = getContentPane();
cp.setLayout(null);
setJMenuBar(menuBar);
menuBar.add(menu);
menu.add(mIEncry);
menu.add(mIDecry);
mIEncry.addActionListener(this);
mIDecry.addActionListener(this);
containerChooser.setFileFilter(new FileNameExtensionFilter("Images (jpg, png)", "jpg", "JPG", "jpeg", "JPEG", "png", "PNG"));
containerChooser.setAcceptAllFileFilterUsed(false);
decryChooser.setFileFilter(new FileNameExtensionFilter("Images (jpg, png)", "jpg", "JPG", "jpeg", "JPEG", "png", "PNG"));
decryChooser.setAcceptAllFileFilterUsed(false);
hideChooser.setFileFilter(new FileNameExtensionFilter("Images (jpg, png)", "jpg", "JPG", "jpeg", "JPEG", "png", "PNG"));
hideChooser.setAcceptAllFileFilterUsed(false);
imgSaver.setFileFilter(new FileNameExtensionFilter("Image", "png"));
imgSaver.setAcceptAllFileFilterUsed(false);
txtSaver.setFileFilter(new FileNameExtensionFilter("Textfile", "txt"));
txtSaver.setAcceptAllFileFilterUsed(false);
taHide.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
if (taHide.getText().equals("")) {
lbHPath.setEnabled(true);
btHideChoose.setEnabled(true);
lbOr.setEnabled(true);
btEncry.setEnabled(false);
type = 0;
}
}
@Override
public void insertUpdate(DocumentEvent e) {
if (!taHide.getText().equals("")) {
lbHPath.setEnabled(false);
btHideChoose.setEnabled(false);
lbOr.setEnabled(false);
btEncry.setEnabled(true);
type = 1;
}
}
@Override
public void changedUpdate(DocumentEvent arg0) {
}
});
lbContainer.setBounds(space, space, 70, 16);
lbImage.setBounds(space, space, 70, 16);
btContainerChoose.setBounds(space, 32, 80, 25);
btContainerChoose.addActionListener(this);
lbCPath.setBounds(96, 32, 200, 25);
lbHide.setBounds(space, 71, 70, 16);
taHide.setBounds(space, 94, 200, 120);
taHide.setLineWrap(true);
lbOr.setBounds(216, 189, 25, 25);
btHideChoose.setBounds(249, 189, 80, 25);
lbHPath.setBounds(336, 189, 170, 25);
btEncry.setBounds(space, 230, 80, 30);
btEncry.addActionListener(this);
btImageChooser.setBounds(space, 32, 80, 25);
btImageChooser.addActionListener(this);
btDecry.setBounds(space, 65, 80, 30);
btDecry.addActionListener(this);
encryView();
btHideChoose.addActionListener(this);
lbResult.setBounds(space, 103, 465, 16);
btSave.setBounds(space, 127, 80, 25);
btSave.addActionListener(this);
setVisible(true);
}
// start methods
public static void main(String[] args) {
new View();
}
/**
* This method adds all items for the encryption-screen to the container and resets them.
*
*/
public void encryView() {
cp.removeAll();
cp.add(lbContainer);
cp.add(btContainerChoose);
lbCPath.setText(this.imageS);
cp.add(lbCPath);
lbHide.setEnabled(false);
cp.add(lbHide);
taHide.setEnabled(false);
taHide.setText("");
cp.add(taHide);
lbOr.setEnabled(false);
cp.add(lbOr);
btHideChoose.setEnabled(false);
cp.add(btHideChoose);
lbHPath.setText(this.fileS);
lbHPath.setEnabled(false);
cp.add(lbHPath);
btEncry.setEnabled(false);
cp.add(btEncry);
reset(containerChooser);
reset(hideChooser);
cp.repaint();
validate();
this.type = 0;
}
/**
* This method adds all items for the decryption-screen to the container and resets them.
*
*/
public void decryView() {
cp.removeAll();
cp.add(lbImage);
cp.add(btImageChooser);
lbCPath.setText(this.imageS);
cp.add(lbCPath);
btDecry.setEnabled(false);
cp.add(btDecry);
lbResult.setText("");
cp.add(lbResult);
btSave.setEnabled(false);
cp.add(lbResult);
btSave.setEnabled(false);
cp.add(btSave);
this.foundImg = null;
this.foundText = "";
this.foundType = 0;
cp.repaint();
validate();
}
/**
* This method resets the selected file and directory of a FileChooser.
*
* @param chooser the JFileChooser that will be resetted.
*/
public void reset(JFileChooser chooser) {
chooser.setSelectedFile(new File(""));
chooser.setCurrentDirectory(this.directory);
}
/**
* This method handles all the ActionListener of the Buttons.
*
*/
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == this.btContainerChoose) {
this.containerFile = openFile(containerChooser);
if (this.containerFile != null) {
lbCPath.setText(this.imageS + this.containerFile.getName());
taHide.setText("");
btHideChoose.setEnabled(true);
taHide.setEnabled(true);
lbHide.setEnabled(true);
lbHPath.setEnabled(true);
lbOr.setEnabled(true);
}
} else if (source == this.btHideChoose){
this.hideFile = openFile(hideChooser);
if (this.hideFile != null) {
lbHPath.setText(this.fileS + this.hideFile.getName());
taHide.setEnabled(false);
btEncry.setEnabled(true);
String ending = getType(this.hideFile);
System.out.println(ending);
//if (ending.equals("png") || ending.equals("PNG") || ending.equals("jpg") || ending.equals("JPG") || ending.equals("jpeg")) {
this.type = 2;
//}
}
} else if (source == this.btEncry) {
System.out.println(this.type);
BufferedImage container = readImg(this.containerFile);
switch (this.type) {
case 1:
if (this.steg.hideText(container, taHide.getText())) {
export(container, saveFile(imgSaver));
reset(imgSaver);
}
break;
case 2:
if (container != null) {
if (this.steg.hideImage(container, readImg(this.hideFile))) {
export(container, saveFile(imgSaver));
reset(imgSaver);
}
}
break;
}
} else if (source == this.mIEncry) {
encryView();
} else if (source == this.mIDecry) {
decryView();
} else if (source == this.btImageChooser){
this.decryFile = openFile(decryChooser);
if (this.decryFile != null) {
lbCPath.setText(this.imageS + this.decryFile.getName());
btDecry.setEnabled(true);
}
} else if (source == this.btDecry) {
if (decryFile != null) {
BufferedImage decryImg = readImg(decryFile);
this.foundType = this.steg.getType(decryImg);
switch (this.foundType) {
case 1:
System.out.println("Text found");
this.foundText = this.steg.discoverText(decryImg);
lbResult.setText("Found Text: " + this.foundText);
btSave.setEnabled(true);
break;
case 2:
System.out.println("Image found");
this.foundImg = this.steg.discoverImage(decryImg);
lbResult.setText("Found Image: " + this.foundImg.getWidth() + " x " + this.foundImg.getHeight());
btSave.setEnabled(true);
break;
}
}
} else if (source == this.btSave) {
switch (this.foundType) {
case 1:
export(this.foundText, saveFile(txtSaver));
reset(txtSaver);
break;
case 2:
export(this.foundImg, saveFile(imgSaver));
reset(imgSaver);
break;
}
}
}
/**
* This method lets the user load a file from a directory using a JFileChooser.
*
* @param chooser the JFileChooser that is being used for the loading
*/
public File openFile(JFileChooser chooser) {
int dialog = chooser.showOpenDialog(this);
if (dialog == JFileChooser.APPROVE_OPTION) {
return chooser.getSelectedFile();
} else if (dialog == JFileChooser.CANCEL_OPTION) {
return chooser.getSelectedFile();
} else {
return new File("");
}
}
/**
* This method lets the user pick a file in a directory using a JFileChooser for saving a file.
*
* @param chooser the JFileChooser that is being used
*/
public File saveFile(JFileChooser chooser) {
int dialog = chooser.showSaveDialog(this);
if (dialog == JFileChooser.APPROVE_OPTION) {
return chooser.getSelectedFile();
} else if (dialog == JFileChooser.CANCEL_OPTION) {
return chooser.getSelectedFile();
} else {
return new File("");
}
}
/**
* This method reads an image from a path and repaints it to an "argb"-type BufferedImage.
*
* @param inputFile the file to the image
*/
public BufferedImage readImg(File inputFile) {
System.out.println(" - Lesen des Bildes in " + inputFile.getPath());
BufferedImage img = null;
System.out.println(" - Umschreiben in ein BufferedImage im ARGB-Type - ");
try {
BufferedImage bildIn = ImageIO.read(inputFile);
img = new BufferedImage(bildIn.getWidth(), bildIn.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
g.drawImage(bildIn, 0, 0, null);
g.dispose();
} catch(IOException e) {
System.out.println(e);
}
return img;
}
/**
* This method exports a BufferedImage to an imagefile.
*
* @param img the BufferedImage that should be exported
* @param outputFile the File in which the image should be saved
*/
public void export(BufferedImage img, File outputFile) {
try {
ImageIO.write(img, "png", outputFile);
} catch(IOException e){
System.out.println(e);
}
}
/**
* This method saves a String to a textfile.
*
* @param text the String that should be saved
* @param outputFile the File in which the text should be saved
*/
public void export(String text, File outputFile) {
PrintWriter out = null;
try {
out = new PrintWriter(outputFile);
out.println(text);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (out != null)
out.close();
}
}
/**
* This method gives back the ending/format of a file.
*
* @param f the File of which the type should be given back
*/
public String getType(File f) {
String name = f.getName();
String ending = "";
if (f.length() > 4)
ending = name.substring(name.length() - 3, name.length());
return ending;
}
}