0
点赞
收藏
分享

微信扫一扫

java+phantomjs网页截图裁剪

木匠0819 2022-04-30 阅读 30
package com.realize.project.common;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;

@Component
public class FileResourceUtil {
    @Autowired
    private ResourceLoader resourceLoader;

    /**
     * 根据文件名字获取路径
     * @param fileNameAndPath
     * @return
     */
    public String getFilePath(String fileNameAndPath) throws IOException {
        File file = resourceLoader.getResource("file:"+ fileNameAndPath).getFile();
        if(!file.exists()) {
            file = resourceLoader.getResource("classpath:"+ fileNameAndPath).getFile();
        }
        return file.getAbsolutePath();
    }
}




package com.realize.project.system.service;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public interface ScreenshotService {

    String screenshot(HttpServletRequest re, String url, String size) throws IOException;
}



package com.realize.project.system.service.impl;

import com.realize.project.common.FileResourceUtil;
import com.realize.project.system.service.ScreenshotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

@Service
public class ScreenshotServiceImpl implements ScreenshotService {
    // 设置截屏后图片存储路径
    private String pdfPath = "D:/testPhoto/";

    @Autowired
    private FileResourceUtil fileResourceUtil;


    @Override
    public String screenshot(HttpServletRequest re, String url, String size) throws IOException {
        String img = "";
        // 此处可根据操作系统获取对应phantomjs文件路径(phantomjs.exe对应Windows,phantomjs对应Linux)
       /* String plugin = '';
        String os = System.getProperty("os.name").toLowerCase();
        if (os.contains("windows")) {
            plugin = resourceUtil.getFilePath("plugin/phantomjs.exe");
        }else{
         plugin = resourceUtil.getFilePath("plugin/phantomjs");
        }*/
        // 测试环境为window环境,故获取phantomjs.exe所在目录
        String plugin = fileResourceUtil.getFilePath("plugin/phantomjs.exe");
        String js = fileResourceUtil.getFilePath("plugin/rasterize.js");

        File file = new File(this.pdfPath);
        if (!file.exists()) {
            file.mkdirs();
        }

        // 截屏后文件名可自定义
        img = this.pdfPath  +  "1.png";

        File pluginFile = new File(plugin);
        if (!pluginFile.canExecute()) {
            pluginFile.setExecutable(true);
        }

        File jsFile = new File(js);
        if (!execute(plugin, jsFile.getAbsolutePath(), url, img,size)) {
            return null;
        }


        //打开刚刚生成的图片
        BufferedImage bufferedImage = ImageIO.read(new File(img));
        int width = bufferedImage.getWidth();
        int height = bufferedImage.getHeight();
        System.out.println("图片的宽度为"+width+",高度为"+height);
        //截图,这里可以根据需要各种裁剪
        BufferedImage subimage = bufferedImage.getSubimage(200, 100, 400, 400);
        File file1 = new File(this.pdfPath+"meal3.png");
        //生成一张新的图片
        ImageIO.write(subimage,"PNG",file1);

        return img;
    }

    // 截屏图片流处理
    public boolean execute(String... args) {

        Process process = null;

        StringBuilder msg = new StringBuilder();

        try {
            process = Runtime.getRuntime().exec(args);
            BufferedReader input = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = input.readLine()) != null) {
                System.out.println(line);
            }
            input.close();
        } catch (IOException e) {
            System.out.println("error:"+e.getCause());
            msg.append("error");
        }
        return !msg.toString().endsWith("error");
    }
}



@Api("用户信息管理")
@RestController
@RequestMapping("/test/user")
public class TestController extends BaseController
{

    @Autowired
    public ScreenshotServiceImpl screenshotService;

    @GetMapping("/screenshot")
    public void screenshot() {
        try {
            // 截屏图片大小可根据实际需求进行设置
            screenshotService.screenshot(null, "https://www.baidu.com/", "800px*800px");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

phantomjs插件官方下载地址:https://phantomjs.org/download.html 

举报

相关推荐

0 条评论