Skip to content

Instantly share code, notes, and snippets.

@spudtrooper
Created September 11, 2012 04:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spudtrooper/3696036 to your computer and use it in GitHub Desktop.
Save spudtrooper/3696036 to your computer and use it in GitHub Desktop.
Convert a JPEG image to Excel spreadsheet (e.g. http://bit.ly/QBd4HI)
import java.awt.*;
import java.awt.Image;
import java.awt.image.*;
import java.io.*;
import java.math.*;
import java.util.*;
import java.util.List;
import javax.imageio.*;
import javax.swing.*;
import java.lang.reflect.*;
import jxl.*;
import jxl.format.*;
import jxl.format.Colour;
import jxl.write.*;
import jxl.write.Label;
/**
* Outputs an Excel version of input images.
* You need this: http://jexcelapi.sourceforge.net.
*
* % javac -g -classpath .:jxl.jar ImageToXls.java
* % java -classpath .:jxl.jar ImageToXls <image>
*/
public final class ImageToXls {
public static void main(String[] args) {
System.exit(new ImageToXls().realMain(args));
}
private boolean verbose = false;
public int realMain(String[] args) {
if (args == null || args.length == 0) {
printHelp();
return 0;
}
try {
go(args);
} catch (Exception e) {e.printStackTrace(); return 1;}
return 0;
}
private void go(String[] args) throws Exception {
final List<String> images = new ArrayList<String>();
for (int i=0; i<args.length;) {
String arg = args[i++];
if (arg.startsWith("-v")) {
verbose = true;
} else if (arg.startsWith("-h")) {
printHelp();
return;
} else {
images.add(arg);
}
}
for (String img : images) convert(img);
}
private void convert(String image) throws Exception {
String output = image.replace(".jpg", ".xls");
note("image: " + image);
note("output: " + output);
//
// create an image and get the array of pixels
//
Image img = Toolkit.getDefaultToolkit().createImage(image);
JButton obs = new JButton(new ImageIcon(img));
int w = img.getWidth(obs);
int h = img.getHeight(obs);
note("size: " + w + "x" + h);
//
// get the pixels
//
int[] pixels = new int[w*h];
PixelGrabber pg = new PixelGrabber(img,0,0,w,h,pixels,0,w);
try {pg.grabPixels();} catch (Exception e) {e.printStackTrace();}
if ((pg.getStatus() & ImageObserver.ABORT) != 0) return;
//
// create the work book
//
WorkbookSettings ws = new WorkbookSettings();
ws.setLocale(new Locale("en", "EN"));
WritableWorkbook workbook = Workbook.createWorkbook(new File(output),ws);
WritableSheet sheet = workbook.createSheet(image,0);
//
// go over the imgage and write to the work book
//
WritableFont font = new WritableFont(WritableFont.ARIAL,4);
for (int i=0; i<h; i++) {
for (int j=0; j<w; j++) {
int c = pixels[i*w+j] & 0xffffff;
String loc = cellLoc(i,j);
WritableCellFormat format = new WritableCellFormat(font);
format.setShrinkToFit(true);
Colour col = findClosestColor(c);
format.setBackground(col);
Label lab = new Label(j,i,"",format);
sheet.addCell(lab);
}
}
//
// close
//
workbook.write();
workbook.close();
}
private Colour findClosestColor(int c) {
Colour res = null;
double min = Double.MAX_VALUE;
int val = -1;
final int red = (c >> 16) & 0xff;
final int green = (c >> 8) & 0xff;
final int blue = (c ) & 0xff;
for (Colour col : Colour.getAllColours()) {
RGB rgb = col.getDefaultRGB();
double diff = Math.sqrt(Math.pow(rgb.getRed() - red, 2) +
Math.pow(rgb.getGreen() - green, 2) +
Math.pow(rgb.getBlue() - blue, 2));
if (diff < min) {
res = col;
min = diff;
}
}
note(res + " -> " + Integer.toHexString(c));
return res;
}
private String cellLoc(int y, int x) {
String loc = "Sheet1!";
if (x < 26) {
loc += (char)('A'+x);
} else {
int first = x/26;
int rest = x%26;
loc += (char)('A' + first);
loc += (char)('A' + rest);
}
loc += y + 1;
return loc;
}
private void printHelp() {
e(getClass().getName() + " [options] [image]+");
e("where options include:");
e(" -h print this message");
e(" -v be loud");
}
private void e(String msg) {
System.err.println(msg);
}
private void note(Object msg) {
if (verbose) {
System.err.println("[" + getClass().getName() + "] " +
String.valueOf(msg));
}
}
}
@meocaonguyen
Copy link

hi spudtrooper,
please help me run this code is not? thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment