2010-10-18 11 views
5

Qualcuno sa come fare? Ho provato con JEditorPane ma non funziona? Qualche altra idea?Come convertire l'HTML di un sito Web in un'immagine?

Grazie in anticipo.

Questo è il codice che sto utilizzando:

import java.awt.Dimension; 
import java.awt.Graphics2D; 
import java.awt.image.BufferedImage; 
import java.io.File; 

import javax.imageio.ImageIO; 
import javax.swing.JEditorPane; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 

public class HtmlToImage 
    { 

     protected static File generateOutput() throws Exception 
     { 
      // Create a temporary output file for the PNG image. 
      File outputFile = new File("Reporte.png"); 
      outputFile.deleteOnExit(); 

      JEditorPane pane = new JEditorPane(); 
      pane.setContentType("text/html"); 
      pane.setPage("http://www.google.com"); 
      final JFrame frame = new JFrame(); 
      frame.pack(); 

      // Time Delay for the correct loading of the file. 
      try 
      { 
       Thread.sleep(5000); 
      } 
      catch(NumberFormatException nfe) 
      { 
      } 

      frame.add(pane); 
      frame.pack(); 

      Dimension prefSize = pane.getPreferredSize(); 
      pane.setSize(prefSize); 

      BufferedImage img = new BufferedImage( prefSize.width, prefSize.height, 
                BufferedImage.TYPE_INT_RGB); 
      Graphics2D g = (Graphics2D) img.getGraphics(); 

      SwingUtilities.paintComponent(g, pane, frame, 0, 0, prefSize.width, prefSize.height); 

      ImageIO.write(img, "png", outputFile); 

      return outputFile; 
     } 

     public static void main(String[] args) 
     { 
      try 
      {    
       generateOutput(); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
     } 

    } 
+0

come non funziona? – Bozho

+1

Immagino perché JEditorPane è un componente Swing e non ha nulla a che fare con l'HTML. –

+0

Ehi, @Bozho, grazie per la risposta. Il problema è l'immagine risultante, è totalmente malformata. – hernangarcia

risposta

3

È necessario per il rendering HTML e in uscita il risultato come un file di immagine. Attualmente non esiste un renderer HTML completo nel core Java, quindi avrai bisogno di una libreria o un'applicazione separata, ad esempio WebRenderer. Basta richiamarlo da un filtro servlet e sovrascrivere la risposta con i risultati del rendering.

Modifica open source alternativa a WebRenderer è Cobra

+0

Grazie a @Saul sto costruendo un'app opensourced, quindi mi piacerebbe utilizzare gli strumenti/librerie opensourced. – hernangarcia

+0

HTMLEditorKit di Swing fornisce il rendering HTML (anche se primitivo). http://download.oracle.com/javase/6/docs/api/javax/swing/text/html/HTMLEditorKit.html – dogbane

+0

@dogbane: HTMLEditorKit supporta solo un set minimo di funzionalità dei siti Web moderni. Non è un motore di rendering web a tutti gli effetti. – Saul

1

Si potrebbe provare a utilizzare un JEditorPane come segue:

//load the webpage into the editor 
JEditorPane ed = new JEditorPane(new URL("http://www.google.com")); 
ed.setSize(200,200); 

//create a new image 
BufferedImage image = new BufferedImage(ed.getWidth(), ed.getHeight(), 
             BufferedImage.TYPE_INT_ARGB); 

//paint the editor onto the image 
SwingUtilities.paintComponent(image.createGraphics(), 
           ed, 
           new JPanel(), 
           0, 0, image.getWidth(), image.getHeight()); 

//save the image to file 
ImageIO.write((RenderedImage)image, "png", new File("google.png")); 
+0

Lo schermo è tutto bianco, di colore bianco. –

-1

È anche possibile utilizzare Html2Image API Java da parte di Google.

Problemi correlati