用java代码根据html页面生成图片

为什么做这个?

目前 微信公众号开发很火,
  开发模式跟用户互动时可以是 文字、 图文 、图片 、语音、视频 等。
  而公众号界面展示基本都是html5页面。

所以把网页 转成图片发送给用户 这种体验非常爽的,比文字、图文 展示更全面、美观而实用 ,图片也可以下载下来保存手机里,图片内容 可以很 用户头像 、统计数据 、推广页面 、二维码等。

 

下面是java 实现

做成工具类 代码里可以直接使用。
下面代码可能出现异常

java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Composite

使用代码前需要导入 jar包。需要三个jar包:
swt.jar,DJNativeSwing-SWT.jar,DJNativeSwing.jar

DJNativeSwing jar包资源下载


Cannot load 32-bit SWT libraries on 64-bit
swt.jar 不兼容导致的。 这个jar可以在自己eclipse安装目录 可以找的
D:\install\eclipse\plugins  下 类似名字org.eclipse.swt.win32.win32.x86_64_3.104.2.v20160212-1350.jar

 如出现Cannot load 32-bit SWT libraries on 64-bit JVM,请检查 当前运行环境的jdk位数与swt.jar的位数是否一致

如出现(x + width) is outside raster可调整最大宽度maxWeight

// 当网页超出目标大小时 截取
final static public int maxWidth = 2000;
final static public int maxHeight = 2000;

生成的图片想加白边可在宽高上加50像素

imageSize.width = Math.max(originalSize.width,imageSize.width + 50);  
imageSize.height = Math.max(originalSize.height,imageSize.height + 50); 

 工具类如下,执行main方法即可

package com.xf.common.util;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import chrriis.dj.nativeswing.swtimpl.NativeComponent;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserAdapter;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserEvent;

/**
 * 模拟游览器 截屏 工具,执行printUrlScreen2jpg可将目标地址内容生成图片
 * @author xufei
 * @version 4.0, 2018年12月11日
 */
public class PrintScreen4DJNativeSwingUtils extends JPanel {
    private static final long serialVersionUID = 1L;
    // 行分隔符
    final static public String LS = System.getProperty("line.separator", "/n");
    // 文件分割符
    final static public String FS = System.getProperty("file.separator", "//");
    // 当网页超出目标大小时 截取
    final static public int maxWidth = 1000;
    final static public int maxHeight = 1400;

