-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPicCrypt.java
103 lines (80 loc) · 2.98 KB
/
PicCrypt.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
/**
* Hide messages inside of picture by slightly modifying the alpha value of certain pixels.
*
* @author Logan May
*
*/
import java.util.*;
import java.io.*;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.imageio.*;
import javax.swing.*;
public class PicCrypt {
public static BufferedImage getBufferedImage(String path) {
BufferedImage img = null;
try {
img = ImageIO.read(new File(path));
} catch (IOException e) {
System.out.println("Could not read path");
}
return img;
}
public static JFrame makeFrame(String title) {
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
return frame;
}
public static void main(String args[]) {
// Initialize JFrame
JFrame frame = makeFrame("Image");
// Initialize BufferedImage for original picture
BufferedImage original = getBufferedImage("matrix.jpg");
// Get it's height and width
int width = original.getWidth();
int height = original.getHeight();
// Initialize a buffered image to hold the picture containing the message
BufferedImage containsMessage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
// ----
containsMessage.getGraphics().drawImage(original, 0, 0, null);
// Get a string and turn it into an array of characters
String input = "ATTACKATDAWN";
char[] message = new char[input.length()];
for (int i = 0; i < input.length(); i++) {
message[i] = input.charAt(i);
}
// For each pixel in the first row of the picture, subtract the (int) value of the corresponding char from the
// message array. Effectively, we're storing the value of the char in the alpha value of that pixel
for (int x = 0; x < input.length(); x++) {
int y = 0;
int color = original.getRGB(x,y);
int alphaLess = (color & 0x00ffffff);
int alpha = (color & 0xff000000) >>> 24;
alpha -= (int) message[x];
int newalpha = (alpha << 24) | alphaLess;
containsMessage.setRGB(x,y,newalpha);
}
// Go through the pixels in the first row, get the (int) value of the char hidden in the alpha value of the pixel
// and turn it back into a string
StringBuilder sb = new StringBuilder();
for (int x = 0; x < input.length(); x++) {
int y = 0;
int color = containsMessage.getRGB(x,y);
int alpha = (color & 0xff000000) >>> 24;
int charValue = 255 - alpha;
sb.append((char) charValue);
}
// Print the string
System.out.println(sb.toString());
// Now prove that you can barely see a difference in the picture by printing the picture to the JFrame
// Initialize ImageIcon
ImageIcon icon = new ImageIcon(containsMessage);
// Initialize JLabel from icon
JLabel label = new JLabel(icon);
// Add label to frame
frame.add(label);
// Show frame
frame.pack();
}
}