Skip to content

Instantly share code, notes, and snippets.

@Andrewcpu
Created September 1, 2018 04:26
Show Gist options
  • Save Andrewcpu/475378ee45a7a54c953da25d738ecff9 to your computer and use it in GitHub Desktop.
Save Andrewcpu/475378ee45a7a54c953da25d738ecff9 to your computer and use it in GitHub Desktop.
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
public class Main {
public static void main(String[] args) throws Exception{
BufferedImage img = createImageWithID(255*3*3);
System.out.println(getImageID(img));
File outputfile = new File("C:\\Users\\stein\\Desktop\\encoded.png");
ImageIO.write(img, "png", outputfile);
}
public static BufferedImage createImageWithID(int id){
BufferedImage buffImg = new BufferedImage(300,300,BufferedImage.TYPE_INT_RGB);
Graphics graphics = buffImg.getGraphics();
graphics.setColor(Color.BLACK);
graphics.fillRect(0,0,buffImg.getWidth(),buffImg.getHeight());
graphics.setColor(Color.BLACK);
graphics.fillRect(0,0,buffImg.getWidth() / 5, buffImg.getWidth() / 5);
drawBlock(graphics,id,1,0);
return buffImg;
}
public static int drawBlock(Graphics g, int val, int x, int y){
double width = 300;
double pixelSize = width / 5;
int valLeft = val;
int red = 0, green = 0, blue = 0;
for(int i = 0; i<valLeft; i++){
if(blue < 255)
blue++;
else if(green < 255)
green++;
else if(red < 255)
red++;
else{
if((x + 1) * pixelSize < width){
drawBlock(g, val - (255 * 3), x + 1, y);
}
else{
drawBlock(g,val - (255 * 3), 0, y + 1);
}
break;
}
}
Color c = new Color(red,green,blue);
g.setColor(c);
g.fillRect((int)(x * pixelSize), (int)(y * pixelSize), (int)(pixelSize), (int)pixelSize);
return 0;
}
public static int getImageID(BufferedImage img){
int pixelSize = img.getWidth() / 5;
int num = 0;
for(int i = 0; i<5; i++){
for(int j = 0; j<5; j++){
int clr= img.getRGB(i * pixelSize,j * pixelSize);
int red = (clr & 0x00ff0000) >> 16;
int green = (clr & 0x0000ff00) >> 8;
int blue = clr & 0x000000ff;
num+= red + green + blue;
}
}
return num;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment