Skip to content

Instantly share code, notes, and snippets.

@vikrum
Created February 11, 2013 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vikrum/4758434 to your computer and use it in GitHub Desktop.
Save vikrum/4758434 to your computer and use it in GitHub Desktop.
Quick hack to dump an image to JSON.
package com.firebase.sandbox;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import org.json.JSONObject;
public class Png2Json {
public static void main(String[] args) {
try {
String f = "/Users/vikrum/Desktop/moo.png";
BufferedImage image = ImageIO.read(new File(f));
JSONObject json = new JSONObject();
for(int i = 0; i < image.getWidth(); i++) {
for(int j = 0; j < image.getHeight(); j++) {
Color color = new Color(image.getRGB(i, j));
if(color.getRGB() == -1)
continue;
String hex = String.format("%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue());
json.put(i + ":" + j, hex);
}
}
System.out.println(json.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment