在 servlet 中截图并 return?
Take a screenshot and return in servlet?
我正在尝试截取网页的屏幕截图,然后 return 从这样的 servlet 截取它:
<img src="http://localhost:8080/image">
这在大多数情况下都有效。我正在使用 phantomjs,从桌面打开时生成的 png 看起来很好。当使用 img 标签显示时,它显示为粉红色调?
我正在使用以下代码
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
String phantomjsHome = "C:\phantomjs-1.9.8-windows\";
String phantomjsRasterizeScript = phantomjsHome + "examples\rasterize.js";
String url = "http://localhost/";
String file = "C:\index.png";
ProcessBuilder pb = new ProcessBuilder(phantomjsHome + "phantomjs", phantomjsRasterizeScript, url, file);
ProcessBuilder.Redirect error = pb.redirectError();
ProcessBuilder.Redirect out = pb.redirectOutput();
ProcessBuilder.Redirect in = pb.redirectInput();
Process process = pb.start();
try {
process.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("image/jpeg");
String pathToWeb = getServletContext().getRealPath(File.separator);
File f = new File("C:\index.png");
BufferedImage bi = ImageIO.read(f);
OutputStream out1 = response.getOutputStream();
ImageIO.write(bi, "jpg", out1);
out1.close();
}
我是否遗漏了导致粉红色调的原因?另外,有谁知道如何让 phantomjs 只截取 div 的屏幕截图,或者有一个非 phantomjs 的解决方案?
谢谢!
答案一直就在我面前,我正在渲染 PNG 但试图 return PNG。我渲染为 JPEG 并 return 编辑它,一切都解决了。谢谢!
我正在尝试截取网页的屏幕截图,然后 return 从这样的 servlet 截取它:
<img src="http://localhost:8080/image">
这在大多数情况下都有效。我正在使用 phantomjs,从桌面打开时生成的 png 看起来很好。当使用 img 标签显示时,它显示为粉红色调?
我正在使用以下代码
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
String phantomjsHome = "C:\phantomjs-1.9.8-windows\";
String phantomjsRasterizeScript = phantomjsHome + "examples\rasterize.js";
String url = "http://localhost/";
String file = "C:\index.png";
ProcessBuilder pb = new ProcessBuilder(phantomjsHome + "phantomjs", phantomjsRasterizeScript, url, file);
ProcessBuilder.Redirect error = pb.redirectError();
ProcessBuilder.Redirect out = pb.redirectOutput();
ProcessBuilder.Redirect in = pb.redirectInput();
Process process = pb.start();
try {
process.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("image/jpeg");
String pathToWeb = getServletContext().getRealPath(File.separator);
File f = new File("C:\index.png");
BufferedImage bi = ImageIO.read(f);
OutputStream out1 = response.getOutputStream();
ImageIO.write(bi, "jpg", out1);
out1.close();
}
我是否遗漏了导致粉红色调的原因?另外,有谁知道如何让 phantomjs 只截取 div 的屏幕截图,或者有一个非 phantomjs 的解决方案?
谢谢!
答案一直就在我面前,我正在渲染 PNG 但试图 return PNG。我渲染为 JPEG 并 return 编辑它,一切都解决了。谢谢!