1. The Java intercepts the webpage to form the picture

Source: Internet
Author: User

1. Idle Chatter

The open source framework for GUI programming is called SWT (Standard Widget Toolkit), which is similar to Sun's AWT, swing. Non-interface programmers, here do not rip their pros and cons.

Perhaps a lot of people have not heard of SWT, but if you say a tool produced by it, you may be awed by it. This tool is an eclipse developed by IBM. So if we were to develop plugins for Eclipse, we would have to know the SWT very well.

2. Requirements

To intercept Web pages to form images, 1-2 requirements are required. The 1 here is to configure the java.libarary.path;2 to be two jar packages required.

1 Requirements: Configure Java.library.path.

Perhaps you all know the configuration environment variable path. But what is the role of this environment variable and the Java.library.path?

Path: Specifies Classpath, which is where the Java class can be found, and sometimes Java may need to take advantage of a local code library file (a DLL file on Windows, and so on Linux). How does the system find this dynamic link library? Of course, a variable is required to point to, and this variable is java.library.path.

2 requirements: Get two jar packs.

A jar package is in eclipse. Open the installation directory for Eclipse, my directory is "E:\eclipse\plugins", there will be a Org.eclipse.swt.win32.x86_64_***.jar package below.

Another package is a jar package that was developed based on this eclipse package for practical applications such as intercepting Web pages to form images and rendering them into HTML files. This jar package is specific to who developed the Don't know. Its items can be downloaded by clicking on it.

3. Deployment

Add two packages to your project. In addition, the jar that was taken out of Eclipse is decompressed in another place, and you can see a dynamic link library file with three DLLs in it. We copy these three files to a directory. My directory is "D:/SWT". Then, you can configure the virtual machine parameters when the development is running, by right-clicking on the project-->run as-->run configurations. This allows Java.library.path to point to several dynamic link libraries that are required during the program's run . Otherwise it would throw:java.lang.UnsatisfiedLinkError no *****.dll in Java.library.path, such an exception. Here we are ready to test. Look at the effect of its amazement.

4. Analysis

When we learn the GUI programming, we all know that we should make the layout of the view first. The view here is a panel. Sets the style and size of the panel. The overlay panel above the panel makes an awesome style, or adds listening events to the panel to interact with the user.

The same is true for intercepting Web pages here. How is the Web page rendered? The style layout of the Web page is rendered by the browser based on the script, and here we take advantage of the browser's "panel". There is also a panel "shell" underneath the browser. It is also said that the shell Panel has a browser panel, we pass a URL to the browser panel, the panel can be rendered to its returned page. Here we execute the browser panel size is a frame, now we only care about its height, this height can be specified by us (of course, we need to modify its source code), the default is 500. By executing JavaScript scripts in Java code, we can return the total height of the page content. The total height of the/500+1 is that we need to brush this page a few times to get a complete page. The core method of the project is as follows (the default is to brush the screen several times to complete the page capture, I will change it to brush a screen to capture the full Web page, but to the thread sleep time to make a point, otherwise the page has not been rendered, it has been captured, then the resulting image of the data will be incomplete):

1 Final intperhight=2027;2      Public voidRenderFinalFile savelocation)throwsrenderingexception3     {4         FinalFile localsavelocation =NewFile (Savelocation.getparentfile (). GetAbsolutePath () + file.separator + "localhost");5 localsavelocation.mkdirs ();6SYSTEM.OUT.PRINTLN ("Saving temporary images in directory:" +Localsavelocation.getabsolutepath ());7 8String Widthstr =NULL;9         //get the width and height of Web content based on JavaScript scriptsTenWidthstr = This. Getjavascriptvalue ("document.body.scrollwidth+ '-' +document.body.scrollheight"); One         if(Widthstr = =NULL) A             Throw NewRenderingexception ("Could not acquire Document height andf width by javascript."); -  -         Final intwidth =Screen_width; the         //if the page content height is greater than 10000 then the value is 10000, otherwise the content height itself -         intNonfinalheight = Math.min (10000, Integer.parseint (Widthstr.split ("-") [1])); -          -         //Perhight is the height of each frame brush, if the content height of the Web page is less than the height of a frame to take the value of a frame; otherwise, the height of the value content +         Final intHeight = Nonfinalheight < perhight?Perhight:nonfinalheight; -  +System.out.println ("Screen sizes:" + width + "," +height); A          at         intStart=0; -          -         //then divide the height variable, which is the content altitude, by a frame height, which is how many times you want to brush the page.  -         //We also want to move the content of the Web page, mobile Web content, as long as the mobile browser on the y-axis of the scroll bar on the line, -         //initial state, no scrolling required, i.e. when i=0. When I=1, it is necessary to scroll the contents of a frame, because each time scrolling, the scrollbar is initially in 0 positions, so each start starts at 0.  -          in          -          for(inti = 0; I < 1 + (height/perhight); i++) to         { +             if(I! = 0) -             { the                  This. Execute ("Window.scrollto" ("+start+", "+ I * (perhight-scrollbary) +"); "); *                 Try $                 {Panax NotoginsengThread.Sleep (10000);//this time a little longer, or the page has not been brushed out to capture this frame -                 } the                 Catch(interruptedexception e) +                 { A                     Throw Newrenderingexception (e); the                 } +             } -             Final intj =i; $             //The following async method is capturing a frame of a Web page $             //The height and width of the screen presented here is fixed, but in this fixed area, the content is changing.  -             //so all we have to do is capture this piece of fixed area.  -Display.syncexec (NewRunnable () the             { -                  Public voidRun ()Wuyi                 { the                     if(j = = 0 | | J < (height/perhight)) -Capture (Localsavelocation.getabsolutepath (), J, Screen_width,perhight, 0, 0); Wu                     Else -Capture (Localsavelocation.getabsolutepath (), J, Screen_width, (height% perhight = = 0? perhight:height% perhight), 0, Perhight-(Height%perhight)); About                 } $             }); -         } -Display.syncexec (NewRunnable () -         { A              Public voidRun () +             { the                 Try -                 { $                     FinalImage result =NewImage (display, width, height); theGC GC =NewGC (result); the                      for(inti = 0; I < (1 + height/perhight); i++) the                     { the  -Imageloader Imageloader =NewImageloader (); inFile Partialimagefile =NewFile (localsavelocation, "savedimage" + i + ". jpg"); theFileInputStream FIS =NewFileInputStream (partialimagefile); theImage Loadedimage =NewImage (Display, Imageloader.load (FIS) [0]); AboutGc.drawimage (loadedimage, 0, I * (Perhight-scrollbary)); the loadedimage.dispose (); the fis.close (); the                         //Delete The partial image file Once we are doing with +                         //it: - Partialimagefile.delete (); the                     }Bayi gc.dispose (); theImageloader Imagesaver =NewImageloader (); theImagesaver.data =Newimagedata[] {result.getimagedata ()}; -SYSTEM.OUT.PRINTLN ("Saving image to location:" +savelocation); -FileOutputStream fos =NewFileOutputStream (savelocation); the imagesaver.save (FOS, SWT. IMAGE_JPEG); the fos.close (); the result.dispose (); the  -                 } the                 Catch(Exception e) the                 { the e.printstacktrace ();94                 } the             } the         }); the 98}

1. The Java intercepts the webpage to form the picture

Related Article

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.