去评论
屠城辅助网

公式转透明图片以及base64源代码

Ysjt、凌何
2019/06/07 20:05:37
  1. import JAVA.awt.Color;
  2. import java.awt.Graphics2D;
  3. import java.awt.Insets;
  4. import java.awt.Transparency;
  5. import java.awt.image.BufferedImage;
  6. import java.io.ByteArrayOutputStream;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import javax.imageio.ImageIO;
  10. import javax.swing.JLabel;
  11. import org.scilab.forge.jlatexmath.TeXConstants;
  12. import org.scilab.forge.jlatexmath.TeXFormula;
  13. import org.scilab.forge.jlatexmath.TeXIcon;
  14. import sun.misc.BASE64Encoder;

  15. public class Latex2Png {

  16.     // latex 转 imgbase64
  17.     public static boolean latex2Png(String latex, String filePath) {
  18.         String suffix = filePath.substring(filePath.lastIndexOf(".") + 1);
  19.         BufferedImage bufferedImage = latex2BuImage(latex);
  20.         if (null == bufferedImage) {
  21.             return false;
  22.         }
  23.         try {
  24.             File file = new File(filePath);
  25.             File pFile = file.getParentFile();
  26.             if (!pFile.exists()) {
  27.                 pFile.mkdirs();
  28.             }
  29.             ImageIO.write(bufferedImage, suffix, file);
  30.             return true;
  31.         } catch (IOException e) {
  32.             e.printStackTrace();
  33.             return false;
  34.         }
  35.     }

  36.     // latex 转 imgbase64
  37.     public static String latex2Png(String latex) {
  38.         BufferedImage bufferedImage = latex2BuImage(latex);
  39.         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  40.         try {
  41.             ImageIO.write(bufferedImage, "png", outputStream);
  42.         } catch (IOException e) {
  43.             e.printStackTrace();
  44.             return null;
  45.         }
  46.         byte[] buffer = outputStream.toByteArray();
  47.         BASE64Encoder encoder = new BASE64Encoder();
  48.         return ("data:image/png;base64," + encoder.encode(buffer));
  49.     }

  50.     private static BufferedImage latex2BuImage(String drawStr) {
  51.         try {
  52.             TeXFormula formula = new TeXFormula(drawStr);
  53.             TeXIcon icon = formula
  54.                     .createTeXIcon(TeXConstants.STYLE_DISPLAY, 40);
  55.             icon.setInsets(new Insets(5, 5, 5, 5));

  56.             int width = icon.getIconWidth();
  57.             int height = icon.getIconHeight();

  58.             JLabel jl = new JLabel();
  59.             jl.setForeground(new Color(0, 0, 0));

  60.             BufferedImage buffImg = new BufferedImage(width, height,
  61.                     BufferedImage.TYPE_INT_RGB);
  62.             Graphics2D gd = buffImg.createGraphics();
  63.             buffImg = gd.getDeviceConfiguration().createCompatibleImage(width,
  64.                     height, Transparency.TRANSLUCENT);
  65.             gd = buffImg.createGraphics();
  66.             gd.setColor(Color.white);
  67.             icon.paintIcon(jl, gd, 0, 0);

  68.             return buffImg;
  69.         } catch (Exception e) {
  70.             System.err.println("公式解析有误:\n" + drawStr);
  71.             e.printStackTrace();
  72.             return null;
  73.         }
  74.     }

  75. }

写了个小Demo,将公式字符串例如“$$R_x=N_xP_xL_x$$”转为公式,公式见图“公式样式”。
用到的jar包jlatexmath主要用来将字符串转为公式,Graphics2D主要就是用来绘制图形。直接上代码。