猿问

使用 SVGSalamander 将 SVG 转为图像无法正确渲染 SVG

我曾为 iText PDF 文档进行 SVG 图像渲染。为此,我使用 SVGSalamander 将 SVG 转换为图像格式。它工作正常,但有一个奇怪的行为,即某些 SVG 图像在某些图像正在渲染时无法正确渲染。那些错误渲染的 svg 与真实图像不对齐。我尝试过,但我不明白为什么它只发生在某些图像上。


如果有人帮助我解决这个问题,我真的很感激。


Java代码:


private static Image createSVGImage(PdfWriter pdfWriter, String imageEntry) throws BadElementException {

        Image image = null;


        Graphics2D g2dgraphics =null;

        PdfTemplate template = null;

        try{

            SVGDiagram diagram = SVGCache.getSVGUniverse().getDiagram( new java.io.File( imageEntry ).toURI() );

            template = pdfWriter.getDirectContent().createTemplate( diagram.getWidth(), diagram.getHeight());

            diagram.setIgnoringClipHeuristic(true);

             g2dgraphics= new PdfGraphics2D(template, diagram.getWidth(), diagram.getHeight());

        diagram.render(g2dgraphics);

    }catch( Exception e ){

        e.printStackTrace();

    } finally {

        if( g2dgraphics != null ){

            g2dgraphics.dispose();

          image = Image.getInstance(template);


        }

        g2dgraphics.dispose();

    }


    return image;

}

SVG xml 代码不对齐


<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">

  <path d="M19,16a46,46 0,1,0 62,0l-8,8a34,34 0,1,1-46,0z" fill="#069"/>

  <path d="M46,43v35a28,28 0,0,1-14-49zM54,43v35a28,28 0,0,0 14-49z" fill="#396"/>

  <circle r="15" cx="50" cy="18" fill="#900"/>

</svg>

真实图像

上面这段代码的输出图像

https://img1.sycdn.imooc.com/6531291a0001552a02400321.jpg


神不在的星期二
浏览 154回答 1
1回答

ibeautiful

我真的不知道为什么这个库会发生这种情况,因为没有答案,我已将 SVGSalamendar 更改为 Batik 库,如果有人需要,这是它的工作代码Maven 依赖项<dependency>&nbsp; &nbsp; <groupId>org.apache.xmlgraphics</groupId>&nbsp; &nbsp; <artifactId>batik-svggen</artifactId>&nbsp; &nbsp; <version>1.11</version></dependency><!-- https://mvnrepository.com/artifact/batik/batik-transcoder --><dependency>&nbsp;<groupId>org.apache.xmlgraphics</groupId>&nbsp; <artifactId>batik-transcoder</artifactId>&nbsp; &nbsp;<version>1.11</version></dependency><!-- https://mvnrepository.com/artifact/batik/batik-rasterizer --><dependency>&nbsp;<groupId>org.apache.xmlgraphics</groupId>&nbsp; <artifactId>batik-rasterizer</artifactId>&nbsp; <version>1.11</version></dependency>Java 代码反映与上面相同的结果:private static Image createSVGImage(PdfWriter pdfWriter, String imageEntry) throws BadElementException, IOException {&nbsp; &nbsp; &nbsp; &nbsp; Image image = null;&nbsp; &nbsp; &nbsp; &nbsp; final BufferedImage[] imagePointer = new BufferedImage[1];&nbsp; &nbsp; &nbsp; &nbsp; PdfContentByte pdfCB = new PdfContentByte(pdfWriter);&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TranscoderInput input = new TranscoderInput(new FileInputStream(imageEntry));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ImageTranscoder t = new ImageTranscoder() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public BufferedImage createImage(int w, int h) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void writeImage(BufferedImage img, TranscoderOutput output) throws TranscoderException {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO Auto-generated method stub&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;imagePointer[0] = img;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t.addTranscodingHint(ImageTranscoder.KEY_FORCE_TRANSPARENT_WHITE,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Boolean.FALSE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t.addTranscodingHint(ImageTranscoder.KEY_BACKGROUND_COLOR, Color.white);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t.transcode(input, null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (TranscoderException ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Requires Java 6&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ex.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IOException("Couldn't convert ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; image = Image.getInstance(pdfCB, imagePointer[0], 1);&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return image;&nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答