一 . 背景介绍
- 最近公司业务有个小小的修改,需要我将一个简单的HTML静态页面,转换成BMP图片方便打印机打印
- 运行环境:Windows 10
- 上网查了资料,也踩过几个坑,在这里与大家分享
二. 大致过程
- 引入Html2Image的Maven依赖
<dependency>
<groupId>com.github.xuwei-k</groupId>
<artifactId>html2image</artifactId>
<version>0.1.0</version>
</dependency>
写个工具类
IO流读取HTML文件为字符串
/** * * @Description 读取HTML文件,获取字符内容 * @param filePath * @param charset * @return */ public static String getHtmlContent(String filePath, String charset){ String line = null; StringBuilder sb = new StringBuilder(); BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath)),charset)); while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("读取HTML文件,获取字符内容异常"); } finally { try { reader.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("关闭流异常"); } } return sb.toString(); }
HTML转Image
/** * * @Description HTML转Image * @param htmText HTML文本字符串 * @return 希望生成的Image Location */ public static String html2Img(String htmText, String saveImageLocation){ HtmlImageGenerator imageGenerator = new HtmlImageGenerator(); try { imageGenerator.loadHtml(htmText); Thread.sleep(100); imageGenerator.getBufferedImage(); Thread.sleep(200); imageGenerator.saveAsImage(saveImageLocation); //imageGenerator.saveAsHtmlWithMap("hello-world.html", saveImageLocation); //不需要转换位图的,下面三行可以不要 BufferedImage sourceImg = ImageIO.read(new File(saveImageLocation)); sourceImg = transform_Gray24BitMap(sourceImg); ImageIO.write(sourceImg, "BMP", new File(saveImageLocation)); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("将HTML文件转换成图片异常"); } return saveImageLocation; }
可能有些特殊要求,需要转换位图,这里就要用到一个位图转换工具类
/** * * @Description 转换成24位图的BMP * @param image * @return */ public static BufferedImage transform_Gray24BitMap(BufferedImage image){ 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; }
测试方法
//测试方法 public static void main(String[] args) { String charset = "GBK"; String saveImageLocation = "E:\\save.png"; String htmlFilePath = "C:\\Users\\xusanduo\\Desktop\\test.html"; String htmText = getHtmlContent(htmlFilePath, charset); html2Img(htmText, saveImageLocation); }
下面附上test.html的内容
<html> <body> <div class="logImg" style="margin:0px;left:0px;top:2px;text-align: center;width: 350px;"> <!-- 注:图片src不支持相对路径,必须加上file:/// 或者直接写http的url。最好写jpg格式,亲测过bmp和png格式不行--> <img align="middle" src="file:///E:/1name.jpg"> </div> <div class="contentText" style="margin:20px 0px 0px 0px"> <table border="0" style="width: 350px;"> <tbody> <tr> <td align="middle" style="font-size:20px">9-6-图片测试</td> </tr> </tbody> </table> </div> </body> </html>
三. 碰到的坑
- 图片格式最好jpg,反正我试过bmp和png的不行
- 图片路径必须加上file:/// 或者直接写http的url,不支持相对路径写法
- 转换成功后,可能需要再做位图转换
更多推荐
Html2Image (JAVA)
发布评论