对于我的应用程序,我需要动态创建网站的缩略图.到目前为止,我有这样的代码从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,width,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); } }
解决方法
我没看到,你提供的代码可以如何工作. Shell没有打开,没有大小,浏览器没有时间实际加载任何东西,没有事件循环似乎运行,以启用任何绘图,…
import java.io.IOException; import org.eclipse.swt.SWT; import org.eclipse.swt.browser.Browser; import org.eclipse.swt.browser.ProgressEvent; import org.eclipse.swt.browser.ProgressListener; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.graphics.ImageLoader; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class CreateWebsiteThumbnail { private static final int WIDTH = 800; private static final int HEIGHT = 600; public static void main( String[] args ) throws IOException { final Display display = new Display(); final Shell shell = new Shell(); shell.setLayout(new FillLayout()); final Browser browser = new Browser(shell,SWT.EMBEDDED); browser.addProgressListener(new ProgressListener() { @Override public void changed( ProgressEvent event ) {} @Override public void completed( ProgressEvent event ) { shell.forceActive(); display.asyncExec(new Runnable() { @Override public void run() { grab(display,shell,browser); } }); } }); browser.setUrl("http://www.google.com"); shell.setSize(WIDTH,HEIGHT); shell.open(); while ( !shell.isDisposed() ) { if ( !display.readAndDispatch() ) display.sleep(); } display.dispose(); } private static void grab( final Display display,final Shell shell,final Browser browser ) { final Image image = new Image(display,browser.getBounds()); GC gc = new GC(browser); gc.copyArea(image,0); gc.dispose(); ImageLoader loader = new ImageLoader(); loader.data = new ImageData[] { image.getImageData() }; loader.save("foo.png",SWT.IMAGE_PNG); image.dispose(); shell.dispose(); } }
但有一些严重的警告:
>你不能在屏幕外做这个. SWT截图只是当前显示的副本.
>屏幕截图时,包含浏览器的窗口必须位于顶部.
>该页面应该在onLoad之后可见(实际上并不是google.com的情况,但是由于asyncExec调用,对我而言 – 如果您获得了白色图像,请尝试另一个URL)
>结果取决于您的操作系统及其安装的浏览器