-
Notifications
You must be signed in to change notification settings - Fork 0
/
HopefullyThisWorks.java
165 lines (128 loc) · 5.32 KB
/
HopefullyThisWorks.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
import java.awt.image.BufferedImage;
import java.awt.Color;
import javax.imageio.ImageIO;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
public class HopefullyThisWorks {
private static Scanner scan = new Scanner(System.in);
static String filePath;
static BufferedImage img;
static File imgFile;
//Message
static StringBuilder binaryMessage = new StringBuilder();
//Image colors
static ArrayList<Color> colors = new ArrayList<Color>();
static ArrayList<Color> finalColors = new ArrayList<Color>();
public static void main(String[] args) {
System.out.println("Give the full file path of an image. ");
filePath = scan.nextLine();
try{
imgFile = new File(filePath);
img = ImageIO.read(imgFile);
System.out.println(img);
}
catch(Exception e){
e.printStackTrace();
System.exit(0);
}
System.out.println("Are you encoding (0) or decoding (1)?");
boolean isEncoding = false;
if(isEncoding){
System.out.println("Please provide a message to encode into this image.");
String message = scan.nextLine();
binaryMessage.append(ConvertStringToBinary(message));
try {
ImageIO.write(editRGBVals(img, binaryMessage), "png", new File("C:/Users/james/Documents/img1.png"));
}
catch(Exception E){
E.printStackTrace();
System.exit(0);
}
} else {
System.out.println(binaryToMessage(new StringBuilder(getMessageBack(img))));
}
/*
getRGBVals(img);
for(int i = 0; i < colors.size(); i++){
System.out.println(colors.get(i));
}
*/
}
//Store binary string in least significant bits of R, G, and B
//Convert the numbers for RGB to binary
public static BufferedImage editRGBVals(BufferedImage img, StringBuilder message){
for(int i = 0; i < img.getHeight(); i++){
for(int j = 0; j < img.getWidth(); j++){
Color tempColor = new Color(img.getRGB(j, i));
int blue = tempColor.getBlue();
if(!(message.length() == 0)){
int injectedChar = Integer.parseInt(message.substring(0,1));
System.out.println(blue);
blue = blue - blue % 10;
blue = blue + injectedChar;
System.out.println(blue);
message = message.deleteCharAt(0);
img.setRGB(j, i, new Color(tempColor.getRed(), tempColor.getGreen(), blue).getRGB());
}
else{
return img;
}
}
}
return img;
}
//get the message back
public static String getMessageBack(BufferedImage img){
StringBuilder message = new StringBuilder();
for(int i = 0; i < img.getHeight(); i++){
for(int j = 0; j < img.getWidth(); j++){
Color tempColor = new Color(img.getRGB(j,i));
int blue = tempColor.getBlue() % 10;
if((blue != 0 && blue != 1) || (message.length() >= 8 && message.substring(message.length() - 8).equals("00000100"))){
return message.toString();
}
message.append(blue);
}
}
return message.toString();
}
//Sets the rgb values to the buffered image
//IMPORTANT: THIS METHOD NEEDS TO STOP WHEN THE ARRAY IS EMPTY. CHECK OTHER METHODS TO MAKE SURE THEY ARENT DOING STUPID THINGS EITHER
//good method
//Getting the characters of the message back from binary
//good method
public static String ConvertStringToBinary(String MESSAGE) {
StringBuilder binaryResult = new StringBuilder();
for (char c : MESSAGE.toCharArray()){
String binString = Integer.toBinaryString((int) c);
binaryResult.append(setASCIIBits(binString, 8));
}
binaryResult.append("00000100");
System.out.println(binaryResult.toString());
return binaryResult.toString();
}
//good method
//Sets number of bits for each block. If I decide to use unicode at any point (hopefully not) this will be useful
public static String setASCIIBits(String inputBinary, int blockSize){
StringBuilder finalString = new StringBuilder();
for(int i = 0; i < blockSize; i++){
finalString.append("0");
}
finalString.append(inputBinary);
finalString.delete(0, finalString.length() - blockSize);
return finalString.toString();
}
//Getting the characters of the message back from binary
public static String binaryToMessage(StringBuilder binary){
int messageLength = (binary.length() / 8);
char[] chars = new char[messageLength];
StringBuilder decodedMessage = new StringBuilder();
for(int i = 0; i < messageLength; i++){
chars[i] = (char) Integer.parseInt(binary.substring(0,8), 2);
binary.delete(0,8);
}
decodedMessage.append(chars, 0, chars.length);
return decodedMessage.toString();
}
}