對於我的應用程序,我需要動態創建網站縮略圖。到目前爲止,我有這個代碼從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現在我會很高興,如果我能得到任何縮略圖 – RoflcoptrException
我要等着看,如果有人有答案,但據我所知,瀏覽器類沒有辦法獲得網絡圖像頁。如果您使用的是Swing,我會建議使用Robot類來獲取圖像。 –