java Html 그림 전환 실현

20003 단어 자바

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.text.EditorKit;
import javax.swing.text.html.HTMLEditorKit;

/**
 *   HtmlImageGenerator
 */

public class HtmlImgGenerator {
    private JEditorPane editorPane;
    private static final Dimension DEFAULT_SIZE = new Dimension(800, 800);
 
    public static Map types = new HashMap();
    static
    {
      types.put("gif", "gif");
      types.put("jpg", "jpg");
      types.put("jpeg", "jpg");
      types.put("png", "png");
    }
    public static String formatForExtension(String extension)
    {
      String type = (String)types.get(extension);
      if (type == null)
        return "png";

      return type;
    }

    public static String formatForFilename(String fileName) {
      int dotIndex = fileName.lastIndexOf(46);
      if (dotIndex < 0)
        return "png";

      String ext = fileName.substring(dotIndex + 1);
      return formatForExtension(ext);
    }

    
    public HtmlImgGenerator() {
        editorPane = createJEditorPane();
    }
 
 
 
    public void setSize(Dimension dimension) {
        editorPane.setPreferredSize(dimension);
    }
 
//    public void loadUrl(URL url) {
//        try {
//            editorPane.setPage(url);
//        } catch (IOException e) {
//            throw new RuntimeException(String.format("Exception while loading %s", url), e);
//        }
//    }
 
    public void loadUrl(String url) {
        try {
            editorPane.setPage(url);
        } catch (IOException e) {
            throw new RuntimeException(String.format("Exception while loading %s", url), e);
        }
    }
 
    public void loadHtml(String html) {
        editorPane.setEditable(false);
        editorPane.setText(html);
        editorPane.setContentType("text/html");
        onDocumentLoad();
    }
 
 
    public void saveAsImage(String file) {
        saveAsImage(new File(file));
    }
 
    public void saveAsImage(File file) {
        BufferedImage image = getBufferedImage();
 
        BufferedImage bufferedImageToWrite = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
        bufferedImageToWrite.createGraphics().drawImage(image, 0, 0, Color.WHITE, null);
 
        final String formatName =  formatForFilename(file.getName());
        try {
            if (!ImageIO.write(bufferedImageToWrite, formatName, file))
                throw new IOException("No formatter for specified file type [" + formatName + "] available");
        } catch (IOException e) {
            throw new RuntimeException(String.format("Exception while saving '%s' image", file), e);
        }
    }
 
    protected void onDocumentLoad() {
    }
 
    public Dimension getDefaultSize() {
        return DEFAULT_SIZE;
    }
 
    public BufferedImage getBufferedImage() {
        JFrame frame = new JFrame();
        frame.setPreferredSize(editorPane.getPreferredSize());
        frame.setUndecorated(true);
        frame.add(editorPane);
        frame.pack();
 
        Dimension prefSize = frame.getPreferredSize();
        BufferedImage img = new BufferedImage(prefSize.width, prefSize.height, BufferedImage.TYPE_INT_ARGB);
        Graphics graphics = img.getGraphics();
 
        frame.setVisible(true);
        frame.paint(graphics);
        frame.setVisible(false);
        frame.dispose();
 
        return img;
    }
 
    protected JEditorPane createJEditorPane() {
        final JEditorPane editorPane = new JEditorPane();
        editorPane.setSize(getDefaultSize());
        editorPane.setEditable(false);
          EditorKit  kit = new HTMLEditorKit();
        editorPane.setEditorKitForContentType("text/html", kit);
        editorPane.setContentType("text/html");
     
        return editorPane;
    }
 
