【发布时间】:2012-05-06 00:40:05
【问题描述】:
对于我的应用程序,我需要动态创建网站缩略图。到目前为止,我从 SO 获得了这段代码:
public class CreateWebsiteThumbnail {
private static final int WIDTH = 128;
private static final int HEIGHT = 128;
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
public void capture(Component component) {
component.setSize(image.getWidth(), image.getHeight());
Graphics2D g = image.createGraphics();
try {
component.paint(g);
} finally {
g.dispose();
}
}
private BufferedImage getScaledImage(int width, int height) {
BufferedImage buffer = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffer.createGraphics();
try {
g.drawImage(image, 0, 0, width, height, null);
} finally {
g.dispose();
}
return buffer;
}
public void save(File png, int width, int height) throws IOException {
ImageIO.write(getScaledImage(width, height), "png", png);
}
public static void main(String[] args) throws IOException {
Shell shell = new Shell();
Browser browser = new Browser(shell, SWT.EMBEDDED);
browser.setUrl("http://www.google.com");
CreateWebsiteThumbnail cap = new CreateWebsiteThumbnail();
cap.capture(What her?);
cap.save(new File("foo.png"), 64, 64);
}
}
但正如您在此处看到的,我不知道应该将浏览器的哪个部分传递给我的捕获方法。有什么提示吗?
【问题讨论】:
-
除非您计算出缩略图的宽度和高度与网站图像的宽度和高度成正比,否则您会得到一个扭曲的缩略图。
-
@GilbertLeBlanc 现在如果我能得到任何缩略图我会很高兴
-
我本来打算等着看有没有人有答案,但据我所知,Browser 类没有获取网页图像的方法。如果您使用的是 Swing,我建议您使用 Robot 类来获取图像。