Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.