Today, I saw a user's consultation post on "How to implement web page technology using Java". Because the location of this consultation is not suitable for long replies, I replied in the form of a blog post.
In fact, if you want to implement a web page in Java, that is, "enter a website, and a thumbnail of a web page will be captured in a few seconds. You can select at least three methods.
1. The most direct method -- using robot
Method Description: This method uses the powerful desktop operation capability provided by robat to call the browser to open a specified webpage and save the webpage information to the local device.
Advantage: it is easy to use and does not require any third-party plug-ins.
Disadvantage: it cannot process a large amount of data at the same time, and the technical content is too low. It is an emergency skill.
Implementation Method: Use the following code.
Public static void main (string [] ARGs) throws malformedurlexception, <br/> ioexception, urisyntaxexception, awtexception {<br/> // This method is only applicable to jdk1.6 and later versions <br/> desktop. getdesktop (). browse (<br/> new URL ("http://google.com/intl/en "). touri (); <br/> Robot robot = new robot (); <br/> robot. delay( 10000); <br/> dimension D = new dimension (Toolkit. getdefatooltoolkit (). getscreensize (); <br/> int width = (INT) d. getwidth (); <br/> int Height = (INT) d. getheight (); <br/> // maximize the browser <br/> robot. keyrelease (keyevent. vk_f11); <br/> robot. delay( 2000); <br/> image = robot. createscreencapture (New rectangle (0, 0, width, <br/> height); <br/> bufferedimage Bi = new bufferedimage (width, height, <br/> bufferedimage. type_int_rgb); <br/> graphics G = Bi. creategraphics (); <br/> G. drawimage (image, 0, 0, width, height, null); <br/> // Save the image <br/> ImageIO. write (Bi, "jpg", new file ("google.jpg"); <br/>}< br/>
2. The most common method-call a third-party C/C ++ component using JNI
Detailed explanation of the method: at present, Java is obviously insufficient for the development of Web Components (Business Opportunities ?), When you need to complete this operation, you must have encountered the weakness of Java. However, as we all know, Java also has powerful JNI capabilities, so that similar components developed by C/C ++ can be easily used by itself.
Advantage: the implementation is simple. You only need to encapsulate the corresponding DLL file to implement similar functions in Java.
Disadvantage: Like other JNI implementations, there are risks in cross-platform applications, and your programs will no longer be pure Java applications.
Implementation Method: see this example. Select the C/C ++ components to encapsulate.
PS: Example from aca html to image converter Project (http://www.acasystems.com/en/web-thumb-activex/faq-convert-html-to-image-in-java.htm
), This is a charged third-party HTML to image component, but the encapsulation method is similar in Java.
Reference JNI encapsulation:
Import sun. AWT. *; <br/> Import Java. AWT. *; <br/> Import javax. swing. *; <br/> Import Java. AWT. event. *; <br/> Import Java. AWT. *; <br/> Import Java. AWT. peer. *; <br/> public class snap <br/> {<br/> static <br/>{< br/> system. loadlibrary ("Snap"); <br/>}< br/> Public static void main (string [] argv) <br/>{< br/> snap t_xsnap = new snap (); <br/> t_xsnap.start ("http://www.google.com", "snapshot-google.png "); <br/>}< br/> Public native void start (string pi_strurl, string pi_strimagename); <br/>}< br/>
CPP implementation:
# Include <windows. h> <br/> # include <atlbase. h> <br/> # include "snap. H "<br/> # pragma comment (Lib," ATL. lib ") <br/> # import ". /.. /.. /acawebthumb. DLL "no_namespace <br/> jniexport void jnicall java_snap_start (jnienv * penv, jobject, jstring pi_strurl, jstring pi_strfilename) <br/>{ <br/> coinitialize (0 ); <br/> _ bstr_t t_strurl = penv-> getstringutfchars (pi_strurl, 0); <br/> _ bstr_t t_strfilename = penv-> getstringutfchars (pi_strfilename, 0 ); <br/> ithumbmakerptr html_converter = NULL; <br/> hresult hR = html_converter.createinstance (L "acawebthumb. thumbmaker "); <br/> If (succeeded (HR) <br/>{< br/> html_converter-> seturl (t_strurl ); <br/> If (0 = html_converter-> startsnap () <br/> html_converter-> saveimage (t_strfilename ); <br/>}< br/> If (html_converter) <br/> html_converter.release (); <br/> couninitialize (); <br/>}< br/>
This component is used to visualize the Yahoo interface:
3. the most solid method-parse HTML tags and visualize them
Detailed explanation of the method: As we all know, HTML appears in a browser in a specific webpage format, instead of uploading an entire application to the client on the server side, it is derived from the browser's self-parsing results on the client side. Therefore, as long as we implement the corresponding resolution one by one, it is not difficult to visualize the web page.
Advantages: pure Java implementation. Once and for all, once the development is complete, it will always be used and has some commercial value.
Disadvantage: It is time-consuming for development and requires precise analysis of different syntaxes to ensure that the output is basically correct. Especially when JavaScript Parsing is involved, the difficulty is particularly increased.
Implementation Method: there are no specific cases for reference. However, because Java has browser projects such as JDIC (https://jdic.dev.java.net/
), And the Java graphical interface is generated by drawing. Theoretically, we can save all graphics components graphically.
If you want to parse the file by yourself, you need to create an HTML Parser (or use a third-party, fortunately Java has many components in this area), understand the java2d mechanism, and know when to use drawstring to draw text, when and when to use drawimage to insert images.
Supplement:
This is an example of using a built-in browser and using the djnativeswing component.
Example Project (Eclipse project, including Lib): http://greenvm.googlecode.com/files/Screenshot.7z
Import Java. AWT. borderlayout; <br/> Import Java. AWT. dimension; <br/> Import Java. AWT. flowlayout; <br/> Import Java. AWT. image. bufferedimage; <br/> Import Java. io. file; <br/> Import Java. io. ioexception; <br/> Import javax. imageIO. imageIO; <br/> Import javax. swing. jframe; <br/> Import javax. swing. jpanel; <br/> Import javax. swing. swingutilities; <br/> Import chrriis. DJ. nativeswing. swtimpl. nativecomponent; <br/> I Mport chrriis. DJ. nativeswing. swtimpl. nativeinterface; <br/> Import chrriis. DJ. nativeswing. swtimpl. components. jwebbrowser; <br/> Import chrriis. DJ. nativeswing. swtimpl. components. webbrowseradapter; <br/> Import chrriis. DJ. nativeswing. swtimpl. components. webbrowserevent; <br/> public class main extends jpanel {<br/>/** <br/> */<br/> Private Static final long serialversionuid = 1l; <br/> // line delimiter <br /> Final static Public String ls = system. getproperty ("line. separator ","/N "); <br/> // file delimiter <br/> final static Public String FS = system. getproperty ("file. separator "," // "); <br/> // use a Javascript script to obtain the full-screen webpage size <br/> final static stringbuffer jsdimension; </P> <p> static {<br/> jsdimension = new stringbuffer (); <br/> jsdimension. append ("Var width = 0 ;"). append (LS); <br/> jsdimension. append ("Var Height = 0 ;"). A Ppend (LS); <br/> jsdimension. append ("if(document.doc umentelement ){"). append (LS); <br/> jsdimension. append (<br/> "width = math. max (width, document.doc umentelement. scrollwidth); ") <br/>. append (LS); <br/> jsdimension. append (<br/> "Height = math. max (height, document.doc umentelement. scrollheight); ") <br/>. append (LS); <br/> jsdimension. append ("}"). append (LS); <br/> jsdimension. append ("If (self. innerwidth) {"). Append (LS); <br/> jsdimension. append ("width = math. max (width, self. innerwidth); ") <br/>. append (LS); <br/> jsdimension. append ("Height = math. max (height, self. innerheight); ") <br/>. append (LS); <br/> jsdimension. append ("}"). append (LS); <br/> jsdimension. append ("If (document. body. scrollwidth ){"). append (LS); <br/> jsdimension. append (<br/> "width = math. max (width, document. body. scrollwidth); ") <br/>. A Ppend (LS); <br/> jsdimension. append (<br/> "Height = math. max (height, document. body. scrollheight); ") <br/>. append (LS); <br/> jsdimension. append ("}"). append (LS); <br/> jsdimension. append ("Return width + ':' + height ;"); <br/>}< br/> // download the djnativeswing component from http://djproject.sourceforge.net/main/index.html< br/> public main (final string URL, final int maxwidth, final int maxheight) {<br/> super (New borderla Yout (); <br/> jpanel webbrowserpanel = new jpanel (New borderlayout (); <br/> final string filename = system. currenttimemillis () + ". jpg "; <br/> final jwebbrowser webbrowser = new jwebbrowser (null); <br/> webbrowser. setbarsvisible (false); <br/> webbrowser. navigate (URL); <br/> webbrowserpanel. add (webbrowser, borderlayout. center); <br/> Add (webbrowserpanel, borderlayout. center); <br/> jpanel = new JPA Nel (New flowlayout (flowlayout. center, 4, 4); <br/> webbrowser. addwebbrowserlistener (New webbrowseradapter () {<br/> // listen for the loading progress <br/> Public void loadingprogresschanged (webbrowserevent E) {<br/> // when loading is complete <br/> If (E. getwebbrowser (). getloadingprogress () = 100) {<br/> string result = (string) webbrowser <br/>. executejavascriptwithresult (jsdimension. tostring (); <br/> int Index = Result = NULL? -1: result. indexof (":"); <br/> nativecomponent = webbrowser <br/>. getnativecomponent (); <br/> dimension originalsize = nativecomponent. getsize (); <br/> dimension imagesize = new dimension (integer. parseint (result <br/>. substring (0, index), integer. parseint (result <br/>. substring (index + 1); <br/> imagesize. width = math. max (originalsize. width, <br/> imagesize. width + 50); <br/> imagesize. height = math. max (originalsize. height, <br/> imagesize. height + 50); <br/> nativecomponent. setsize (imagesize); <br/> bufferedimage image = new bufferedimage (imagesize. width, <br/> imagesize. height, bufferedimage. type_int_rgb); <br/> nativecomponent. paintcomponent (image); <br/> nativecomponent. setsize (originalsize); <br/> // when the webpage exceeds the target size <br/> If (imagesize. width> maxwidth <br/> | imagesize. height> maxheight) {<br/> // some images <br/> image = image. getsubimage (0, 0, maxwidth, maxheight); <br/>/* use a thumbnail <br/> int width = image. getwidth (), height = image <br/>. getheight (); <br/> affinetransform Tx = new affinetransform (); <br/> Tx. scale (double) maxwidth/width, (double) maxheight <br/>/height); <br/> affinetransformop op = new affinetransformop (TX, <br/> affinetransformop. type_nearest_neighbor); <br/> // zoom out <br/> image = op. filter (image, null); */<br/>}< br/> try {<br/> // output the image <br/> ImageIO. write (image, "jpg", new file (filename); <br/>} catch (ioexception ex) {<br/> ex. printstacktrace (); <br/>}< br/> // exit <br/> system. exit (0); <br/>}< br/>); <br/> Add (panel, borderlayout. south); <br/>}< br/> Public static void main (string [] ARGs) {<br/> nativeinterface. open (); <br/> swingutilities. invokelater (New runnable () {<br/> Public void run () {<br/> // switch the SWT component to the swing component, if the parent form is not initialized, webbrowser cannot be started. <br/> jframe frame = new jframe ("Save specified webpage with DJ component"); <br/> // load specified webpage, A frame with a maximum of xforwardedframes <br/>. getcontentpane (). add (<br/> New Main ("http://blog.csdn.net/cping1982", 640,480), <br/> borderlayout. center); <br/> frame. setsize (800,600); <br/> // initialization only, but not <br/> frame. invalidate (); <br/> frame. pack (); <br/> frame. setvisible (false); <br/>}< br/>}); <br/> nativeinterface. runeventpump (); <br/>}< br/>