You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Mariusz Chmielewski <mc...@isi.wat.waw.pl> on 2006/01/16 14:33:08 UTC
Merging two SVG and sending them as an ImageIcon
I am using JDOM to parse the SVG files becouse when i use Batic i always
get an exception ... what am i doing wrong ?? th eproblem is that i
would like to cut whole content of svg tree from one file and insert it
in another:
using JDOM it goes like this:
private static ArrayList<Element> parseSVGElementSource(URL path)
throws IOException, JDOMException {
// wklejana jes cala sekcja <g>
ArrayList<Element> result = new ArrayList<Element>();
SAXBuilder builder = new SAXBuilder();
Document doc = (Document) builder.build(path);
Element root = doc.getRootElement();
// List groups = root.getChildren("g");
List groups = root.getChildren();
for (int i = 0; i < groups.size(); i++) {
Object temp = groups.get(i);
// wyciagam wszystkie elementy bezposrednio podpiete pod
hierarchie
// SVG
if (temp instanceof Element) {
Element elem = (Element) temp;
Content cont = elem.detach();
System.out.println(elem.getName());
// Element nElem = new Element(elem.getName());
// nElem.setContent(elem.getChildren());
result.add((Element) cont);
}
}
return result;
}
public static Document mergeSVGDocs(URL destinationPath, URL sourcePath)
throws IOException, JDOMException {
// wklejana jes cala sekcja <g>
ArrayList<Element> result = null;
SAXBuilder builder = new SAXBuilder();
Document doc = (Document) builder.build(destinationPath);
Element root = doc.getRootElement();
root.addContent(parseSVGElementSource(sourcePath));
return doc;
}
then i want to produce a ImageIcon from the merged Document and i always
get an error:
my method looks like this:
ArrayList<Element> result = null;
SAXBuilder builder = new SAXBuilder();
Document doc = (Document) builder.build(baseSign);
Element root = doc.getRootElement();
root.addContent(parseSVGElementSource(elementSign));
// canva.setDocument(new DOMOutputter().output(doc));
// JSVGCanvas canva = new JSVGCanvas();
// canva.setDocument(new DOMOutputter().output(doc));
//TranscoderOutput to = new TranscoderOutput(new DOMOutputter()
// .output(doc));
File nameDest = new File("c.jpeg");
//FileOutputStream fos = new FileOutputStream(nameDest);
ByteArrayOutputStream boa = new ByteArrayOutputStream();
TranscoderOutput to = new TranscoderOutput(boa);
String path = new File ("a.svg").toURL().toString();
TranscoderInput ti = new TranscoderInput(path);
//TranscoderInput ti = new TranscoderInput(new
DOMOutputter().output(doc));
JPEGTranscoder pngProducer = new JPEGTranscoder();
pngProducer.transcode(ti, to);
pngProducer.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,
new Float(.9));
pngProducer.addTranscodingHint(ImageTranscoder.KEY_BACKGROUND_COLOR,
Color.white);
BufferedImage img = pngProducer.createImage(100, 100);
//BufferedImage img = new
BufferedImage(100,100,BufferedImage.TYPE_BYTE_INDEXED);
pngProducer.writeImage(img, to);
img.flush();
//to.getOutputStream().flush();
return new ImageIcon(img);
}
its messy but i tried different thins to produce valid ImageIcon ... no
results Yet i always get black image
HELP
Mchmiel
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
Re: Merging two SVG and sending them as an ImageIcon
Posted by th...@kodak.com.
Hi Mariusz,
Mariusz Chmielewski <mc...@isi.wat.waw.pl> wrote on 01/16/2006 08:33:08
AM:
> I am using JDOM to parse the SVG files becouse when i use Batic i always
> get an exception ... what am i doing wrong ??
It would help to see the Batik version of the code and the error you get
since I know nothing about JDOM. My first guess is that you aren't
calling 'importNode' to import the elements from the one Document into
the first document.
> the problem is that i would like to cut whole content of svg tree from
> one file and insert it in another:
You might find that the 'image' element works better for you here,
rather than do all this work to munge the DOM tree's.
> then i want to produce a ImageIcon from the merged Document and i always
> get an error:
The code below looks rather hacked together, I would suggest reading:
http://xml.apache.org/batik/rasterizerTutorial.html
It has a nice simple example of converting an SVG to a JPEG.
I would suggest getting that working first then try adding your
code to 'merge' DOM tree's (however I think you are best off just
using the image element).
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org