0

I am currently using SVGGraphics2D to create a SVG file. I was able to have a SVG file as an output by drawing shapes on it but then what I need is to have a bufferedimage - a PNG file, drawn in the SVG document. The following are the current codes I am working with.

Question: What should be the proper process to draw a bufferedimage in a SVG document?

Method to draw the image from source.

public void paintImage(Graphics g) throws IOException {
    File imageSrc = new File("C:\\Users\\anthony\\Downloads\\SVGGraphics2D\\src\\svggraphics\\eg.png");
    BufferedImage img = ImageIO.read(imageSrc);

    Graphics2D g2d = (Graphics2D) img.getGraphics();
    g2d.drawImage(img,0,0,null);
}

Creates SVG document.

 public static void main(String [] args) throws IOException {
    DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();

    String svgNS = "http://www.w3.org/2000/svg";
    Document document = domImpl.createDocument(svgNS, "svg", null);

    SVGGraphics2D graphics = new SVGGraphics2D(document);

    TestSVGGenerator test = new TestSVGGenerator();
    test.paintImage(graphics);

    boolean useCSS = true;
    Writer out = new OutputStreamWriter(new FileOutputStream("test.svg"), "UTF-8");
    graphics.stream(out, useCSS);
}
ACM
  • 11
  • 1
  • 5
  • I do not see any question. Btw. SVG is 2D vector format but have a capability to store 2D bitmaps but that is not effective way of storing images. – Spektre Sep 28 '16 at 07:33
  • Thank you. But is it possible to directly convert a PNG image to a SVG? Because I already tried converting PNG images to its Base64 to SVG but it still doesn't work. – ACM Sep 28 '16 at 09:46
  • I do not think SVG supports PNG chunks natively. You most likely have to convert your PNG into uncompressed image and store that. Do not know if the lib you are using supports such thing but you can still add the chunk programaticaly into SVG it is just XML see the SVG docs for how ... – Spektre Sep 28 '16 at 10:23
  • from quick search I found: [Does SVG support embedding of bitmap images?](http://stackoverflow.com/a/6250418/2521214) the linked SVG contains bitmap image ... – Spektre Sep 28 '16 at 10:34

1 Answers1

0

Firstly, you don't specify which library is providing the SVGGraphics2D class. That is important information you have omitted. So the following is a bit of a guess.

Secondly, you pass an instance of SVGGraphics2D to paintImage(), but don't use it. I suspect what you should have written is:

public void paintImage(Graphics g) throws IOException {
    File imageSrc = new File("C:\\Users\\anthony\\Downloads\\SVGGraphics2D\\src\\svggraphics\\eg.png");
    BufferedImage img = ImageIO.read(imageSrc);

    g.drawImage(img,0,0,null);
}

Try that.

Paul LeBeau
  • 97,474
  • 9
  • 154
  • 181