首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >加载SVG图像所需的batik jar

加载SVG图像所需的batik jar
EN

Stack Overflow用户
提问于 2014-07-17 20:19:38
回答 1查看 1.2K关注 0票数 3

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

maven下载的jars列表如下:

代码语言:javascript
复制
[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了。它允许我想要的东西,但它看起来不像是一个设计良好、支持良好的库(IMHO)。

EN

回答 1

Stack Overflow用户

发布于 2020-06-23 13:00:08

如果使用gradle,可以使用以下命令包含最小的依赖项:

代码语言:javascript
复制
  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'

deploying reports到服务器时,JasperReports 5.0建议使用本质上相同的集合。不同之处在于batik-代码转换器(相对较新?)不在他们的列表中(尽管我想以后的JasperReports版本需要它):

代码语言:javascript
复制
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

要获得所有内容:

代码语言:javascript
复制
  implementation 'org.apache.xmlgraphics:batik-all:1.13'

根据需要更改版本号。

注意:我只对两个SVG插图进行了测试。两者都渲染得天衣无缝。根据您的需求,可能有一些不是必需的依赖项(例如batik-anim)。

SVG Salamander是另一个SVG引擎。它比蜡染轻,不像蜡染那样结实。请注意,Batik不能轻松处理文本flowRoot元素,并且可能会产生黑盒。消除黑盒需要删除所有flowRoot元素,这可能需要一些时间。(另一种可能是对SVG文件执行XSL转换来修复flowRoot问题,但这种方法可能无法解决遇到的所有问题。)

下面是一个构建并使用上述Batik库文件子集的示例SVGRasterizer类:

代码语言:javascript
复制
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;
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24803392

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档