1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| package com.ruben.utils;
import gui.ava.html.image.generator.HtmlImageGenerator;
import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.Arrays;
public class HtmlUtils {
public static void htmlSaveAsImage(String html, String targetPath) { HtmlImageGenerator imageGenerator = new HtmlImageGenerator(); imageGenerator.loadHtml(html); imageGenerator.saveAsImage(targetPath); }
public static byte[] htmlToImageByte(String html) { HtmlImageGenerator imageGenerator = new HtmlImageGenerator(); imageGenerator.loadHtml(html); BufferedImage image = imageGenerator.getBufferedImage(); byte[] bytes = null; try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { ImageIO.write(image, "png", os); bytes = os.toByteArray(); } catch (IOException e) { e.printStackTrace(); } return bytes; }
public static void main(String[] args) { htmlSaveAsImage("<h1 color='red'>ruben</h1>", "D:\\file\\files\\target\\ruben.png"); byte[] bytes = htmlToImageByte("<h1 color='red'>ruben</h1>"); } }
|