2014-10-20 116 views
4

我有a.html文件,其中包含一個表,我想將它轉換並保存爲本地圖像。HTML文件將其轉換爲本地圖像文件

可能嗎?

爲了將其保存爲圖像我嘗試這樣做:

ImageIO.write(image, "png", new File("image.png")); 

a library to convert html to image,但它是一個在線的HTML頁面。

+0

所以你想把表格保存爲圖像?您可以操縱dom並製作圖像 – 2014-10-20 12:18:51

+0

html文件的內容是一個表格,但是我想要讀取的文件是html擴展名,我想將其轉換爲圖像 – 2014-10-20 12:32:51

回答

1

你可以通過使用命令行程序的組合:

  • wkhtmltopdf將你的HTML文件轉換成PDF文件
  • convert(來自ImageMagick的)變成PDF到PNG(或其他圖像文件格式)
+0

謝謝您的回答。因爲我在Java中很新,所以從未使用過wkhtmltopdf。據我所知,我必須使用類似於ProcessBuilder pb = new ProcessBuilder(「wkhtmltopdf.exe」,htmlFilePath,pdfFilePath);但在此之後,我應該逐行閱讀文件嗎?如果有可能你能給我一個例子嗎? – 2014-10-20 12:52:55

+2

@SaraBalloiuk不會這將爲您的html文件生成PDF,然後使用ImageMagick將該pdf轉換爲圖像文件。您不需要逐行閱讀 – 2014-10-20 12:55:32

2

這裏是一個使用java提供的程序swings

public class MyClass { 

    public static void main(String[] args) throws IOException { 
     URL url = MyClass.class.getResource("myhtml.html"); 
     Dimension size = new Dimension(200, 200); 
     Image img = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB); 
     JFrame frame = new JFrame(); 
     frame.setUndecorated(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JEditorPane pane = new JEditorPane(url) { 
      @Override 
      protected void paintComponent(Graphics g) { 
       super.paintComponent(g); 
      } 
     }; 
     frame.setSize(size); 
     frame.add(pane); 
     frame.setVisible(true); 
     Graphics g = img.getGraphics(); 
     pane.paintAll(g); 
     ImageIO.write((RenderedImage) img, "jpg", new FileOutputStream("myimg.jpg")); 
     frame.setVisible(false); 
     frame.dispose(); 
    } 
} 

但它會顯示一個閃光(未修飾的幀一秒)。
這裏是我使用的示例:
myhtml.html:

<table border="1"> 
    <tr><td>one</td><td>two</td><td>three</td></tr> 
    <tr><td>four</td><td>five</td><td>six</td></tr> 
    <tr><td>seven</td><td>eight</td><td>nine</td></tr> 
</table> 

enter image description here

我承認這是不是做這個的有效方法。但它是由java提供的標準GUI完成的

1

您可以將您的HTML文件加載到WebView中,並以文本方式執行屏幕截圖並將該屏幕截圖保存到文件中。您可以將該片段中的網址替換爲您的本地文件。

import java.io.FileOutputStream; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.graphics.Picture; 
import android.os.Bundle; 
import android.view.Menu; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 

    public class MainActivity extends Activity { 
    WebView w ; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     w = new WebView(this); 
     w.setWebViewClient(new WebViewClient(){ 
      public void onPageFinished(WebView view, String url){ 
       Picture picture = view.capturePicture(); 
       Bitmap b = Bitmap.createBitmap(picture.getWidth(), 
       picture.getHeight(), Bitmap.Config.ARGB_8888); 
       Canvas c = new Canvas(b); 
       picture.draw(c); 
       FileOutputStream fos = null; 
       try { 
        fos = new FileOutputStream("mnt/sdcard/yahoo.jpg"); 
        if(fos != null){ 
         b.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
         fos.close(); 
        } 
       }catch(Exception e){} 
      } 
     }); 
     setContentView(w); 
     w.loadUrl("http://search.yahoo.com/search?p=android"); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    }