Showing
1 changed file
with
23 additions
and
27 deletions
| @@ -77,61 +77,57 @@ public class LatexFormulaExcelExporterUtil { | @@ -77,61 +77,57 @@ public class LatexFormulaExcelExporterUtil { | ||
| 77 | } | 77 | } |
| 78 | 78 | ||
| 79 | /** | 79 | /** |
| 80 | - * 将LaTeX公式字符串渲染为图片字节数组 | ||
| 81 | - * | ||
| 82 | - * @param latex LaTeX公式字符串,如 "\\frac{-b \\pm \\sqrt{b^2-4ac}}{2a}" | ||
| 83 | - * @param fontSize 渲染的字体大小 | ||
| 84 | - * @return 图片的字节数组 (PNG格式) | ||
| 85 | - * @throws IOException 如果渲染或转换失败 | 80 | + * 针对Excel优化的高质量渲染 |
| 86 | */ | 81 | */ |
| 87 | public static byte[] latexToImageBytes(String latex, int fontSize) throws IOException { | 82 | public static byte[] latexToImageBytes(String latex, int fontSize) throws IOException { |
| 88 | try { | 83 | try { |
| 89 | - // 1. 创建TeXFormula对象,解析LaTeX字符串 | 84 | + // 对于Excel,使用适中的缩放和RGB格式 |
| 85 | + double scale = 1.5; // 1.5倍缩放平衡质量和文件大小 | ||
| 90 | TeXFormula formula = new TeXFormula(latex); | 86 | TeXFormula formula = new TeXFormula(latex); |
| 91 | - | ||
| 92 | - // 2. 创建TeXIcon,指定样式和大小 | ||
| 93 | TeXIcon icon = formula.createTeXIcon(TeXConstants.STYLE_DISPLAY, fontSize); | 87 | TeXIcon icon = formula.createTeXIcon(TeXConstants.STYLE_DISPLAY, fontSize); |
| 94 | 88 | ||
| 95 | - // 3. 创建一个BufferedImage,用于绘制公式 | 89 | + int baseWidth = icon.getIconWidth(); |
| 90 | + int baseHeight = icon.getIconHeight(); | ||
| 91 | + int scaledWidth = (int) (baseWidth * scale); | ||
| 92 | + int scaledHeight = (int) (baseHeight * scale); | ||
| 93 | + | ||
| 94 | + // 使用RGB类型,Excel兼容性更好 | ||
| 96 | BufferedImage image = new BufferedImage( | 95 | BufferedImage image = new BufferedImage( |
| 97 | - icon.getIconWidth(), | ||
| 98 | - icon.getIconHeight(), | ||
| 99 | - BufferedImage.TYPE_INT_ARGB | 96 | + scaledWidth, |
| 97 | + scaledHeight, | ||
| 98 | + BufferedImage.TYPE_INT_RGB | ||
| 100 | ); | 99 | ); |
| 101 | 100 | ||
| 102 | - // 4. 获取Graphics2D进行绘制 | ||
| 103 | Graphics2D g2 = image.createGraphics(); | 101 | Graphics2D g2 = image.createGraphics(); |
| 104 | 102 | ||
| 105 | - // 设置最高质量的渲染提示 | 103 | + // 针对公式渲染优化的设置 |
| 106 | g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); | 104 | g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
| 107 | - g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); | 105 | + g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); |
| 108 | g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); | 106 | g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); |
| 109 | g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); | 107 | g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); |
| 110 | - g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); | ||
| 111 | - g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); | ||
| 112 | - g2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); | ||
| 113 | - g2.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); | ||
| 114 | 108 | ||
| 115 | - // 设置背景为白色 | 109 | + // 绘制白色背景 |
| 116 | g2.setColor(Color.WHITE); | 110 | g2.setColor(Color.WHITE); |
| 117 | - g2.fillRect(0, 0, icon.getIconWidth(), icon.getIconHeight()); | 111 | + g2.fillRect(0, 0, scaledWidth, scaledHeight); |
| 112 | + | ||
| 113 | + // 应用缩放 | ||
| 114 | + g2.scale(scale, scale); | ||
| 118 | 115 | ||
| 119 | - // 设置公式颜色为黑色 | 116 | + // 绘制公式 |
| 120 | JLabel label = new JLabel(); | 117 | JLabel label = new JLabel(); |
| 121 | label.setForeground(Color.BLACK); | 118 | label.setForeground(Color.BLACK); |
| 122 | - | ||
| 123 | - // 5. 将公式绘制到BufferedImage上 | ||
| 124 | icon.paintIcon(label, g2, 0, 0); | 119 | icon.paintIcon(label, g2, 0, 0); |
| 120 | + | ||
| 125 | g2.dispose(); | 121 | g2.dispose(); |
| 126 | 122 | ||
| 127 | - // 6. 将BufferedImage转换为字节数组 | 123 | + // 使用标准PNG编码,确保Excel兼容性 |
| 128 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); | 124 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 129 | ImageIO.write(image, "png", baos); | 125 | ImageIO.write(image, "png", baos); |
| 130 | 126 | ||
| 131 | return baos.toByteArray(); | 127 | return baos.toByteArray(); |
| 132 | 128 | ||
| 133 | } catch (Exception e) { | 129 | } catch (Exception e) { |
| 134 | - throw new IOException("LaTeX渲染失败: " + e.getMessage(), e); | 130 | + throw new IOException("LaTeX Excel优化渲染失败: " + e.getMessage(), e); |
| 135 | } | 131 | } |
| 136 | } | 132 | } |
| 137 | 133 |