【问题标题】:Required batik jars for loading SVG images加载 SVG 图像所需的蜡染罐
【发布时间】:2014-09-08 07:35:07
【问题描述】:

我使用 batik-transcoder 读取 SVG 文件并将它们转换为 BufferedImage。我的问题是,maven 下载了大约 19 个另外的 jar 来编译我的代码。我的用例需要哪些 jar?

maven下载的jar列表为:

[INFO] |  +- org.apache.xmlgraphics:batik-transcoder:jar:1.7:compile
[INFO] |  |  +- org.apache.xmlgraphics:fop:jar:0.94:compile
[INFO] |  |  |  +- org.apache.xmlgraphics:xmlgraphics-commons:jar:1.2:compile
[INFO] |  |  |  +- org.apache.avalon.framework:avalon-framework-api:jar:4.3.1:compile
[INFO] |  |  |  \- org.apache.avalon.framework:avalon-framework-impl:jar:4.3.1:compile
[INFO] |  |  +- org.apache.xmlgraphics:batik-awt-util:jar:1.7:compile
[INFO] |  |  +- org.apache.xmlgraphics:batik-bridge:jar:1.7:compile
[INFO] |  |  |  +- org.apache.xmlgraphics:batik-anim:jar:1.7:compile
[INFO] |  |  |  +- org.apache.xmlgraphics:batik-css:jar:1.7:compile
[INFO] |  |  |  +- org.apache.xmlgraphics:batik-ext:jar:1.7:compile
[INFO] |  |  |  +- org.apache.xmlgraphics:batik-parser:jar:1.7:compile
[INFO] |  |  |  +- org.apache.xmlgraphics:batik-script:jar:1.7:compile
[INFO] |  |  |  \- xalan:xalan:jar:2.6.0:compile
[INFO] |  |  +- org.apache.xmlgraphics:batik-dom:jar:1.7:compile
[INFO] |  |  +- org.apache.xmlgraphics:batik-gvt:jar:1.7:compile
[INFO] |  |  +- org.apache.xmlgraphics:batik-svg-dom:jar:1.7:compile
[INFO] |  |  +- org.apache.xmlgraphics:batik-svggen:jar:1.7:compile
[INFO] |  |  +- org.apache.xmlgraphics:batik-util:jar:1.7:compile
[INFO] |  |  +- org.apache.xmlgraphics:batik-xml:jar:1.7:compile
[INFO] |  |  \- xml-apis:xml-apis-ext:jar:1.3.04:compile

附:也欢迎提出有关其他 SVG 库的建议。我已经看过 SVGSalamander。它允许我想要的,但它看起来不像是一个设计良好且支持良好的库(恕我直言)。

【问题讨论】:

  • @RavipatiPraveen 这是具有相同代码的新link

标签: java swing maven svg batik


【解决方案1】:

如果使用 gradle,可以使用以下方式包含最小依赖项:

  implementation 'org.apache.xmlgraphics:batik-anim:1.13'
  implementation 'org.apache.xmlgraphics:batik-awt-util:1.13'
  implementation 'org.apache.xmlgraphics:batik-bridge:1.13'
  implementation 'org.apache.xmlgraphics:batik-css:1.13'
  implementation 'org.apache.xmlgraphics:batik-dom:1.13'
  implementation 'org.apache.xmlgraphics:batik-ext:1.13'
  implementation 'org.apache.xmlgraphics:batik-gvt:1.13'
  implementation 'org.apache.xmlgraphics:batik-parser:1.13'
  implementation 'org.apache.xmlgraphics:batik-script:1.13'
  implementation 'org.apache.xmlgraphics:batik-svg-dom:1.13'
  implementation 'org.apache.xmlgraphics:batik-svggen:1.13'
  implementation 'org.apache.xmlgraphics:batik-transcoder:1.13'
  implementation 'org.apache.xmlgraphics:batik-util:1.13'
  implementation 'org.apache.xmlgraphics:batik-xml:1.13'

JasperReports 5.0 向服务器建议deploying reports 时基本相同的集合。不同之处在于 batik-transcoder(相对较新?)不在他们的列表中(尽管我认为 JasperReports 的更高版本需要它):

batik-anim.jar
batik-awt-util.jar
batik-bridge.jar
batik-css.jar
batik-dom.jar
batik-ext.jar
batik-gvt.jar
batik-parser.jar
batik-script.jar
batik-svg-dom.jar
batik-svggen.jar
batik-util.jar
batik-xml.jar

获得一切:

  implementation 'org.apache.xmlgraphics:batik-all:1.13'

根据需要更改版本号。

注意:我只针对两个 SVG 插图对此进行了测试。两者都完美呈现。根据您的要求,可能存在一些不需要的依赖项(例如 batik-anim)。


