java+phantomjs网页截图裁剪

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 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Selenium是一个用于自动化浏览器操作的工具,而PhantomJS是一个无界面的浏览器。它们可以结合使用来进行爬虫操作。使用Selenium和PhantomJS,你可以模拟用户在浏览器中的操作,例如点击、填写表单等。这样就可以实现爬取网页数据的功能。 安装Selenium和PhantomJS的步骤如下: 1. 首先确保你已经安装了Python 3.5以及pip。 2. 使用pip安装Selenium,运行以下命令: ``` pip install selenium ``` 3. 下载PhantomJS并解压到Python的Scripts目录中。具体下载链接请参考中提供的系统环境下载说明。 安装完成后,你就可以开始使用Selenium和PhantomJS来实现爬虫功能了。你可以参考中提供的示例代码来学习如何使用Selenium和PhantomJS进行网页爬取。 此外,关于使用Selenium和PhantomJS进行数据爬取的方法,你可以按照以下步骤进行: 1. 使用Selenium和PhantomJS打开目标网页,模拟用户在浏览器中的操作来获取数据。 2. 如果网页使用了ajax接口来加载数据,你可以通过分析网页源代码或使用开发者工具来找到数据来源的接口,并发送请求获取数据。 3. 如果你只需要获取列表页的数据,可以在第一次爬取时只爬取列表页,将详情页的URL放到每条数据中心。这样可以避免增加请求数量。 4. 如果需要获取详情页的数据,可以在第二次爬取时从数据库中提取详情页URL,并使用Selenium和PhantomJS来爬取详情页的数据。 希望这些信息能对你有所帮助。如果你有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值