    /**
     * @param file
     *            预生成的图片全路径
     * @param url
     *            网页地址
     * @param width
     *            打开网页宽度 ,0 = 全屏
     * @param height
     *            打开网页高度 ,0 = 全屏
     * @return boolean
     * @author xufei
     * @version 4.0, 2018年12月11日
     */
    public PrintScreen4DJNativeSwingUtils(final String file, final String url,
            final String WithResult) {
        super(new BorderLayout());
        JPanel webBrowserPanel = new JPanel(new BorderLayout());
        final JWebBrowser webBrowser = new JWebBrowser(null);
        webBrowser.setBarsVisible(false);
        webBrowser.navigate(url);
        webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
        add(webBrowserPanel, BorderLayout.CENTER);
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4));
        webBrowser.addWebBrowserListener(new WebBrowserAdapter() {
            // 监听加载进度
            public void loadingProgressChanged(WebBrowserEvent e) {
                // 当加载完毕时
                if (e.getWebBrowser().getLoadingProgress() == 100) {
                    String result = (String) webBrowser
                            .executeJavascriptWithResult(WithResult);
                    int index = result == null ? -1 : result.indexOf(":");
                    NativeComponent nativeComponent = webBrowser
                            .getNativeComponent();
                    Dimension originalSize = nativeComponent.getSize();
                    Dimension imageSize = new Dimension(Integer.parseInt(result
                            .substring(0, index)), Integer.parseInt(result
                            .substring(index + 1)));
                    imageSize.width = Math.max(originalSize.width,
                            imageSize.width);
                    imageSize.height = Math.max(originalSize.height,
                            imageSize.height);
                    nativeComponent.setSize(imageSize);
                    BufferedImage image = new BufferedImage(imageSize.width,
                            imageSize.height, BufferedImage.TYPE_INT_RGB);
                    nativeComponent.paintComponent(image);
                    nativeComponent.setSize(originalSize);
                    // 当网页超出目标大小时
                    if (imageSize.width > maxWidth
                            || imageSize.height > maxHeight) {
                        // 截图部分图形
                        image = image.getSubimage(0, 0, maxWidth, maxHeight);
                        // 此部分为使用缩略图
                        /*
                         * int width = image.getWidth(), height = image
                         * .getHeight(); AffineTransform tx = new
                         * AffineTransform(); tx.scale((double) maxWidth /
                         * width, (double) maxHeight / height);
                         * AffineTransformOp op = new AffineTransformOp(tx,
                         * AffineTransformOp.TYPE_NEAREST_NEIGHBOR); //缩小 image
                         * = op.filter(image, null);
                         */
                    }
                    try {
                        // 输出图像
                        ImageIO.write(image, "jpg", new File(file));
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    // 退出操作
                    System.exit(0);
                }
            }
        });
        add(panel, BorderLayout.SOUTH);

    }

    /**
     *  以javascript脚本获得网页全屏后大小
     * @return
     */
    public static String getScreenWidthHeight() {

        StringBuffer jsDimension = new StringBuffer();
        jsDimension.append("var width = 0;").append(LS);
        jsDimension.append("var height = 0;").append(LS);
        jsDimension.append("if(document.documentElement) {").append(LS);
        jsDimension
                .append("  width = Math.max(width, document.documentElement.scrollWidth);")
                .append(LS);
        jsDimension
                .append("  height = Math.max(height, document.documentElement.scrollHeight);")
                .append(LS);
        jsDimension.append("}").append(LS);
        jsDimension.append("if(self.innerWidth) {").append(LS);
        jsDimension.append("  width = Math.max(width, self.innerWidth);")
                .append(LS);
        jsDimension.append("  height = Math.max(height, self.innerHeight);")
                .append(LS);
        jsDimension.append("}").append(LS);
        jsDimension.append("if(document.body.scrollWidth) {").append(LS);
        jsDimension.append(
                "  width = Math.max(width, document.body.scrollWidth);")
                .append(LS);
        jsDimension.append(
                "  height = Math.max(height, document.body.scrollHeight);")
                .append(LS);
        jsDimension.append("}").append(LS);
        jsDimension.append("return width + ':' + height;");

        return jsDimension.toString();
    }

    /**
     * 
     * @param file 图片保存路径
     * @param url 要截图的URL地址
     * @param width 裁剪图片的宽   0代表按照图片的原始大小保存
     * @param height 裁剪图片的高 0代表按照图片的原始大小保存
     * @return
     */
    public static boolean printUrlScreen2jpg(final String file,
            final String url, final int width, final int height) {

        NativeInterface.open();
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                String withResult = "var width = " + width + ";var height = "
                        + height + ";return width +':' + height;";
                if (width == 0 || height == 0)
                    withResult = getScreenWidthHeight();

                // SWT组件转Swing组件,不初始化父窗体将无法启动webBrowser
                JFrame frame = new JFrame("网页截图");
                // 加载指定页面,最大保存为640x480的截图
                frame.getContentPane().add(
                        new PrintScreen4DJNativeSwingUtils(file, url,
                                withResult), BorderLayout.CENTER);
                frame.setSize(640, 480);
                // 仅初始化,但不显示
                frame.invalidate();

                frame.pack();
                frame.setVisible(false);
            }
        });
        NativeInterface.runEventPump();

        return true;
    }
    
    public static void main(String[] args) {  
    	PrintScreen4DJNativeSwingUtils.printUrlScreen2jpg("d:/test/hello-world.png", "https://blog.csdn.net/xufei512/article/details/84955470", 0, 0);
    }  
}

执行main方法生成的图片效果如下 

 

  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值