加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 生成网站的缩略图?

发布时间:2020-12-14 05:10:36 所属栏目:Java 来源:网络整理
导读:对于我的应用程序,我需要动态创建网站的缩略图.到目前为止,我有这样的代码从SO: public class CreateWebsiteThumbnail { private static final int WIDTH = 128; private static final int HEIGHT = 128; private BufferedImage image = new BufferedImage(
对于我的应用程序,我需要动态创建网站的缩略图.到目前为止,我有这样的代码从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没有打开,没有大小,浏览器没有时间实际加载任何东西,没有事件循环似乎运行,以启用任何绘图,…

以下代码使用SWT浏览器进行页面截图:

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)
>结果取决于您的操作系统及其安装的浏览器

我会用非Java解决方案,以便下载屏幕绘图.我相信链接的问题可能会帮助你进一步.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读