將圖片轉換為HTML TABLE

来源:互联网 发布:js获取select值 编辑:程序博客网 时间:2024/04/29 13:07

 

 

實際共用不多,因為生成頁面超大,只是用來消遣而已。

  1. /**
  2.  * by fy.zhang
  3.  * to convert a image to a html table
  4.  */
  5. import java.awt.image.BufferedImage;
  6. import java.io.BufferedWriter;
  7. import java.io.File;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. import java.io.OutputStream;
  11. import java.io.OutputStreamWriter;
  12. import javax.imageio.ImageIO;
  13. public class TransImageToHtml {
  14.     private static String toHexString(int v) {
  15.         String result = Integer.toHexString(v).toUpperCase();
  16.         if (result.length() <= 1) {
  17.             result = "0" + result;
  18.         }
  19.         return result;
  20.     }
  21.     
  22.     public static void main(String[] args) {
  23.         if (args != null && args.length == 1) {
  24.             String fileName = args[0].toUpperCase();
  25.             if (fileName.endsWith(".JPG") || fileName.endsWith(".PNG")
  26.                     || fileName.endsWith(".GIF") || fileName.endsWith(".BMP")) {
  27.                 try {
  28.                     StringBuilder code = new StringBuilder();
  29.                     BufferedImage image = ImageIO.read(new File(fileName));
  30.                     int width = image.getWidth();
  31.                     int height = image.getHeight();
  32.                     // int pixcels[][] = new int[height][width];
  33.                     int pixcel = 0;
  34.                     // int alpha = 0;
  35.                     int red = 0;
  36.                     int green = 0;
  37.                     int blue = 0;
  38.                     code.append("<table width=" + width + " height=" + height
  39.                             + " border=0 cellpadding=0 cellspacing=0>/n");
  40.                     for (int i = 0; i < height - 1; i++) {
  41.                         code.append("/t<tr>/n");
  42.                         for (int j = 0; j < width - 1; j++) {
  43.                             // pixcels[i][j] = image.getRGB(i, j);
  44.                             // System.out.println(pixcels[i][j]);
  45.                             pixcel = image.getRGB(j, i);
  46.                             // alpha = pixcel >> 24 & 0xff;
  47.                             red = pixcel >> 16 & 0xff;
  48.                             green = pixcel >> 8 & 0xff;
  49.                             blue = pixcel & 0xff;
  50.                             code
  51.                                     .append("/t/t<td bgcolor=#" + toHexString(red)
  52.                                     + toHexString(green) + toHexString(blue)
  53.                                             + ">/n");
  54.                             code.append("/t/t</td>/n");
  55.                         }
  56.                         code.append("/t</tr>/n");
  57.                     }
  58.                     code.append("</table>");
  59.                     File toSave = new File(fileName + ".htm");
  60.                     OutputStream stream = new FileOutputStream(toSave);
  61.                     BufferedWriter writer = new BufferedWriter(
  62.                             new OutputStreamWriter(stream, "utf-8"));
  63.                     writer.write(code.toString());
  64.                     writer.close();
  65.                     stream.close();
  66.                 } catch (IOException e) {
  67.                     // TODO Auto-generated catch block
  68.                     e.printStackTrace();
  69.                 }
  70.             } else {
  71.                 System.out.println("Suppor type : jpg, gif, png, bmp");
  72.             }
  73.         } else {
  74.             System.out
  75.                     .println("Usage : java TransImageToHtml yourImageFile.jpg");
  76.         }
  77.     }
  78. }

不禁想到把代碼轉回來,不是可以解決樓主的問題了嗎?

代碼如下:

  1. /**
  2.  * by fy.zhang
  3.  * convert formatted table to image
  4.  */
  5. import java.awt.image.BufferedImage;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.util.List;
  9. import javax.imageio.ImageIO;
  10. import org.jdom.Document;
  11. import org.jdom.Element;
  12. import org.jdom.JDOMException;
  13. import org.jdom.input.SAXBuilder;
  14. public class TransHtmlToImage {
  15.     @SuppressWarnings("unchecked")
  16.     public static void main(String[] args) {
  17.         File f = new File(args[0]);
  18.         SAXBuilder dom = new SAXBuilder();
  19.         try {
  20.             Document doc = dom.build(f);
  21.             Element table = doc.getRootElement();
  22.             int rows = table.getChildren().size();
  23.             int columns = ((Element) table.getChildren().get(0)).getChildren()
  24.                     .size();
  25.             BufferedImage image = new BufferedImage(columns + 1, rows + 1,
  26.                     BufferedImage.TYPE_INT_BGR);     //為什么要+1,還沒有搞清楚,但是讀出來確實是少了一個節點
  27.             List<Element> trs = table.getChildren();
  28.             int r = 0, c = 0;
  29.             for (Element tr : trs) {
  30.                 List<Element> tds = tr.getChildren();
  31.                 c = 0;
  32.                 for (Element td : tds) {
  33.                     String color = td.getAttribute("bgcolor").getValue();
  34.                     color = color.substring(1, color.length());
  35.                     
  36.                     int rgb = Integer.parseInt(color, 16);
  37.                     image.setRGB(c, r, rgb);
  38.                     c++;
  39.                 }
  40.                 r++;
  41.             }
  42.             ImageIO.write(image, "GIF"new File(args[0] + ".gif"));
  43.         } catch (JDOMException e) {
  44.             // TODO Auto-generated catch block
  45.             e.printStackTrace();
  46.         } catch (IOException e) {
  47.             // TODO Auto-generated catch block
  48.             e.printStackTrace();
  49.         }
  50.     }
  51. }

但是仍然有一個問題,就是源文檔并不是標準的XML,所以需要用替換的方法將源代碼中的屬性欄位都加上雙引號。

 

原创粉丝点击