SVG Salamander 是另一个 SVG 引擎。比蜡染更轻,但没有那么坚固。请注意,Batik 无法轻松处理文本 flowRoot 元素,并可能导致黑框。消除黑匣子需要删除所有flowRoot 元素,这可能需要一些时间。 (另一种可能性是对 SVG 文件执行 XSL 转换以修复 flowRoot 问题,但这种方法可能无法解决遇到的所有问题。)


这是一个示例 SVGRasterizer 类,它构建并使用上述蜡染库文件的子集:

public class SVGRasterizer {
  private final static SAXSVGDocumentFactory mFactory =
      new SAXSVGDocumentFactory( getXMLParserClassName() );

  private final static Map<Object, Object> RENDERING_HINTS = Map.of(
      KEY_ALPHA_INTERPOLATION,
      VALUE_ALPHA_INTERPOLATION_QUALITY,
      KEY_INTERPOLATION,
      VALUE_INTERPOLATION_BICUBIC,
      KEY_ANTIALIASING,
      VALUE_ANTIALIAS_ON,
      KEY_COLOR_RENDERING,
      VALUE_COLOR_RENDER_QUALITY,
      KEY_DITHERING,
      VALUE_DITHER_DISABLE,
      KEY_RENDERING,
      VALUE_RENDER_QUALITY,
      KEY_STROKE_CONTROL,
      VALUE_STROKE_PURE,
      KEY_FRACTIONALMETRICS,
      VALUE_FRACTIONALMETRICS_ON,
      KEY_TEXT_ANTIALIASING,
      VALUE_TEXT_ANTIALIAS_OFF
  );

  private static class BufferedImageTranscoder extends ImageTranscoder {
    private BufferedImage mImage;

    @Override
    public BufferedImage createImage( final int w, final int h ) {
      return new BufferedImage( w, h, BufferedImage.TYPE_INT_ARGB );
    }

    @Override
    public void writeImage(
        final BufferedImage image, final TranscoderOutput output ) {
      mImage = image;
    }

    public Image getImage() {
      return mImage;
    }

    @Override
    protected ImageRenderer createRenderer() {
      final ImageRenderer renderer = super.createRenderer();
      final RenderingHints hints = renderer.getRenderingHints();
      hints.putAll( RENDERING_HINTS );

      renderer.setRenderingHints( hints );

      return renderer;
    }
  }

  /**
   * Rasterizes the vector graphic file at the given URL. If any exception
   * happens, a red circle is returned instead.
   *
   * @param url   The URL to a vector graphic file, which must include the
   *              protocol scheme (such as file:// or https://).
   * @param width The number of pixels wide to render the image. The aspect
   *              ratio is maintained.
   * @return Either the rasterized image upon success or a red circle.
   */
  public static Image rasterize( final String url, final int width ) {
    try {
      return rasterize( new URL( url ), width );
    } catch( final Exception e ) {
      return createPlaceholderImage( width );
    }
  }

  /**
   * Converts an SVG drawing into a rasterized image that can be drawn on
   * a graphics context.
   *
   * @param url   The path to the image (can be web address).
   * @param width Scale the image width to this size (aspect ratio is
   *              maintained).
   * @return The vector graphic transcoded into a raster image format.
   * @throws IOException         Could not read the vector graphic.
   * @throws TranscoderException Could not convert the vector graphic to an
   *                             instance of {@link Image}.
   */
  public static Image rasterize( final URL url, final int width )
      throws IOException, TranscoderException {
    return rasterize(
        (SVGDocument) mFactory.createDocument( url.toString() ), width );
  }

  public static Image rasterize(
      final SVGDocument svg, final int width ) throws TranscoderException {
    final var transcoder = new BufferedImageTranscoder();
    final var input = new TranscoderInput( svg );

    transcoder.addTranscodingHint( KEY_BACKGROUND_COLOR, WHITE );
    transcoder.addTranscodingHint( KEY_WIDTH, (float) width );
    transcoder.transcode( input, null );

    return transcoder.getImage();
  }

  @SuppressWarnings("SuspiciousNameCombination")
  private static Image createPlaceholderImage( final int width ) {
    final var image = new BufferedImage( width, width, TYPE_INT_RGB );
    final var graphics = (Graphics2D) image.getGraphics();

    graphics.setColor( RED );
    graphics.setStroke( new BasicStroke( 5 ) );
    graphics.drawOval( 5, 5, width / 2, width / 2 );

    return image;
  }
}

【讨论】:

    猜你喜欢
    • 2014-08-16
    • 1970-01-01
    • 2015-12-19
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 2019-08-08
    相关资源
    最近更新 更多