各位高手,请问有没有什么java开源组件能够实现将IE浏览器打开的网页转换为图片????

解决方案 »

  1.   

    he Flying Saucer Project
    An XML/XHTML/CSS 2.1 Renderer
    https://xhtmlrenderer.dev.java.net/Rendering to an ImageYou can render from a document directly to an image format of your choice using org.xhtmlrenderer.simple.ImageRenderer. To use ImageRenderer, just call ImageRenderer.renderToImage(url, path, width), or one of the overloaded versions of the method. You must specify either a width or a width and a height for the image if you like; if height is not specified, it's determined based on the content of the document. renderToImage() creates the document, writes it out to the given path, and returns a java.awt.image.BufferedImage which you can further manipulate—for example, scale and re-save or save in multiple image formats. Here's a simple sample rendering the contents of the http://www.w3c.org homepage:String address = "http://www.w3.org/";// render
    try {
      BufferedImage buff = null;
      buff = Graphics2DRenderer.renderToImage(address, "w3c-homepage.png", 1024);
    } catch (IOException e) {
      e.printStackTrace();
    }That's it. You can use Java's ImageIO class to write the image out in different formats; it supports writing most image formats you would want to use. Using BufferedImage, you can also manipulate (scale, rotate, transform) the images you create.
      

  2.   

    首先谢谢了,
    我想利用java把IE浏览器或firefox打开的整体网页截图成图片保存!
      

  3.   

    谢谢,谢谢,哈哈。我也是没办法,公司要求!.net应该好做这样的事吧!就为这个问题我问了好多做开发的同仁,回答都比较含糊。
      

  4.   

    投机取巧。直接写个按钮,点击按钮触发截屏事件(ctrl+printScreen),前提是浏览器全屏运行。多问一句,为什么要写这个东西?直接按键盘上的就可以得到网页的图片格式了啊
      

  5.   

    哈哈,这事我干过,先切频下来,然后用PHOTOSHOP拼起来,我已前这么干的,别人问的时候怎么说那就是你的事了。要是哪位有什么好方法也共享一下。
      

  6.   

    csdn回复  论坛 是不是 针对一个帖子 可以回复多少条都可以  不断加分  还是 针对一个帖子只需回复一条
      

  7.   

    package com.liuxin.utils;
    import com.sun.image.codec.jpeg.*;
    import java.io.*;
    import java.awt.*;   
    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;   
    import javax.swing.*;
    import chrriis.dj.nativeswing.swtimpl.*;   
    import chrriis.dj.nativeswing.swtimpl.components.*; 
    public class WebImage  extends JPanel{
    private static WebImage webImage=new WebImage();

    private WebImage(){};

    public static WebImage getWebImage(){
    return webImage;
    }
     /**  
         *   
         */  
    final static String fileName = System.currentTimeMillis() + ".jpg"; 
        private static final long serialVersionUID = 1L;   
        // 行分隔符   
        final static public String LS = System.getProperty("line.separator", " ");   
        // 文件分割符   
        final static public String FS = System.getProperty("file.separator", "\\");   
        //以javascript脚本获得网页全屏后大小   
        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.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;");   
        }   
      //DJNativeSwing组件请于http://djproject.sourceforge.net/main/index.html下载   
        public WebImage(final String url, final int maxWidth, final int maxHeight,final String contextpath) {     
            super(new BorderLayout());   
            JPanel webBrowserPanel = new JPanel(new BorderLayout());    
            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() {   
                // 监听加载进度   
                public void loadingProgressChanged(WebBrowserEvent e) {   
                    // 当加载完毕时   
                    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);  
                        //String contextpath=ServletActionContext.getServletContext().getRealPath("/upload/webimage");
                     String webimgpath=contextpath+"/"+fileName;
                        // 当网页超出目标大小时   
                        if (imageSize.width > maxWidth || imageSize.height > maxHeight) {   
                            //截图部分图形   
                           image = image.getSubimage(0, 0, maxWidth, maxHeight);                       
                        }   
                       try {   
                            // 输出图像   
                            ImageIO.write(image, "jpg", new File(webimgpath));   
                            reduceImg(webimgpath,webimgpath, 230,144);
                            addwater(webimgpath,contextpath+"\\shuiyin.jpg");
                        } catch (Exception ex) {   
                            ex.printStackTrace();   
                        }   
                        // 退出操作   
                       //System.exit(0); 
                    }   
                }   
            }   
            ); 
            add(panel, BorderLayout.SOUTH);   
        } 
        /**
         * 截图对象
         * @return
         */
        public String getWebImg(final String website,final String imgcontextpath) {
          NativeInterface.open();   
             SwingUtilities.invokeLater(new Runnable() {   
                public void run() {   
                     // SWT组件转Swing组件,不初始化父窗体将无法启动webBrowser   
                     JFrame frame = new JFrame("以DJ组件保存指定网页截图");   
                     // 加载指定页面,最大保存为640x480的截图   
                     frame.getContentPane().add(new WebImage(website, 1010, 480,imgcontextpath),BorderLayout.CENTER);   
                     frame.setSize(460, 288);   
                     // 仅初始化,但不显示   
                     frame.invalidate();   
                     frame.pack();   
                     frame.setVisible(false);   
                 }   
             });       
             NativeInterface.runEventPump(); 
             NativeInterface
             NativeInterface.close();
             return fileName;
        }
        /**
         * 缩小图片
         * @param imgsrc
         * @param imgdist
         * @param widthdist
         * @param heightdist
         */
        public static void reduceImg(String imgsrc, String imgdist, int widthdist,int heightdist) {
         try {
         File srcfile = new File(imgsrc);
         if (!srcfile.exists()) {
         return;
         }
         Image src = javax.imageio.ImageIO.read(srcfile);     BufferedImage tag = new BufferedImage((int) widthdist,(int) heightdist, BufferedImage.TYPE_INT_RGB);     tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist,Image.SCALE_SMOOTH), 0, 0, null);     FileOutputStream out = new FileOutputStream(imgdist);
         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
         encoder.encode(tag);
         out.close();    
         } catch (Exception ex) {
         ex.printStackTrace();
         }     }
        /**
         * 添加水印
         * @throws Exception
         */
        public static void addwater(String imgpath,String wmpath)  throws Exception{
          //需要添加水印的图片的路径    
         InputStream is = new FileInputStream(imgpath);    
                
                
            //通过JPEG图象流创建JPEG数据流解码器    
            JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is);    
            //解码当前JPEG数据流,返回BufferedImage对象    
            BufferedImage buffImg = jpegDecoder.decodeAsBufferedImage();    
            //得到画笔对象    
            Graphics g = buffImg.getGraphics();    
                
            //创建你要附加的图象。    
            //需要添加的图片水印   
            ImageIcon imgIcon = new ImageIcon(wmpath);     
                
            //得到Image对象。    
            Image img = imgIcon.getImage();    
                
            //将小图片绘到大图片上。    
            //5,300 .表示你的小图片在大图片上的位置。    
            g.drawImage(img,140,127,null);    
                
                
                
            //设置颜色。    
            g.setColor(Color.BLACK);    
                
                
            //最后一个参数用来设置字体的大小    
           // Font f = new Font("宋体",Font.BOLD,8);    
                
          //  g.setFont(f); 
            
                
            //10,20 表示这段文字在图片上的位置(x,y) .第一个是你设置的内容。    
           /* g.drawString("添加的水印文字(测试)",10,30);*/    
                
            g.dispose();    
                
                
                
            OutputStream os = new FileOutputStream(imgpath);    
                
            //创键编码器,用于编码内存中的图象数据。    
                
            JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);    
            en.encode(buffImg);    
                
                
            is.close();    
            os.close();    
        }
    public static void main(String[] args) {
         String str=WebImage.getWebImage().getWebImg("http://www.aihuaedu.cn","E:\\apache-tomcat-6.0.20\\webapps\\liuxin\\upload\\webimage\\");
         System.out.println(str);
        }}
    你参考一下,这是基于web的截图,可以缩小图片,可以加水印,还有一些架包,可以qq我:971475760
      

  8.   


    好像只能用utf-8格式的网页