    public static void main(String[] args) {
    	/*
        HtmlImgGenerator imageGenerator = new HtmlImgGenerator();
        imageGenerator.setSize(new Dimension(1000, 1000));
        String url = new File("D:/test.html").toURI().toString();
        imageGenerator.loadUrl(url);
        //imageGenerator.loadHtml(String html);//    html
        imageGenerator.getBufferedImage();
        imageGenerator.saveAsImage("D:/aaa.png");
        */
    	String content="

2( ):
, , , 2( ), 07 1 , 。

/ :
2020 01 01
"; html2Img(" ",content,"D:/aaa.png"); } /** * * @Description HTML Image * @param htmText HTML * @return Image Location */ public static String html2Img(String title,String content, String saveImageLocation){ StringBuffer buffer = new StringBuffer(); buffer.append(" "); buffer.append(" "); buffer.append(" "); buffer.append(" <style> "); buffer.append(" </style> "); buffer.append(" "); buffer.append(" "); buffer.append(" <div class="\"main2\"" style="\"margin-left:"> "); buffer.append(" <table class="\"tab2\"" width="\"100%\""> "); buffer.append(" <tr> "); buffer.append(" <td class="\"td_bg2\"" style="\"" width:10="" align="\"right\""> :</td> "); buffer.append(" <td> #bt# </td> "); buffer.append(" </tr> "); buffer.append(" <tr> "); buffer.append(" <td class="\"td_bg2\"" align="\"right\""/> "); buffer.append(" <td> "); buffer.append(" <div id="\"content\"" name="\"content\"" style="\"width:90%;height:450px;"> "); buffer.append(" #nr# "); buffer.append(" </div> "); buffer.append(" </td> "); buffer.append(" </tr> "); buffer.append(" </table> "); buffer.append(" </div> "); buffer.append(" "); buffer.append(" "); HtmlImgGenerator imageGenerator = new HtmlImgGenerator(); try { String str=buffer.toString(); str = str.replaceAll("#bt#",title); str = str.replaceAll("#nr#",content); //str = str.toString().replaceAll(" ","      "); //str = str.toString().replaceAll(" ","           "); //str = str.toString().replaceAll("style=\"float:right; clear:both;\">","style=\"float:right; clear:both;\">     "); str = str.toString().replaceAll("<span style="\"margin-left:30px\"">","<span style="\"margin-left:30px\"">     "); str = str.toString().replaceAll("<span style="\"margin-left:32px\"">","<span style="\"margin-left:32px\"">          "); str = str.toString().replaceAll("<style clear:both="">","<style=\"float:right; clear:both;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"); str = str.toString().replaceAll("style=\"float:right; clear:both;\">","style=\"float:right; clear:both;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"); //str = str.toString().replaceAll("<br> ","<br> <br> "); System.out.println(str); imageGenerator.loadHtml(str); imageGenerator.getBufferedImage(); imageGenerator.saveAsImage(saveImageLocation); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(" HTML "); } return saveImageLocation; } }</code></pre> <p> </p> </div> </div> </div> </div> </div> <!--PC WAP --> <div id="SOHUCS" sid="1280689891558047744"></div> <script type="text/javascript" src="/views/front/js/chanyan.js"></script> <!-- - --> <div class="youdao-fixed-ad" id="detail_ad_bottom"></div> </div> <div class="col-md-3"> <div class="row" id="ad"> <!-- - 1 --> <div id="right-1" class="col-lg-12 col-md-12 col-sm-4 col-xs-4 ad"> <div class="youdao-fixed-ad" id="detail_ad_1"> </div> </div> <!-- - 2 --> <div id="right-2" class="col-lg-12 col-md-12 col-sm-4 col-xs-4 ad"> <div class="youdao-fixed-ad" id="detail_ad_2"></div> </div> <!-- - 3 --> <div id="right-3" class="col-lg-12 col-md-12 col-sm-4 col-xs-4 ad"> <div class="youdao-fixed-ad" id="detail_ad_3"></div> </div> </div> </div> </div> </div> </div> <div class="container"> <h4 class="pt20 mb15 mt0 border-top"> </h4> <div id="paradigm-article-related"> <div class="recommend-post mb30"> <ul class="widget-links"> <li><a href="/article/112.htm" title="HQL " target="_blank">HQL </a> <span class="text-muted"> </span> <a class="tag" taget="_blank" href="/search/HQL/1.htm">HQL</a><a class="tag" taget="_blank" href="/search/Hibernate/1.htm">Hibernate</a><a class="tag" taget="_blank" href="/search/%E6%9F%A5%E8%AF%A2%E8%AF%AD%E5%8F%A5/1.htm"> </a><a class="tag" taget="_blank" href="/search/%E6%8A%95%E5%BD%B1%E6%9F%A5%E8%AF%A2/1.htm"> </a> </li> <li><a href="/article/239.htm" title="Spring redis" target="_blank">Spring redis</a> <span class="text-muted">bylijinnan</span> <a class="tag" taget="_blank" href="/search/redis/1.htm">redis</a> </li> <li><a href="/article/366.htm" title="org.hibernate.NonUniqueResultException: query did not return a unique result: 2" target="_blank">org.hibernate.NonUniqueResultException: query did not return a unique result: 2</a> <span class="text-muted">0624chenhong</span> <a class="tag" taget="_blank" href="/search/Hibernate/1.htm">Hibernate</a> </li> <li><a href="/article/493.htm" title="android " target="_blank">android </a> <span class="text-muted"> </span> <a class="tag" taget="_blank" href="/search/android%E5%8A%A8%E7%94%BB/1.htm">android </a> </li> <li><a href="/article/620.htm" title="js delete " target="_blank">js delete </a> <span class="text-muted"> </span> <a class="tag" taget="_blank" href="/search/JavaScript/1.htm">JavaScript</a> </li> <li><a href="/article/747.htm" title="Oracle " target="_blank">Oracle </a> <span class="text-muted"> </span> <a class="tag" taget="_blank" href="/search/oracle/1.htm">oracle</a> </li> <li><a href="/article/874.htm" title="spring ——springmvc( )" target="_blank">spring ——springmvc( )</a> <span class="text-muted">a-john</span> <a class="tag" taget="_blank" href="/search/springMVC/1.htm">springMVC</a> </li> <li><a href="/article/1001.htm" title="POJ-2828-Buy Tickets" target="_blank">POJ-2828-Buy Tickets</a> <span class="text-muted">aijuans</span> <a class="tag" taget="_blank" href="/search/ACM_POJ/1.htm">ACM_POJ</a> </li> </ul> </div> </div> </div> <div> <div class="container"> <div class="indexes"> <strong> :</strong> <a href="/tags/A/1.htm" target="_blank">A</a><a href="/tags/B/1.htm" target="_blank">B</a><a href="/tags/C/1.htm" target="_blank">C</a><a href="/tags/D/1.htm" target="_blank">D</a><a href="/tags/E/1.htm" target="_blank">E</a><a href="/tags/F/1.htm" target="_blank">F</a><a href="/tags/G/1.htm" target="_blank">G</a><a href="/tags/H/1.htm" target="_blank">H</a><a href="/tags/I/1.htm" target="_blank">I</a><a href="/tags/J/1.htm" target="_blank">J</a><a href="/tags/K/1.htm" target="_blank">K</a><a href="/tags/L/1.htm" target="_blank">L</a><a href="/tags/M/1.htm" target="_blank">M</a><a href="/tags/N/1.htm" target="_blank">N</a><a href="/tags/O/1.htm" target="_blank">O</a><a href="/tags/P/1.htm" target="_blank">P</a><a href="/tags/Q/1.htm" target="_blank">Q</a><a href="/tags/R/1.htm" target="_blank">R</a><a href="/tags/S/1.htm" target="_blank">S</a><a href="/tags/T/1.htm" target="_blank">T</a><a href="/tags/U/1.htm" target="_blank">U</a><a href="/tags/V/1.htm" target="_blank">V</a><a href="/tags/W/1.htm" target="_blank">W</a><a href="/tags/X/1.htm" target="_blank">X</a><a href="/tags/Y/1.htm" target="_blank">Y</a><a href="/tags/Z/1.htm" target="_blank">Z</a><a href="/tags/0/1.htm" target="_blank"> </a> </div> </div> </div> <footer id="footer" class="mb30 mt30"> <div class="container"> <div class="footBglm"> <a target="_blank" href="/"> </a> - <a target="_blank" href="/custom/about.htm"> </a> - <a target="_blank" href="/search/Java/1.htm"> </a> - <a target="_blank" href="/sitemap.xml">Sitemap</a> - <a target="_blank" href="/custom/delete.htm"> </a> </div> <div class="copyright"> IT CopyRight © 2000-2050 IT IT610.com , All Rights Reserved. <a href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank"> ICP 09083238 </a><br> </div> </div> </footer> <!-- --> <script type="text/javascript" src="/static/syntaxhighlighter/scripts/shCore.js"></script> <script type="text/javascript" src="/static/syntaxhighlighter/scripts/shLegacy.js"></script> <script type="text/javascript" src="/static/syntaxhighlighter/scripts/shAutoloader.js"></script> <link type="text/css" rel="stylesheet" href="/static/syntaxhighlighter/styles/shCoreDefault.css"/> <script type="text/javascript" src="/static/syntaxhighlighter/src/my_start_1.js"></script> </body> </html></style></span></span></span></span></string></string></code></pre></div></div></div></div>

좋은 웹페이지 즐겨찾기