Java – generate thumbnails of websites?

For my application, I need to dynamically create thumbnails of the website So far, I have such code from 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);
    }


}

However, as you can see here, I don't know which part of the browser should be passed to my capture method Any tips?

Solution

I don't see how the code you provide works The shell is not open, there is no size, the browser has no time to actually load anything, no event loop seems to run to enable any drawing

The following code uses the SWT browser to take a screenshot of the page:

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();
   }

}

But there are some serious warnings:

>You can't do this off screen SWT screenshot is only the copy currently displayed. > When taking screenshots, the window containing the browser must be at the top. > The page should be visible after onload (it's not really the case with google.com, but for me due to the asyncexec call - if you get a white image, try another URL) > the result depends on your operating system and the browser installed

I will use a non Java solution to download screen drawings I believe the link problem may help you further

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>