Skip to content

Instantly share code, notes, and snippets.

@Wieku
Created June 22, 2016 18:32
Show Gist options
  • Save Wieku/55e4d73582fbadd1d05deb8c6a8d4d9b to your computer and use it in GitHub Desktop.
Save Wieku/55e4d73582fbadd1d05deb8c6a8d4d9b to your computer and use it in GitHub Desktop.
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
if(args.length < 2) {
System.out.println("Not enough arguments given!");
System.out.println("\"command encode [PATH]\" - to encode text and save to given path");
System.out.println("\"command decode [PATH]\" - to decode given file and print to console");
System.exit(0);
}
try {
String tempString = "";
ArrayList<Integer> colors = new ArrayList<>();
BufferedImage img;
if(args[0].equals("encode")) {
Scanner in = new Scanner(System.in);
loop:
for (;;) {
System.out.println("Write something in ASCII:");
String input = in.nextLine();
for(char har : input.toCharArray()) {
String numR = getNumber(har);
if(numR==null) {
System.out.println("Text must only contain ASCII characters!");
continue loop;
}
tempString += numR;
}
int rgb=0, stg=0;
while (tempString.length() > 0) {
int num = Integer.valueOf(String.format("%1$-8s",tempString.substring(0,Math.min(8, tempString.length()))).replace(" ", "0"), 2);
tempString = tempString.substring(Math.min(8, tempString.length()));
if(stg==3) {
colors.add(rgb | num<<24);
rgb = 0; stg = -1;
} else rgb |= num << (8*(2-stg));
++stg;
}
if(stg != 0)
colors.add(rgb);
int size = (int)Math.sqrt(colors.size());
int size2 = size + (Math.sqrt(colors.size())-size>0?1:0);
img = new BufferedImage(size, size2, BufferedImage.TYPE_INT_ARGB);
for(int y=0, i=0; y<size2; y++) {
for(int x=0; x<size; x++) {
img.setRGB(x, y, colors.get(i));
if(++i>=colors.size()) break;
}
}
ImageIO.write(img, "png", new File(args[1]));
System.out.println("Image saved as: " + new File(args[1]).getName());
in.close();
break;
}
} else {
try {
img = ImageIO.read(new URL(args[1]));
} catch (MalformedURLException r) {
img = ImageIO.read(new File(args[1]));
}
for(int i=0; i < img.getHeight(); i++)
for (int j = 0; j < img.getWidth(); j++)
tempString += get8Number((img.getRGB(j, i) >> 16) & 0xFF) + get8Number((img.getRGB(j, i) >> 8) & 0xFF)
+ get8Number(img.getRGB(j, i) & 0xFF) + get8Number((img.getRGB(j, i) >> 24) & 0xFF);
while (tempString.length() > 0) {
System.out.print((char)(int)Integer.valueOf(tempString.substring(0, Math.min(7, tempString.length())), 2));
tempString = tempString.substring(Math.min(7, tempString.length()));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getNumber(char chr) {
return ((int)chr)>=0&&((int)chr)<=127?String.format("%7s", Integer.toBinaryString((int)chr)).replace(" ", "0"):null;
}
public static String get8Number(int num) {
return String.format("%8s", Integer.toBinaryString(num)).replace(" ", "0");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment