package com.enation.javashop.core.service.impl;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.FileWriter;
import javax.imageio.ImageIO;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
public class HtmlToImage extends JFrame {
public HtmlToImage(String url, File file) throws Exception {
JEditorPane editorPane = new JEditorPane();
editorPane.setEditable(false);
String vNewReportFileName = "d://temp.html";
File f = new File(vNewReportFileName);
f.createNewFile();
FileWriter fw = new FileWriter(f);
fw.write("<html>");
fw.write("<head><body>");
fw.write("<font color=red>你好</font>");
fw.write("</body></html>");
fw.flush();
fw.close();
f = null;
editorPane.setPage("file:"+vNewReportFileName);
//editorPane.setText("<b>你好</b>");//setPage(url);
editorPane.setSize(100, 100);
//JScrollPane jsp = new JScrollPane(editorPane);
//getContentPane().add(jsp);
this.setLocation(0, 0);
this.setVisible(true); // 如果这里不设置可见,则里面的图片等无法截取
// 如果不延时,则图片等可能没有时间下载显示
// 具体的秒数需要根据网速等调整
//Thread.sleep(5 * 1000);
//setSize(10000, 10000);
//pack();
// BufferedImage image = new BufferedImage(editorPane.getWidth(),
// editorPane.getHeight(), BufferedImage.TYPE_INT_RGB);
BufferedImage image = new BufferedImage(editorPane.getWidth(), editorPane.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = image.createGraphics();
editorPane.paint(graphics2D);
BufferedImage image1 = resize(image, 600, 400);
ImageIO.write(image1, "jpg", file);
dispose();
}
public static void main(String[] args) throws Exception {
new HtmlToImage("http://www.baidu.com/acomgo?uid=10001", new File("d:/file.jpg"));
}
public static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
// targetW,targetH分别表示目标长和宽
int type = source.getType();
BufferedImage target = null;
double sx = (double) targetW / source.getWidth();
double sy = (double) targetH / source.getHeight(); // 这里想实现在targetW,targetH范围内实现等比缩放。如果不需要等比缩放 // 则将下面的if else语句注释即可
if (sx > sy) {
sx = sy;
targetW = (int) (sx * source.getWidth()); // } else { // sy = sx; // targetH = (int) (sy * source.getHeight());
}
if (type == BufferedImage.TYPE_CUSTOM) { // handmade
ColorModel cm = source.getColorModel();
WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
target = new BufferedImage(cm, raster, alphaPremultiplied, null);
}
else
target = new BufferedImage(targetW, targetH, type);
Graphics2D g = target.createGraphics(); // smoother than exlax:
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
return target;
}
}
package com.enation.javashop.core.utils;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.FileWriter;
import javax.imageio.ImageIO;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
public class HtmlToJpgUtilTest extends JFrame {
public HtmlToJpgUtilTest(String url, File file) throws Exception {
JEditorPane editorPane = new JEditorPane();
editorPane.setEditable(false);
//editorPane.setContentType("text/html");
String vNewReportFileName = "d://temp.html";
/* File f = new File(vNewReportFileName);
f.createNewFile();
FileWriter fw = new FileWriter(f);
fw.write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'> ");
fw.write("<html xmlns='http://www.w3.org/1999/xhtml'> ");
fw.write("<head> ");
fw.write("<style> ");
fw.write("body{font-size:12px; background:#ffffff;} ");
fw.write("ol li{line-height:180%;list-style:decimal;margin: 0 0 0 20px;} ");
fw.write("td{vertical-align:middle;} ");
fw.write("</style> ");
fw.write("</head> ");
fw.write("<body> ");
fw.write("<table width='600' border='0' align='center' cellpadding='20' cellspacing='0' style='border:1px solid #333333; padding:20px;'> ");
fw.write(" <tr> ");
fw.write(" <td><h3 align='center' style='height:55px; line-height:35px;font-size:25px;font-weight:bolder;'>2012年统计从业资格考试准考证 </h3> ");
fw.write("<table width='100%'> <tr>");
fw.write(" <td width='50%' style='float:left;width:300px;'> ");
fw.write(" <div style='line-height:24px; width:270px; padding-left:40px; color: #0000ff; font-size: 14px;'><strong>姓 名:</strong>王三<br /> ");
fw.write(" <strong>身份证号:</strong>455555555555555555<br /> ");
fw.write(" <strong>准考证号:</strong>1211013010108<br /> ");
fw.write(" <strong>考试地点:</strong>顺义二中 <br /> ");
fw.write(" <strong>考试地址:</strong>顺义二中 <br /> ");
fw.write(" </div> ");
fw.write(" </td> ");
fw.write(" <td width='50%'> ");
fw.write(" <img src='http://tongji.7east.com:8080/statics/attachment/goods/201202071558296883_thumbnail.jpg' > ");
fw.write(" </td> ");
fw.write(" ");
fw.write("</tr></table><br>");
fw.write(" <div align='center' style='clear:both;'> ");
fw.write(" <h3 style='margin-top:10px;font-size:20px;'>考试安排 </h3> ");
fw.write(" <table cellpadding='2' width='95%' cellspacing='1' bgcolor='#000000'> ");
fw.write(" ");
fw.write(" <tr> ");
fw.write(" <td width='238' height='30' align='center' bgcolor='#FFFFFF' style='padding-top:5px;padding-top:9px\0;_padding-top:9px;color:#000000;'><strong>考试时间 </strong></td> ");
fw.write(" <td width='192' align='center' bgcolor='#FFFFFF' style='padding-top:5px;padding-top:9px\0;_padding-top:9px;color:#000000;'><strong>考试科目 </strong></td> ");
fw.write(" <td width='70' align='center' bgcolor='#FFFFFF' style='padding-top:5px;padding-top:9px\0;_padding-top:9px;color:#000000;'><strong>考场号 </strong></td> ");
fw.write(" <td width='70' align='center' bgcolor='#FFFFFF' style='padding-top:5px;padding-top:9px\0;_padding-top:9px;color:#000000;'><strong>座位号 </strong></td> ");
fw.write(" </tr> ");
fw.write(" <tr> ");
fw.write(" <td width='238' align='center' height='25' bgcolor='#FFFFFF' style='color:#000000;padding-top:3px;padding-top:5px\0;_padding-top:3px;color:#000000;''>2012年02月08日09点30分-11点30分</td> ");
fw.write(" <td width='192' align='center' bgcolor='#FFFFFF' style='color:#000000;padding-top:3px;padding-top:5px\0;_padding-top:3px;color:#000000;'>统计基础知识与统计实务</td> ");
fw.write(" <td width='70' align='center' bgcolor='#FFFFFF' style='color:#000000;padding-top:3px;padding-top:5px\0;_padding-top:3px;color:#000000;'>第一考场</td> ");
fw.write(" <td width='70' align='center' bgcolor='#FFFFFF' style='color:#000000;padding-top:3px;padding-top:5px\0;_padding-top:3px;color:#000000;'>08</td> ");
fw.write(" </tr> ");
fw.write(" <tr> ");
fw.write(" <td width='238' align='center' height='25' bgcolor='#FFFFFF' style='color:#000000;padding-top:3px;padding-top:5px\0;_padding-top:3px;color:#000000;''>2012年02月08日14点00分-16点00分</td> ");
fw.write(" <td width='192' align='center' bgcolor='#FFFFFF' style='color:#000000;padding-top:3px;padding-top:5px\0;_padding-top:3px;color:#000000;'>统计法基础知识</td> ");
fw.write(" <td width='70' align='center' bgcolor='#FFFFFF' style='color:#000000;padding-top:3px;padding-top:5px\0;_padding-top:3px;color:#000000;'>第一考场</td> ");
fw.write(" <td width='70' align='center' bgcolor='#FFFFFF' style='color:#000000;padding-top:3px;padding-top:5px\0;_padding-top:3px;color:#000000;'>08</td> ");
fw.write(" </tr> ");
fw.write(" </table> ");
fw.write(" </div> ");
fw.write(" <p style='color:#ff0000;width:90%;margin:0 auto;margin-top:8px;' > 注:准考证上没有照片信息或照片信息不清晰的考生,务必在参加考试时携带本人近期二寸正面免冠照片一张交给本考场的监考员。</p> ");
fw.write(" ");
fw.write(" <p > ");
fw.write(" <h3 align='center' style='margin-top:20px;font-size:20px;'>考生注意事项 </h3> ");
fw.write(" <ol style='width:90%;margin:0 auto;padding-left:3px;'> ");
fw.write(" <li>“准考证”由考生使用A4复印纸在规定时间内上网自行打印。“准考证”涂改无效,背面必须为空白。 </li> ");
fw.write(" <li>考生凭“准考证”、有效身份证件(与报名时一致),按规定时间进入考场,对号入座。入座后将上述证件放在桌面左上角,以便检查。</li> ");
fw.write(" <li>应试时,考生须携带黑色或蓝色字迹钢笔或签字笔(同一科目答卷的字迹必须是一种颜色)、铅笔(2B)、橡皮。</li> ");
fw.write(" <li>严禁将各种电子设备、通讯、计算、储存或其他设备带至座位。</li> ");
fw.write(" <li>考试时间以北京时间为准。开考15分钟后不得入场。交卷出场时间不得早于各科目考试结束前30分钟。交卷出场后不得再进场续考,也不得在考场附近逗留或交谈。 </li> ");
fw.write(" <li>严禁将答题纸(卡)、试卷、草稿纸带离考场。</li> ");
fw.write(" <li>考生要严格遵守考试规则,若有 行为,将取消考试资格。 </li> ");
fw.write(" <li>请考生按照准考证上的指定考试地址,提前确认考点的具体位置和乘车路线。</li></ol></p> ");
fw.write(" <h3 align='center' style='margin-top:10px;font-size:20px;'>诚信考试 从我做起 </h3> ");
fw.write(" <p style='width:90%;margin:0 auto;margin-bottom:10px;color:#000000;'> 该考生在报名确认及下载打印准考证时均已承诺“本人已周知考场规则和报考者违纪违规处理有关规定;考试当天,保证遵守考场规则;若有违反,愿按相关规定接受处理。” </p></td> ");
fw.write(" </tr> ");
fw.write("</table> ");
fw.write("</body> ");
fw.write("</html> ");
fw.flush();
fw.close();
f = null;
*/
//editorPane.set
editorPane.setPage("file:"+vNewReportFileName);
//editorPane.setText("<b>你好</b>");//setPage(url);
//editorPane.setSize(1600, 1900);
JScrollPane jsp = new JScrollPane(editorPane);
getContentPane().add(jsp);
this.setLocation(0, 0);
this.setVisible(true); // 如果这里不设置可见,则里面的图片等无法截取
// 如果不延时,则图片等可能没有时间下载显示
// 具体的秒数需要根据网速等调整
// Thread.sleep(5 * 1000);
// setSize(700, 1800);
pack();
//BufferedImage image = new BufferedImage(editorPane.getWidth(),
//editorPane.getHeight(), BufferedImage.TYPE_INT_RGB);
BufferedImage image = new BufferedImage(editorPane.getWidth(), editorPane.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = image.createGraphics();
editorPane.paint(graphics2D);
// BufferedImage image1 = resize(image, 700, 1800);
ImageIO.write(image, "jpg", file);
dispose();
}
public static void main(String[] args) throws Exception {
new HtmlToJpgUtilTest("http://www.baidu.com/acomgo?uid=10001", new File("d:/file.jpg"));
}
public static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
// targetW,targetH分别表示目标长和宽
int type = source.getType();
BufferedImage target = null;
double sx = (double) targetW / source.getWidth();
double sy = (double) targetH / source.getHeight(); // 这里想实现在targetW,targetH范围内实现等比缩放。如果不需要等比缩放 // 则将下面的if else语句注释即可
/*if (sx > sy) {
sx = sy;
targetW = (int) (sx * source.getWidth()); // } else { // sy = sx; // targetH = (int) (sy * source.getHeight());
} */
if (type == BufferedImage.TYPE_CUSTOM) { // handmade
ColorModel cm = source.getColorModel();
WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
target = new BufferedImage(cm, raster, alphaPremultiplied, null);
}
else
target = new BufferedImage(targetW, targetH, type);
Graphics2D g = target.createGraphics(); // smoother than exlax:
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
return target;
}
}