Html2Image转换html为图片
(2018-12-16 15:34:03)
标签:
html2imagehtmlimagegenerator |
分类: java |
代码实例:
public static void
main(String[] args) {
HtmlImageGenerator imageGenerator = new
HtmlImageGenerator();
String html = "
";
imageGenerator.loadHtml(html);//加载HTML字符串
try {
imageGenerator.getBufferedImage();// 生成图片字符流
Thread.sleep(8000);
} catch (InterruptedException e) {
e.printStackTrace();
}
imageGenerator.saveAsImage("d:/car.png");//保存图片
imageGenerator.saveAsHtmlWithMap("hello-world.html",
"d:/car.png");//把图片转成网页,只是简单的img 引用
System.out.println("finish");
}
int h =
image.getHeight();
int w =
image.getWidth();
int[] pixels = new int[w
* h]; // 定义数组,用来存储图片的像素
int gray;
PixelGrabber pg = new
PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
try {
pg.grabPixels(); // 读取像素值
} catch
(InterruptedException e) {
throw new
RuntimeException("转换成24位图的BMP时,处理像素值异常");
}
for (int j = 0; j <
h; j++){ // 扫描列
for (int i = 0; i < w; i++) { // 扫描行
//
由红,绿,蓝值得到灰度值
gray =
(int) (((pixels[w * j + i] >> 16) & 0xff) * 0.8);
gray +=
(int) (((pixels[w * j + i] >> 8) & 0xff) * 0.1);
gray +=
(int) (((pixels[w * j + i]) & 0xff) * 0.1);
pixels[w *
j + i] = (255 << 24) | (gray << 16) | (gray << 8)
| gray;
}
}
MemoryImageSource s= new
MemoryImageSource(w,h,pixels,0,w);
Image img
=Toolkit.getDefaultToolkit().createImage(s);
BufferedImage buf = new
BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);//如果要转换成别的位图,改这个常量即可
buf.createGraphics().drawImage(img, 0, 0, null);
return buf;
package htmlToImg;
import gui.ava.html.image.generator.HtmlImageGenerator;
public class HtmlToImgTest {
//
String html = "
";
//
Thread.sleep(5000);
//不需要转换位图的,下面三行可以不要
// BufferedImage sourceImg = ImageIO.read(new
File(saveImageLocation));
// sourceImg =
transform_Gray24BitMap(sourceImg);
// ImageIO.write(sourceImg, "BMP", new
File(saveImageLocation));
}
附位图转换方法:
public static BufferedImage
transform_Gray24BitMap(BufferedImage image){
}
遇到的问题:
一、当你的html页面引入外部的CSS文件以及JS文件,生成的图片是无法带有这些动态效果的。也就是说,它不支持复杂的动态特性,只能支持写在html代码里的css效果。
二、当html代码里带有图片时,生成的程序必须有一定的等待时间,否则生成的图片打不开
Thread.sleep(5000);
三、图片格式最好png,反正我试过bmp和jpg的不好使
四、图片路径必须加上file:/// 或者直接写http的url,不支持相对路径写法
五、转换成功后,可能需要再做位图转换
六、我的table边框本地调试样式都是OK的,上了服务器表格的边框不见了,怎么都没有调出来,有朋友研究出来可以共享一下。
API
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
-
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
-
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. (设置生成图片大小)
前一篇:java生成pdf文件