-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package application; | ||
|
||
public class Config { | ||
public static final int IMAGE_MARGIN_TOP = 13; | ||
|
||
/* | ||
* Spacing between changed pixels is 13 | ||
* AND BECAUSE OF IT | ||
* SPACING_CIPHER has to be equal to 14 | ||
* | ||
* */ | ||
public static final int SPACING_CIPHER = 14; | ||
public static final int COLOR_BOOST = 87; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package application; | ||
|
||
import java.awt.Color; | ||
import java.util.*; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.awt.image.BufferedImage; | ||
import javax.imageio.ImageIO; | ||
|
||
public class Decrypter { | ||
public static String decrypt(String fileName) throws IOException { | ||
BufferedImage bufferedImage = ImageIO.read(new File(fileName)); | ||
|
||
int width = bufferedImage.getWidth(); | ||
String text = ""; | ||
|
||
ArrayList<Character> characters = new ArrayList<Character>(); | ||
|
||
for(int pixel = 0; pixel < width; pixel++){ | ||
if(pixel % Config.SPACING_CIPHER == 0){ | ||
Color colorShade = new Color(bufferedImage.getRGB(pixel, Config.IMAGE_MARGIN_TOP), true); | ||
char character = (char) colorShade.getGreen(); | ||
characters.add(character); | ||
} | ||
} | ||
|
||
for(char character : characters) | ||
text += character; | ||
|
||
return text; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package application; | ||
|
||
import java.awt.Color; | ||
import java.util.*; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.awt.image.BufferedImage; | ||
import javax.imageio.ImageIO; | ||
|
||
public class Encrypter { | ||
public static void encrypt(String fileName, String text) throws IOException { | ||
int width = 0; | ||
int index = 0; | ||
|
||
File f = null; | ||
|
||
ArrayList<Character> chars = new ArrayList<Character>(); | ||
ArrayList<Integer> asciiChars = new ArrayList<Integer>(); | ||
BufferedImage originalImage = ImageIO.read(new File(fileName)); | ||
|
||
for (char c : text.toCharArray()) | ||
chars.add(c); | ||
|
||
for(char c : chars) | ||
asciiChars.add(Character.getNumericValue(c)); | ||
|
||
width = originalImage.getWidth(); | ||
|
||
for(int x = 0; x < width; x++){ | ||
if(x % Config.SPACING_CIPHER == 0 && index < asciiChars.size()){ | ||
int iCharacter = asciiChars.get(index) + Config.COLOR_BOOST; | ||
Color greenShade = new Color(1, iCharacter, 1); | ||
originalImage.setRGB(x, Config.IMAGE_MARGIN_TOP, greenShade.getRGB()); | ||
index++; | ||
} | ||
} | ||
|
||
try{ | ||
f = new File("output.png"); | ||
ImageIO.write(originalImage, "png", f); | ||
}catch(IOException e){ | ||
System.out.println("Error: " + e); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package application; | ||
|
||
import java.io.IOException; | ||
|
||
import javafx.application.Application; | ||
import javafx.collections.ObservableList; | ||
import javafx.event.ActionEvent; | ||
import javafx.event.EventHandler; | ||
import javafx.geometry.Insets; | ||
import javafx.stage.Stage; | ||
import javafx.scene.Scene; | ||
import javafx.scene.control.Button; | ||
import javafx.scene.control.Label; | ||
import javafx.scene.control.Separator; | ||
import javafx.scene.control.TextField; | ||
import javafx.scene.layout.HBox; | ||
import javafx.scene.layout.VBox; | ||
|
||
public class Main extends Application { | ||
|
||
TextField fileName; | ||
TextField fieldTextToEncrypt; | ||
VBox vbox; | ||
HBox hbox; | ||
Separator separator; | ||
Button btnDecrypt; | ||
Button btnEncrypt; | ||
Label lblTextToEncrypt; | ||
Label lblResult; | ||
|
||
ObservableList hList; | ||
ObservableList list; | ||
|
||
public static void main(String[] args) { | ||
launch(args); | ||
} | ||
|
||
@Override | ||
public void start(Stage myStage) throws Exception { | ||
myStage.setTitle("Image Cipher (alpha)"); | ||
|
||
vbox = new VBox(); | ||
hbox = new HBox(); | ||
|
||
lblResult = new Label("Result: "); | ||
btnDecrypt = new Button("Decrypt"); | ||
btnEncrypt = new Button("Encrypt"); | ||
lblTextToEncrypt = new Label("Text: "); | ||
separator = new Separator(); | ||
fileName = new TextField(); | ||
fieldTextToEncrypt = new TextField(); | ||
|
||
fileName.setPromptText("Image name to process: "); | ||
fileName.setText("some_image.png"); | ||
fileName.setPrefColumnCount(15); | ||
|
||
fieldTextToEncrypt.setPromptText("Text to encrypt"); | ||
fieldTextToEncrypt.setPrefColumnCount(40); | ||
|
||
hbox.setSpacing(10); | ||
hbox.setMargin(lblTextToEncrypt, new Insets(1, 1, 1, 20)); | ||
hbox.setMargin(fieldTextToEncrypt, new Insets(1, 1, 1, 1)); | ||
|
||
vbox.setSpacing(10); | ||
vbox.setMargin(btnEncrypt, new Insets(0, 10, 0, 20)); | ||
vbox.setMargin(fileName, new Insets(20, 20, 5, 20)); | ||
vbox.setMargin(btnDecrypt, new Insets(5, 5, 5, 20)); | ||
vbox.setMargin(separator, new Insets(10, 10, 10, 10)); | ||
vbox.setMargin(lblResult, new Insets(10, 10, 10, 10)); | ||
|
||
separator.setPrefWidth(180); | ||
|
||
hList = hbox.getChildren(); | ||
hList.addAll(lblTextToEncrypt, fieldTextToEncrypt); | ||
|
||
list = vbox.getChildren(); | ||
list.addAll(fileName, btnDecrypt, separator, hbox, btnEncrypt, lblResult); | ||
|
||
btnDecrypt.setOnAction(new EventHandler<ActionEvent>() { | ||
@Override | ||
public void handle(ActionEvent e){ | ||
try{ | ||
lblResult.setText(Decrypter.decrypt(fileName.getText())); | ||
} catch (IOException error) { | ||
System.out.println("Error: " + error); | ||
} | ||
} | ||
}); | ||
|
||
btnEncrypt.setOnAction(new EventHandler<ActionEvent>() { | ||
@Override | ||
public void handle(ActionEvent e){ | ||
try{ | ||
Encrypter.encrypt(fileName.getText(), fieldTextToEncrypt.getText()); | ||
} catch (IOException error){ | ||
System.out.println("Error: " + error); | ||
} | ||
} | ||
}); | ||
|
||
Scene myScene = new Scene(vbox, 600, 300); | ||
myStage.setScene(myScene); | ||
myStage.setMinHeight(300); | ||
myStage.setMinWidth(600); | ||
|
||
myStage.show(); | ||
} | ||
} |