gpt4 book ai didi

java - 如何优化使用 Apache Batik 生成的 SVG 图像的权重?

转载 作者:行者123 更新时间:2023-12-02 04:06:36 26 4
gpt4 key购买 nike

我使用 Apache Batik 编写了以下代码,以便生成代表徽章的 SVG 图像 ( full source available on GitHub ),显示代码是否通过 SonarQube 质量门:

private SVGGraphics2D generateFor(final QualityGateStatus status) {
// new SVG graphics
final SVGGraphics2D svgGraphics2D = new SVGGraphics2D(this.svgGeneratorContext, false);
// set SVG canvas size
svgGraphics2D.setSVGCanvasSize(new Dimension(LABEL_WIDTH + status.displayWidth(), CANVAS_HEIGHT));
// set font
svgGraphics2D.setFont(FONT_FAMILY);
// draw Label background
svgGraphics2D.setColor(COLOR_BACKGROUND_LABEL);
svgGraphics2D.fillRoundRect(0, 0, LABEL_WIDTH, CANVAS_HEIGHT, BACKGROUND_CORNER_ARC_DIAMETER, BACKGROUND_CORNER_ARC_DIAMETER);
svgGraphics2D.fillRect(LABEL_WIDTH - BACKGROUND_CORNER_ARC_DIAMETER, 0, BACKGROUND_CORNER_ARC_DIAMETER, CANVAS_HEIGHT);
// draw Label text shadow
svgGraphics2D.setColor(COLOR_SHADOW);
svgGraphics2D.drawString(LABEL_TEXT, X_MARGIN, Y_OFFSET_SHADOW);
// draw Label text
svgGraphics2D.setColor(COLOR_TEXT);
svgGraphics2D.drawString(LABEL_TEXT, X_MARGIN, Y_OFFSET_TEXT);
// draw result background
svgGraphics2D.setColor(status.displayBackgroundColor());
svgGraphics2D.fillRoundRect(LABEL_WIDTH, 0, status.displayWidth(), CANVAS_HEIGHT, BACKGROUND_CORNER_ARC_DIAMETER, BACKGROUND_CORNER_ARC_DIAMETER);
svgGraphics2D.fillRect(LABEL_WIDTH, 0, BACKGROUND_CORNER_ARC_DIAMETER, CANVAS_HEIGHT);
// draw result text shadow
svgGraphics2D.setColor(COLOR_SHADOW);
svgGraphics2D.drawString(status.displayText(), LABEL_WIDTH + X_MARGIN, 15);
// draw result text
svgGraphics2D.setColor(COLOR_TEXT);
svgGraphics2D.drawString(status.displayText(), LABEL_WIDTH + X_MARGIN, 14);
return svgGraphics2D;
}

生成图像的示例(取决于传递给 generateFor() 方法的参数)可以在 GitHub as well 上看到。 .

这段代码工作正常,但目前生成的图像的重量大约为 2KB,几乎是重量大约为 700 字节的 travis-ci 徽章的 3 倍。因此我感觉生成的图像可以简化,大小可以减小到~1KB。

如何使用 Apache Batik 实现此目的?我应该使用另一个库或使用另一种方法吗?

预先感谢您的见解和建议!

最佳答案

我尝试使用 JFreeSVG 3.0 生成 SVG ,并使用 GZIP 选项创建大约 570 字节的文件。在纯文本中,SVG 大约为 1.7 KB。

这是我运行的测试程序(根据您的代码,您还需要 QualityGateStatus 类来运行它):

/*
* qualinsight-plugins-sonarqube-status
* Copyright (c) 2015, QualInsight
* http://www.qualinsight.com/
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, you can retrieve a copy
* from <http://www.gnu.org/licenses/>.
*/

import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.IOException;

import org.jfree.graphics2d.svg.SVGGraphics2D;
import org.jfree.graphics2d.svg.SVGUtils;

public final class SVGImageGenerator {

private static final int FONT_SIZE = 11;

private static final Font FONT_FAMILY = new Font("Verdana", Font.PLAIN, FONT_SIZE);

private static final int X_MARGIN = 4;

private static final int CANVAS_HEIGHT = 20;

private static final int LABEL_WIDTH = 46;

private static final String LABEL_TEXT = "quality";

private static final int BACKGROUND_CORNER_ARC_DIAMETER = 6;

private static final Color COLOR_BACKGROUND_LABEL = new Color(85, 85, 85, 255);

private static final Color COLOR_SHADOW = new Color(0, 0, 0, 85);

private static final Color COLOR_TEXT = new Color(255, 255, 255, 255);

private static final int Y_OFFSET_SHADOW = 14;

private static final int Y_OFFSET_TEXT = 14;


private static String generateFor(final QualityGateStatus status) {
// new SVG graphics
final SVGGraphics2D svgGraphics2D = new SVGGraphics2D(LABEL_WIDTH + status.displayWidth(), CANVAS_HEIGHT);
// set font
svgGraphics2D.setFont(FONT_FAMILY);
// draw Label background
svgGraphics2D.setColor(COLOR_BACKGROUND_LABEL);
svgGraphics2D.fillRoundRect(0, 0, LABEL_WIDTH, CANVAS_HEIGHT, BACKGROUND_CORNER_ARC_DIAMETER, BACKGROUND_CORNER_ARC_DIAMETER);
svgGraphics2D.fillRect(LABEL_WIDTH - BACKGROUND_CORNER_ARC_DIAMETER, 0, BACKGROUND_CORNER_ARC_DIAMETER, CANVAS_HEIGHT);
// draw Label text shadow
svgGraphics2D.setColor(COLOR_SHADOW);
svgGraphics2D.drawString(LABEL_TEXT, X_MARGIN, Y_OFFSET_SHADOW);
// draw Label text
svgGraphics2D.setColor(COLOR_TEXT);
svgGraphics2D.drawString(LABEL_TEXT, X_MARGIN, Y_OFFSET_TEXT);
// draw result background
svgGraphics2D.setColor(status.displayBackgroundColor());
svgGraphics2D.fillRoundRect(LABEL_WIDTH, 0, status.displayWidth(), CANVAS_HEIGHT, BACKGROUND_CORNER_ARC_DIAMETER, BACKGROUND_CORNER_ARC_DIAMETER);
svgGraphics2D.fillRect(LABEL_WIDTH, 0, BACKGROUND_CORNER_ARC_DIAMETER, CANVAS_HEIGHT);
// draw result text shadow
svgGraphics2D.setColor(COLOR_SHADOW);
svgGraphics2D.drawString(status.displayText(), LABEL_WIDTH + X_MARGIN, 15);
// draw result text
svgGraphics2D.setColor(COLOR_TEXT);
svgGraphics2D.drawString(status.displayText(), LABEL_WIDTH + X_MARGIN, 14);
return svgGraphics2D.getSVGElement();
}

public static void main(String[] args) {
try {
SVGUtils.writeToSVG(new File("error.svgz"), generateFor(QualityGateStatus.ERROR), true);
SVGUtils.writeToSVG(new File("none.svgz"), generateFor(QualityGateStatus.NONE), true);
SVGUtils.writeToSVG(new File("ok.svgz"), generateFor(QualityGateStatus.OK), true);
SVGUtils.writeToSVG(new File("server_error.svgz"), generateFor(QualityGateStatus.SERVER_ERROR), true);
SVGUtils.writeToSVG(new File("warn.svgz"), generateFor(QualityGateStatus.WARN), true);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

关于java - 如何优化使用 Apache Batik 生成的 SVG 图像的权重?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34226525/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com