Java或其他技术 将html转化成图片?
2014-02-07 09:19
537 查看
如何使用Java或其他技术将html转换为图片呢?知道的朋友交流一下~
网上找了一下,发现几种方法。
1、html2image【java】
转自:/article/3526580.html
html2image:http://code.google.com/p/java-html2image/
jar包下载地址:http://code.google.com/p/java-html2image/downloads/list
HtmlImageGenerator Methods
loadUrl(url) - Loads HTML from URL object or URL string. (从url载入html)
loadHtml(html) - Loads HTML source. (载入本地html)
saveAsImage(file) - Save loaded HTML as image. (以图片形式保存html)
saveAsHtmlWithMap(file, imageUrl) - Creates an HTML file containing client-side image-map <map> generated from HTML's links. (创建一个HTML文件包含客户端image-map)
getLinks() - List all links in the HTML document and their corresponding href, target, title, position and dimension. (列出所有在HTML文档的链接和相应href、目标、头衔、位置和尺寸)
getBufferedImage() - Get AWT buffered image of the HTML. (获得awt,html缓冲后的图片)
getLinksMapMarkup(mapName) - Get HTML snippet of the client-side image-map <map> generated from the links. (HTML代码段里获得的客户端image-map <地图>产生的链接)
get/setOrientation(orientation) - Get/Set document orientation (left-to-right or right-to-left). (get/set文本定位)
get/setSize(dimension) - Get/Set size of the generated image. (设置生成图片大小)
测试结果:不支持相对路径链接图片,排版错乱。
2、纯Java http://www.tuicool.com/articles/2yAryy
Java中江HTML片段转换为图片,不需要调用额外的jar包,支持css,但写在style之间的css和外置的css文件不受支持,只能写在标签上。
知道不支持外置css文件就没测试了。
示例代码:
3、html2canvas 【JS截图技术】
官网:http://html2canvas.hertzen.com/
测试结果:相对前面2种较好,支持图片、css外链等样式。缺点是有些还是不能正常排版。
api文档:http://html2canvas.hertzen.com/documentation.html
4、DJNativeSwing 模拟浏览器截图 【待研究 】
5、在windows环境下,可以考虑这个工具软件来抓页面截图:
IECapt.exe
测试结果:还可以,就是功能比较少,可以完成网页截图,缺点:不能控制图片大小
在linux环境下,可以考虑这个工具软件来抓页面截图:
HTML2Image
/article/4061530.html
6、JS生成缩略图
①、 <script language="JavaScript">
<!--
var flag=false;
function DrawImage(ImgD){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= 360/270){
if(image.width>360){
ImgD.width=360;
ImgD.height=(image.height*360)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>270){
ImgD.height=270;
ImgD.width=(image.width*270)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
}
}
//-->
</script>
<img src="1.png" onload="javascript:DrawImage(this);" />
②、 <script type="text/javascript" language="javascript">
var flag = false;
function DrawImage(ImgD) {
var image = new Image();
image.src = ImgD.src;
if (image.width > 0 && image.height > 0) {
flag = true;
if (image.width / image.height >= 164 / 112) {
if (image.width > 164) {
ImgD.width = 164;
ImgD.height = (image.height * 164) / image.width;
} else {
ImgD.width = image.width;
ImgD.height = image.height;
}
ImgD.alt = image.width + "×" + image.height;
}
else {
if (image.height > 35) {
ImgD.height = 35;
ImgD.width = (image.width * 35) / image.height;
} else {
ImgD.width = image.width;
ImgD.height = image.height;
}
ImgD.alt = image.width + "×" + image.height;
}
}
}
</script>
<input type="button" value="生成缩略图" onclick="DrawImage(this.nextSibling)" /><img src="1.png" border="0" >
7、待研究icepdf xhtmlrenderer http://download.csdn.net/detail/Altered/3094060#comment
网上找了一下,发现几种方法。
1、html2image【java】
转自:/article/3526580.html
html2image:http://code.google.com/p/java-html2image/
jar包下载地址:http://code.google.com/p/java-html2image/downloads/list
HtmlImageGenerator imageGenerator = new HtmlImageGenerator(); imageGenerator.loadHtml("<b>Hello World!</b> Please goto <a title=\"Goto Google\" href=\"http://www.google.com\">Google</a>."); imageGenerator.saveAsImage("hello-world.png"); imageGenerator.saveAsHtmlWithMap("hello-world.html", "hello-world.png");
HtmlImageGenerator Methods
loadUrl(url) - Loads HTML from URL object or URL string. (从url载入html)
loadHtml(html) - Loads HTML source. (载入本地html)
saveAsImage(file) - Save loaded HTML as image. (以图片形式保存html)
saveAsHtmlWithMap(file, imageUrl) - Creates an HTML file containing client-side image-map <map> generated from HTML's links. (创建一个HTML文件包含客户端image-map)
getLinks() - List all links in the HTML document and their corresponding href, target, title, position and dimension. (列出所有在HTML文档的链接和相应href、目标、头衔、位置和尺寸)
getBufferedImage() - Get AWT buffered image of the HTML. (获得awt,html缓冲后的图片)
getLinksMapMarkup(mapName) - Get HTML snippet of the client-side image-map <map> generated from the links. (HTML代码段里获得的客户端image-map <地图>产生的链接)
get/setOrientation(orientation) - Get/Set document orientation (left-to-right or right-to-left). (get/set文本定位)
get/setSize(dimension) - Get/Set size of the generated image. (设置生成图片大小)
测试结果:不支持相对路径链接图片,排版错乱。
2、纯Java http://www.tuicool.com/articles/2yAryy
Java中江HTML片段转换为图片,不需要调用额外的jar包,支持css,但写在style之间的css和外置的css文件不受支持,只能写在标签上。
知道不支持外置css文件就没测试了。
示例代码:
import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import javax.swing.JTextPane; import javax.swing.border.EmptyBorder; import javax.swing.plaf.basic.BasicEditorPaneUI; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGEncodeParam; import com.sun.image.codec.jpeg.JPEGImageEncoder; import com.xxx.util.FileUtil; /** * HTML转换图片的方式 * * */ public class HTML2Picture { public static int DEFAULT_IMAGE_WIDTH = 730; public static int DEFAULT_IMAGE_HEIGHT = 700; public static boolean paintPage(Graphics g, int hPage, int pageIndex, JTextPane panel) { Graphics2D g2 = (Graphics2D) g; Dimension d = ((BasicEditorPaneUI) panel.getUI()) .getPreferredSize(panel); double panelHeight = d.height; double pageHeight = hPage; int totalNumPages = (int) Math.ceil(panelHeight / pageHeight); g2.translate(0f, -(pageIndex - 1) * pageHeight); panel.paint(g2); boolean ret = true; if (pageIndex >= totalNumPages) { ret = false; return ret; } return ret; } /** * html转换为jpeg文件 * * @param bgColor * 图片的背景色 * @param html * html的文本信息 * @param width * 显示图片的Text容器的宽度 * @param height * 显示图片的Text容器的高度 * @param eb * 設置容器的边框 * @return * @throws Exception */ private static void html2jpeg(Color bgColor, String html, int width, int height, EmptyBorder eb) throws Exception { JTextPane tp = new JTextPane(); tp.setSize(width, height); if (eb == null) { eb = new EmptyBorder(0, 50, 0, 50); } if (bgColor != null) { tp.setBackground(bgColor); } if (width <= 0) { width = DEFAULT_IMAGE_WIDTH; } if (height <= 0) { height = DEFAULT_IMAGE_HEIGHT; } tp.setBorder(eb); tp.setContentType("text/html"); tp.setText(html); int pageIndex = 1; boolean bcontinue = true; while (bcontinue) { BufferedImage image = new java.awt.image.BufferedImage(width, height, java.awt.image.BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); g.setClip(0, 0, width, height); bcontinue = paintPage(g, height, pageIndex, tp); g.dispose(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(baos); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image); param.setQuality(1.0f, false); encoder.setJPEGEncodeParam(param); encoder.encode(image); byte[] bytes = baos.toByteArray(); baos.close(); FileUtil.writeBinFile("C:\123.jpg", bytes); pageIndex++; } } public static void main(String[] args) throws Exception { html2jpeg(Color.white, FileUtil.readAscFile("C:\table.html"), DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_WIDTH, new EmptyBorder(0, 0, 0, 0)); System.out.println("over!"); } }
3、html2canvas 【JS截图技术】
官网:http://html2canvas.hertzen.com/
测试结果:相对前面2种较好,支持图片、css外链等样式。缺点是有些还是不能正常排版。
api文档:http://html2canvas.hertzen.com/documentation.html
4、DJNativeSwing 模拟浏览器截图 【待研究 】
5、在windows环境下,可以考虑这个工具软件来抓页面截图:
IECapt.exe
测试结果:还可以,就是功能比较少,可以完成网页截图,缺点:不能控制图片大小
在linux环境下,可以考虑这个工具软件来抓页面截图:
HTML2Image
/article/4061530.html
6、JS生成缩略图
①、 <script language="JavaScript">
<!--
var flag=false;
function DrawImage(ImgD){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= 360/270){
if(image.width>360){
ImgD.width=360;
ImgD.height=(image.height*360)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>270){
ImgD.height=270;
ImgD.width=(image.width*270)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
}
}
//-->
</script>
<img src="1.png" onload="javascript:DrawImage(this);" />
②、 <script type="text/javascript" language="javascript">
var flag = false;
function DrawImage(ImgD) {
var image = new Image();
image.src = ImgD.src;
if (image.width > 0 && image.height > 0) {
flag = true;
if (image.width / image.height >= 164 / 112) {
if (image.width > 164) {
ImgD.width = 164;
ImgD.height = (image.height * 164) / image.width;
} else {
ImgD.width = image.width;
ImgD.height = image.height;
}
ImgD.alt = image.width + "×" + image.height;
}
else {
if (image.height > 35) {
ImgD.height = 35;
ImgD.width = (image.width * 35) / image.height;
} else {
ImgD.width = image.width;
ImgD.height = image.height;
}
ImgD.alt = image.width + "×" + image.height;
}
}
}
</script>
<input type="button" value="生成缩略图" onclick="DrawImage(this.nextSibling)" /><img src="1.png" border="0" >
7、待研究icepdf xhtmlrenderer http://download.csdn.net/detail/Altered/3094060#comment
相关文章推荐
- java把html转化为图片,并且和另一张图片合成新的图片
- JAVA将HTML转化图片最靠谱的方法
- java+springmvc+html2cavas转化图片后台处理
- Java-GUI(5)绘图技术、常用的绘图方法、绘制图片
- java技术加强之其他1.5特性
- java实现图片浏览:java核心技术学习
- java 生成静态html技术
- 黑马程序员________Java中IO技术其他常用流对象学习笔记
- java实现图片转化为字符图片--最终代码
- java指纹识别以及谷歌图片识别技术源码
- Java序列化和反序列化超强工具类(包含tif图片与其他格式互转)
- JavaWeb开发之普通图片验证码生成技术与算术表达式验证码生成技术
- Java中图片文件和字符串的相互转化
- html精灵技术(用来显示图片的某个区域)
- Servlet技术实现动态图片验证码(Java)
- 黑马程序员--java技术blog---第一篇:其他对象
- Python语言实现将图片转化为html页面
- java 上传图片 二进制保存到 mysql 请求图片二进制转化为图片 cxf rest jax-rs
- 如何在Linux上将HTML页面转化成png图片
- 解析html标签并转化成图片