|
Running Environment: jdk1.6 or above
Import java. awt. BorderLayout;
Import java. awt. Dimension;
Import java. awt. FlowLayout;
Import java. awt. image. BufferedImage;
Import java. io. File;
Import java. io. IOException;
Import javax. imageio. ImageIO;
Import javax. swing. JFrame;
Import javax. swing. JPanel;
Import javax. swing. SwingUtilities;
Import chrriis. dj. nativeswing. swtimpl. NativeComponent;
Import chrriis. dj. nativeswing. swtimpl. NativeInterface;
Import chrriis. dj. nativeswing. swtimpl. components. JWebBrowser;
Import chrriis. dj. nativeswing. swtimpl. components. WebBrowserAdapter;
Import chrriis. dj. nativeswing. swtimpl. components. WebBrowserEvent;
Public class Main extends JPanel {
/**
*
*/
Private static final long serialVersionUID = 1L;
// Line Separator
Final static public String LS = System. getProperty ("line. separator", "n ");
// File delimiter
Final static public String FS = System. getProperty ("file. separator ","\");
// Use a javascript script to obtain the page size after full screen
Final static StringBuffer jsDimension;
Static {
JsDimension = new StringBuffer ();
JsDimension. append ("var width = 0;"). append (LS );
JsDimension. append ("var height = 0;"). append (LS );
JsDimension. append ("if(document.doc umentElement) {"). append (LS );
JsDimension. append (
"Width = Math. max (width, document.doc umentElement. scrollWidth );")
. Append (LS );
JsDimension. append (
"Height = Math. max (height, document.doc umentElement. scrollHeight );")
. Append (LS );
JsDimension. append ("}"). append (LS );
JsDimension. append ("if (self. innerWidth) {"). append (LS );
JsDimension. append ("width = Math. max (width, self. innerWidth );")
. Append (LS );
JsDimension. append ("height = Math. max (height, self. innerHeight );")
. Append (LS );
JsDimension. append ("}"). append (LS );
JsDimension. append ("if (document. body. scrollWidth) {"). append (LS );
JsDimension. append (
"Width = Math. max (width, document. body. scrollWidth );")
. Append (LS );
JsDimension. append (
"Height = Math. max (height, document. body. scrollHeight );")
. Append (LS );
JsDimension. append ("}"). append (LS );
JsDimension. append ("return width + ':' + height ;");
}
Public Main (final String url, final int maxWidth, final int maxHeight ){
Super (new BorderLayout ());
JPanel webBrowserPanel = new JPanel (new BorderLayout ());
Final String fileName = System. currentTimeMillis () + ". jpg ";
Final JWebBrowser webBrowser = new JWebBrowser (null );
WebBrowser. setBarsVisible (false );
WebBrowser. navigate (url );
WebBrowserPanel. add (webBrowser, BorderLayout. CENTER );
Add (webBrowserPanel, BorderLayout. CENTER );
JPanel panel = new JPanel (new FlowLayout (FlowLayout. CENTER, 4, 4 ));
WebBrowser. addWebBrowserListener (new WebBrowserAdapter (){
// Listen on the loading progress
Public void loadingProgressChanged (WebBrowserEvent e ){
// When loading is complete
If (e. getWebBrowser (). getLoadingProgress () = 100 ){
String result = (String) webBrowser
. ExecuteJavascriptWithResult (jsDimension. toString ());
Int index = result = null? -1: result. indexOf (":");
NativeComponent nativeComponent = webBrowser
. GetNativeComponent ();
Dimension originalSize = nativeComponent. getSize ();
Dimension imageSize = new Dimension (Integer. parseInt (result
. Substring (0, index), Integer. parseInt (result
. Substring (index + 1 )));
ImageSize. width = Math. max (originalSize. width,
ImageSize. width + 50 );
ImageSize. height = Math. max (originalSize. height,
ImageSize. height + 50 );
NativeComponent. setSize (imageSize );
BufferedImage image = new BufferedImage (imageSize. width,
ImageSize. height, BufferedImage. TYPE_INT_RGB );
NativeComponent. paintComponent (image );
NativeComponent. setSize (originalSize );
// When the webpage exceeds the target size
If (imageSize. width> maxWidth
| ImageSize. height> maxHeight ){
// Some images
Image = image. getSubimage (0, 0, maxWidth, maxHeight );
/* Use a thumbnail.
Int width = image. getWidth (), height = image
. GetHeight ();
AffineTransform tx = new AffineTransform ();
Tx. scale (double) maxWidth/width, (double) maxHeight
/Height );
AffineTransformOp op = new AffineTransformOp (tx,
AffineTransformOp. TYPE_NEAREST_NEIGHBOR );
// Zoom out
Image = op. filter (image, null );*/
}
Try {
// Output image
ImageIO. write (image, "jpg", new File (fileName ));
} Catch (IOException ex ){
Ex. printStackTrace ();
}
// Exit the operation
System. exit (0 );
}
}
}
);
Add (panel, BorderLayout. SOUTH );
}
Public static void main (String [] args ){
NativeInterface. open ();
SwingUtilities. invokeLater (new Runnable (){
Public void run (){
// Switch the SWT component to the Swing component. webBrowser cannot be started if the parent form is not initialized.
JFrame frame = new JFrame ("Save specified webpage with DJ component ");
// Load google and save it as a maximum of 640xx
Frame. getContentPane (). add (
New Main (http://yijianfengvip.blog.163.com/, 640,480 ),
BorderLayout. CENTER );
Frame. setSize (800,600 );
// Only initialize but not display
Frame. invalidate ();
Frame. pack ();
Frame. setVisible (false );
}
});
NativeInterface. runEventPump ();
}
} |