將圖片轉換為HTML TABLE
来源:互联网 发布:js获取select值 编辑:程序博客网 时间:2024/04/29 13:07
實際共用不多,因為生成頁面超大,只是用來消遣而已。
- /**
- * 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,所以需要用替換的方法將源代碼中的屬性欄位都加上雙引號。
- 將圖片轉換為HTML TABLE
- html table
- HTML TABLE
- Html Table
- HTML table
- HTML table
- HTML table
- html table
- html --table
- php create html-table
- HTML Color Table
- Table HTML Tags from
- html table排序
- 动态操作html table
- JAVASCRIPT 操作HTML TABLE
- html動態增刪table
- html table 对齐
- html 圆角table
- 与Boss相处更好的10种方法
- pictureBox上透明Label的实现
- 一些很酷的.Net技巧
- 今天笔试记录
- 从A股暴跌看中国经济发展
- 將圖片轉換為HTML TABLE
- 自动化测试之---Expect交互实例
- Chapter 2 The first steps in Vim
- 技术站点推荐
- 用jdom解析applicationContext.xml出错
- 用VFP读写*.INI 文件
- bfin-xxx-gcc-4.3.1中config.status的生成及运行(7):创建临时目录
- amoeba for mysql 0.22 发布
- 重写html代码当中的img标签内容,图片地址不变