實際功用不多,因為生成頁面超大,只是用來消遣而已。
/**
* by fy.zhang
* to convert a image to a html table
*/
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import javax.imageio.ImageIO;
public class TransImageToHtml {
private static String toHexString(int v) {
String result = Integer.toHexString(v).toUpperCase();
if (result.length() <= 1) {
result = "0" + result;
}
return result;
}
public static void main(String[] args) {
if (args != null && args.length == 1) {
String fileName = args[0].toUpperCase();
if (fileName.endsWith(".JPG") || fileName.endsWith(".PNG")
|| fileName.endsWith(".GIF") || fileName.endsWith(".BMP")) {
try {
StringBuilder code = new StringBuilder();
BufferedImage image = ImageIO.read(new File(fileName));
int width = image.getWidth();
int height = image.getHeight();
// int pixcels[][] = new int[height][width];
int pixcel = 0;
// int alpha = 0;
int red = 0;
int green = 0;
int blue = 0;
code.append("<table width=" + width + " height=" + height
+ " border=0 cellpadding=0 cellspacing=0>/n");
for (int i = 0; i < height - 1; i++) {
code.append("/t<tr>/n");
for (int j = 0; j < width - 1; j++) {
// pixcels[i][j] = image.getRGB(i, j);
// System.out.println(pixcels[i][j]);
pixcel = image.getRGB(j, i);
// alpha = pixcel >> 24 & 0xff;
red = pixcel >> 16 & 0xff;
green = pixcel >> 8 & 0xff;
blue = pixcel & 0xff;
code
.append("/t/t<td bgcolor=#" + toHexString(red)
+ toHexString(green) + toHexString(blue)
+ ">/n");
code.append("/t/t</td>/n");
}
code.append("/t</tr>/n");
}
code.append("</table>");
File toSave = new File(fileName + ".htm");
OutputStream stream = new FileOutputStream(toSave);
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(stream, "utf-8"));
writer.write(code.toString());
writer.close();
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("Suppor type : jpg, gif, png, bmp");
}
} else {
System.out
.println("Usage : java TransImageToHtml yourImageFile.jpg");
}
}
}
不禁想到把代碼轉回來,不是可以解決樓主的問題了嗎?
代碼如下:
/**
* by fy.zhang
* convert formatted table to image
*/
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.imageio.ImageIO;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class TransHtmlToImage {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
File f = new File(args[0]);
SAXBuilder dom = new SAXBuilder();
try {
Document doc = dom.build(f);
Element table = doc.getRootElement();
int rows = table.getChildren().size();
int columns = ((Element) table.getChildren().get(0)).getChildren()
.size();
BufferedImage image = new BufferedImage(columns + 1, rows + 1,
BufferedImage.TYPE_INT_BGR); //為什么要+1,還沒有搞清楚,但是讀出來確實是少了一個節點
List<Element> trs = table.getChildren();
int r = 0, c = 0;
for (Element tr : trs) {
List<Element> tds = tr.getChildren();
c = 0;
for (Element td : tds) {
String color = td.getAttribute("bgcolor").getValue();
color = color.substring(1, color.length());
int rgb = Integer.parseInt(color, 16);
image.setRGB(c, r, rgb);
c++;
}
r++;
}
ImageIO.write(image, "GIF", new File(args[0] + ".gif"));
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
但是仍然有一個問題,就是源文檔并不是標準的XML,所以需要用替換的方法將源代碼中的屬性欄位都加上雙引號。