使用Java将网页转换为jpeg图像 [英] converting webpage into jpeg image using java

查看:129
本文介绍了使用Java将网页转换为jpeg图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我提供网页的URL作为输入,则我正在用Java构建一个Web应用程序,在该应用程序中我需要该网页的整个屏幕截图.

I am building a web application, in Java, where i want the whole screenshot of the webpage, if i give the URL of the webpage as input.

我的基本想法是捕获渲染组件的显示缓冲区.我不知道该怎么做. 请帮助.

The basic idea i have is to capture the display buffer of the rendering component..I have no idea of how to do it.. plz help..

推荐答案

对于可以扩展以支持并发呈现的纯Java解决方案,您可以使用Java HTML4/CSS2浏览器,例如

For a pure-java solution that can scale to support concurrent rendering, you could use a java HTML4/CSS2 browser, such as Cobra, that provides a Swing component for the GUI. When you instantiate this component, you can call it's paint(Graphics g) method to draw itself into an off-screen image

E.g.
Component c = ...; // the browser component
BufferedImage bi = new BufferedImage(c.getWidth(), c.getHeight(), TYPE_INT_RGB)
Graphics2d g = bi.createGraphics();    
c.paint(g);

然后您可以使用Java图像API将其另存为JPG.

You can then use the java image API to save this as a JPG.

JPEGImageEncoder encoder = JPEGCodec.createEncoder(new FileOutputStream("screen.jpg"));
enncoder.encode(bi);  // encode the buffered image

与已建立的本机浏览器相比,基于Java的浏览器通常显得苍白.但是,由于您的目标是静态图像而不是交互式浏览器,因此在这方面基于Java的浏览器可能绰绰有余.

Java-based browsers typically pale in comparison with the established native browsers. However, as your goal is static images, and not an interactive browser, a java-based browser may be more than adequate in this regard.

这篇关于使用Java将网页转换为jpeg图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