Java page screenshot based on URL, thumbnail

Source: Internet
Author: User

Source Address: http://blog.csdn.net/cping1982/article/details/5353049

public class Shotspic extends JPanel {private static final long serialversionuid = 1L;

Row delimiter final static public String LS = System.getproperty ("Line.separator", "\ n");

File delimiter final static public String FS = System.getproperty ("File.separator", "\ \");

The final static StringBuffer jsdimension of the size of the full screen after the Web page is obtained in JavaScript script; static {jsdimension = new StringBuffer (); Jsdimension.append ("var width = 0;").
Append (LS); Jsdimension.append ("var height = 0;").
Append (LS);
Jsdimension.append ("if (document.documentelement) {"). Append (LS); Jsdimension.append ("width = Math.max (width, document.documentElement.scrollWidth);").
Append (LS); Jsdimension.append ("height = Math.max (height, document.documentElement.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 shotspic (String URL) {Super (New BorderLayout ());
JPanel Webbrowserpanel = new JPanel (new BorderLayout ());
Final String fileName = "Screenshots.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 () {//Listener load Progress public void loadingprogresschanged ( Webbrowserevent e) {//when loading completed if (E.getwebbrowser (). getloadingprogress () = = 100) {//dynamic execution JS mostFinally returns the actual width and height of the Web page String result = (string) webbrowser.executejavascriptwithresult (jsdimension.tostring ());

SYSTEM.OUT.PRINTLN (result); 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 (

Index + 1));
ImageSize.Width = Math.max (originalsize.width,imagesize.width);

Imagesize.height = Math.max (originalsize.height,imagesize.height);
System.out.println (ImageSize.Width);


System.out.println (Imagesize.height);
Nativecomponent.setsize (ImageSize);
BufferedImage image = New BufferedImage (imagesize.width,imagesize.height, BUFFEREDIMAGE.TYPE_INT_RGB);

Nativecomponent.paintcomponent (image);
Nativecomponent.setsize (ImageSize); When the page exceeds the target large hours/*if (ImageSize.Width > maxwidth| | imagesize.height > MaxHeight) {//screenshot part graphic image = Image.getsubimage (0, 0, Maxwidth, maxheight);
}*//*//This section is using the 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);
Shrink image = Op.filter (image, NULL);
*/try {//Output image imageio.write (image, "JPG", New File ("e:\\" +filename)), catch (IOException ex) {ex.printstacktrace ();
}//Exit operation System.exit (0);
}
}
}


);


Add (Panel, Borderlayout.south);
public static void Main (string[] args) {nativeinterface.open ();

Swingutilities.invokelater (New Runnable () {public void run () {JFrame frame = new JFrame ("Save a screenshot of the specified page with DJ component");
Frame.getcontentpane (). Add (New Shotspic ("www.qq.com"), Borderlayout.center);
Frame.setsize (1280, 768);
Initialize only, but do not show frame.invalidate ();
Frame.pack ();
Frame.setvisible (FALSE);
}
});
Nativeinterface.runeventpump (); }

}

Program needs three packages:, download the address please refer to the original address!

Note: If the speed is not very good, will have a certain impact on the screenshot.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.