Reputation: 3
I'm trying to make a decoder for images in processing, but it returns þ no matter what I put in. Help would be much appreciated.
void setup() {
//size(255, 255);
colorMode(HSB);
String result = decode();
println(result);
saveStrings("output.txt", new String[]{result});
}
String decode() {
PImage img = loadImage("code.png");
//image(img, 0, 0);
img.loadPixels();
String decoded = "";
int[] chars = new int[255 * 255];
for (int i = 0; i < 255; i++) {
for (int j = 0; j < 255; j++) {
color col = img.pixels[i * 255 + j];//get(i, j);
if (hue(col) == 0 && saturation(col) == 0) {
chars[floor(hue(col) * 255 + saturation(col))] = j;
println((char) j);
}
}
}
decoded = new String(chars, 0, chars.length);
String[] list = decoded.split("\u0000");
StringBuilder builder = new StringBuilder();
for (String string : list) {
builder.append(string);
}
decoded = builder.toString();
return decoded;
}
Upvotes: 0
Views: 66
Reputation: 855
The main problem is likely at if (hue(col) == 0 && saturation(col) == 0) {
The next line will always have 0 for hue(col)
and saturation(col)
. This means you are always executing chars[0] = j;
, and your final result has only one character (the þ you described).
Upvotes: 1