將圖片轉換為HTML TABLE

 

 

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

/**
 * 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,所以需要用替換的方法將源代碼中的屬性欄位都加上雙引號。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值