Created
February 11, 2013 23:04
-
-
Save vikrum/4758434 to your computer and use it in GitHub Desktop.
Quick hack to dump an image to JSON.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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