Html转化为图片,并且和另一张图片合成新的图片

简介: Html转化为图片,并且和另一张图片合成新的图片

最近项目里有一个需求:把kedit里的html内容和事先设定好的背景颜色转为图片,嵌入到一张事先准备好的图片里先看下效果

                                               图1

                                                  图2    

                                                                     

                      图3     (合成后背景颜色设为蓝色)                        

           

  上面这个需求要分为两步来完成:

  1、把html转为图片

  2、在图片2上画图片1

  先说步骤1:

   网上有个开源的jar包html2image-0.9.jar   (http://download.csdn.net/detail/jishuisanqianli/9633608)

   能将html转为图片,底层使用java swing中提供的JEditorPane类

  打开html2image-0.9.jar包的HtmlImageGenerator类里可以看到几个关键的方法我想你应该就理解的差不多了:

     protected JEditorPane createJEditorPane() {
        JEditorPane editorPane = new JEditorPane();
        editorPane.setSize(getDefaultSize());
        editorPane.setEditable(false);
        SynchronousHTMLEditorKit kit = new SynchronousHTMLEditorKit();
        editorPane.setEditorKitForContentType("text/html", kit);
        editorPane.setContentType("text/html");
        editorPane.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals("page"))
                    HtmlImageGenerator.this.onDocumentLoad();
            }
        });
        return editorPane;
     }
      public void loadUrl(String url) {
        try {
            this.editorPane.setPage(url);
        } catch (IOException e) {
            throw new RuntimeException(String.format(
                    "Exception while loading %s", new Object[] { url }), e);
        }
    }
    public BufferedImage getBufferedImage() {
        //注意这个原来的jar包里没有,加了这句话避免生成的图片存在内边距
        editorPane.setMargin(new Insets(0, 0, 0, 0));
        Dimension prefSize = this.editorPane.getPreferredSize();
        BufferedImage img = new BufferedImage(prefSize.width,
                this.editorPane.getPreferredSize().height, 2);
        Graphics2D graphics = img.createGraphics();
        this.editorPane.setSize(prefSize);
        this.editorPane.paint(graphics);
        return img;
    }
    public void saveAsImage(File file) {
        BufferedImage img = getBufferedImage();
        try {
            String formatName = FormatNameUtil
                    .formatForFilename(file.getName());
            ImageIO.write(img, formatName, file);
        } catch (IOException e) {
            throw new RuntimeException(
                    String.format("Exception while saving '%s' image",
                            new Object[] { file }), e);
        }
    }

详细代码

https://gitee.com/lzhcode/maven-parent/blob/master/lzh-technology/src/main/java/com/lzhsite/technology/util/HtmlImageGenerator.java

说完了原理现在上步骤1的实现代码:

String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String html =  Content.htmlTemplate.replace("backgroundcolor",carDesignModel1.getCarcorlor());
            html=html.replace("baseurl",basePath);
            html=html.replace("content",carDesignModel1.getContent());
//把html转为图片,得到图1 imageKedit
HtmlImageGenerator imageGenerator = new HtmlImageGenerator();imageGenerator.loadHtml(html);
BufferedImage imageKedit =  imageGenerator.getBufferedImage();
//根据url读取图片,得到图2 imageOrigin   
 URL u = new 
    URL(Property.getProperty("FILESERVER_PATH")+"id="+carDesignModel1.getPicid());
BufferedImage imageOrigin = ImageIO.read(u);

    下面说步骤2:

     通过Graphics里的drawImage重新绘图并且生成新的图片  

//合成新的图片
Graphics g=imageOrigin.getGraphics();
//四个参数分别是:图1横坐标,图1纵坐标,图1上绘制的图2的长,图1上绘制的图2的宽
g.drawImage(imageKedit,imageOrigin.getWidth()-imageKedit.getWidth(),imageOrigin.getHeight()-imageKedit.getHeight(),imageKedit.getWidth(),imageKedit.getHeight(),null);
OutputStream outImage=new FileOutputStream("D:\\test2.png");
//合成的图片必须为png否则会导致颜色有所偏差
ImageIO.write(imageOrigin, "png", outImage);
outImage.close();

 

对图片操作的一些代码:

https://gitee.com/lzhcode/maven-parent/blob/master/lzh-technology/src/main/java/com/lzhsite/technology/util/HttpPostUploadUtil1.java

https://gitee.com/lzhcode/maven-parent/blob/master/lzh-technology/src/main/java/com/lzhsite/technology/util/HttpPostUploadUtil2.java

                                                                                                                                         


相关文章
|
3月前
|
移动开发 前端开发 JavaScript
基于 HTML5 和 Canvas 开发的在线图片编辑器
基于 HTML5 和 Canvas 开发的在线图片编辑器
79 0
|
13天前
HTML图片
【10月更文挑战第4天】HTML图片。
16 2
|
1月前
|
前端开发
Twaver-HTML5基础学习(37)network导出图片并下载
本文介绍了如何在Twaver-HTML5中将network导出为图片并提供下载,主要通过将network转换为canvas对象,然后转换为base64编码的图片进行展示和下载。
32 5
|
1月前
|
前端开发 Windows
【前端web入门第一天】02 HTML图片标签 超链接标签 音频标签 视频标签
本文档详细介绍了HTML中的图片、超链接、音频和视频标签的使用方法。首先讲解了`<img>`标签的基本用法及其属性,包括如何使用相对路径和绝对路径。接着介绍了`<a>`标签,用于创建超链接,并展示了如何设置目标页面打开方式。最后,文档还涵盖了如何在网页中嵌入音频和视频文件,包括简化写法及常用属性。
42 13
|
2月前
在线将多张图片拼接起来图工具HTML源码
在线将多张图片拼接成一张图片,多图合一并导出下载。无需本地安装软件。 下载时,使用日期时间作为文件名,规避图片文件名相同造成的覆盖问题;也能省去一部覆盖确认操作 多语言支持
32 0
在线将多张图片拼接起来图工具HTML源码
|
5月前
|
移动开发
uni-app使用v-html输出富文本图片溢出解决
uni-app使用v-html输出富文本图片溢出解决
582 1
|
2月前
|
Python
Python 下载 html 中的 图片
Python 下载 html 中的 图片
27 2
|
3月前
|
数据采集 缓存 自然语言处理
PHP将HTML标签转化为图片
通过这个方法,PHP后端能够实现将HTML内容转化为图片的功能。这种方式虽然牵涉到一些额外的安装和配置,但能够相对灵活且稳定地解冀转换需求,适用于需要在后端动态生成图片的场景。
141 1
|
3月前
|
JavaScript 前端开发 CDN
vue 生成分享海报、下载图片(含生成二维码qrcodejs2的使用,网页截屏html2canvas的使用)
vue 生成分享海报、下载图片(含生成二维码qrcodejs2的使用,网页截屏html2canvas的使用)
51 0
vue 生成分享海报、下载图片(含生成二维码qrcodejs2的使用,网页截屏html2canvas的使用)
|
4月前
|
移动开发 前端开发 JavaScript
将HTML5 Canvas的内容保存为图片
将HTML5 Canvas的内容保存为图片
35 5