Hi,

 

I try to convert an SVG file to a BufferedImage and to use that BufferedImage 
to create a GIF, JPG, etc... (I know there is a transcoder to do that...). The 
problem is that I always get a black background on all my images GIF and JPEG.

 

There is two possible sources for the problem :

1.       The conversion from SVG to the BufferedImage

2.       The conversion from the BufferedImage to the GIF, JPEG from the sun 
library javax.imageio.ImageIO;

 

I suspect there is a problem from the first option since I did  a test to 
convert a SVG to a BufferedImage and I used the later one to print on a AWT 
paint component: I saw the same black background.

 

The simplified code used to convert from SVG to the BufferedImage is the 
following 

 

      public BufferedImage getImage(SVGDocument svgDocument, int width, int 
height) {

            // Paint svg into image buffer

            //BufferedImage bufferedImage = new BufferedImage(width, height,

                  //    BufferedImage.TYPE_USHORT_565_RGB);

            BufferedImage bufferedImage = new BufferedImage(width, height,

                        BufferedImage.TYPE_3BYTE_BGR);

            Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();

 

            // For a smooth graphic with no jagged edges or rastorized look.

            

            

            g2d.setBackground(Color.WHITE);

            

            g2d.setRenderingHint(RenderingHints.KEY_RENDERING , 

                        RenderingHints.VALUE_RENDER_QUALITY);

            

            g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, 

                        RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);

            

            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

                        RenderingHints.VALUE_ANTIALIAS_OFF);

            

            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,

                        RenderingHints.VALUE_INTERPOLATION_BILINEAR);

 

            // Scale image to desired size

            Element elt = svgDocument.getRootElement();

            

            AffineTransform usr2dev = ViewBox.getViewTransform(null, elt, width,

                        height, null);

            g2d.transform(usr2dev);

            

 

 

            UserAgent      userAgent = new UserAgentAdapter();

            DocumentLoader loader = new DocumentLoader(userAgent);

            GVTBuilder     builder = new GVTBuilder();

            BridgeContext ctx  = new BridgeContext(userAgent, loader);

 

            ctx.setDynamicState(BridgeContext.DYNAMIC);

            GraphicsNode rootSvgNode = builder.build(ctx, svgDoc);

 

            

            rootSvgNode.paint(g2d);

 

            // Cleanup and return image

            g2d.dispose();

            return bufferedImage;

      }

 

Anyone has an idea on the problem??

 

Thanks,

 

Sébastien

Reply via email to