package rxpe.svg;
import java.awt.Color;
import java.awt.Rectangle;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import javax.swing.JFrame;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
import org.apache.batik.anim.dom.SVGDOMImplementation;
import org.apache.batik.svggen.SVGGeneratorContext;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.svggen.SVGGraphics2DIOException;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.util.XMLResourceDescriptor;
import org.apache.commons.io.FileUtils;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.svg.SVGDocument;
/*
* java -jar batik-rasterizer.jar -m image/png test.svg
*/
public class Main {
public static void convert()
{
File f = new File("E:\\bnwork\\荣信\\SVG相关\\元件库\\svg.svg");
File destFile = new File("src/jxt.png");
SVGUtil svgUtil = new SVGUtil();
try {
svgUtil.convertSVGFile2Png(f, destFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void display( String uri) {
Document doc2=null;
if(uri==null || uri=="")uri = "http://tongxinmao.com/txm/svg.svg";
//SVG转DOM
try {
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
doc2 = f.createDocument(uri);
} catch (Exception ex) {
// ...
}
JSVGCanvas canvas = new JSVGCanvas();
JFrame f = new JFrame();
f.getContentPane().add(canvas);
//canvas.setSVGDocument(doc2);
canvas.setDocument(doc2);
f.pack();
f.setVisible(true);
}
public static void paint() throws UnsupportedEncodingException,
SVGGraphics2DIOException {
DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();
String svgURI = "http://www.w3.org/2000/svg";
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
// 创建SVG文件
SVGDocument doc = (SVGDocument) domImpl.createDocument(null, "svg", null);
//http://www.udel.edu/CIS/software/dist/batik-1.6/docs/svggen.html
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(doc);
ctx.setComment("Generated by TCM");
SVGGraphics2D svggener = new SVGGraphics2D(ctx, false);
//SVGGraphics2D svggener = new SVGGraphics2D(doc);
svggener.setPaint(Color.red);
// 画一个矩形
svggener.fill(new Rectangle(10, 10, 100, 100));
svggener.drawLine(0, 0, 100, 300);
boolean useCSS = false;
// 输出SVG文件
try {
FileOutputStream fo = new FileOutputStream("MyOut.svg");
Writer out = new OutputStreamWriter(fo, "UTF-8"); //System.out
svggener.stream(out, useCSS);
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//convert();
}
public static void dbg(String msg){
System.out.println(msg);
}
public static void loadVdrJson(){
File file = new File("vdr.json");
String JSONText="";
try {
JSONText = FileUtils.readFileToString(file);
} catch (IOException e) {
e.printStackTrace();
return;
}
try {
JSONObject json = new JSONObject(JSONText);
String filepath = json.getString("file");
dbg(filepath);
JSONArray components = json.getJSONArray("components");
JSONArray drawings = json.getJSONArray("drawings");
for(int i=0;i
JSONObject c = (JSONObject)components.getJSONObject(i);
//dbg(c.getString("script"));
}
for(int i=0;i
JSONObject d = (JSONObject)drawings.getJSONObject(i);
String name = (d.getString("name"));
if(!d.isNull("time")){
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
/**
* Document 转换为 String 并且进行了格式化缩进
*
* @param doc XML的Document对象
* @return String
*/
public static String doc2FormatString(Document doc) {
StringWriter stringWriter = null;
try {
stringWriter = new StringWriter();
if(doc != null){
OutputFormat format = new OutputFormat(doc,"UTF-8",true);
//format.setIndenting(true);//设置是否缩进,默认为true
//format.setIndent(4);//设置缩进字符数
//format.setPreserveSpace(false);//设置是否保持原来的格式,默认为 false
//format.setLineWidth(500);//设置行宽度
XMLSerializer serializer = new XMLSerializer(stringWriter,format);
serializer.asDOMSerializer();
serializer.serialize(doc);
return stringWriter.toString();
} else {
return null;
}
} catch (Exception e) {
return null;
} finally {
if(stringWriter != null){
try {
stringWriter.close();
} catch (IOException e) {
return null;
}
}
}
}
public static void dom()
{
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();
SVGDocument doc = (SVGDocument) domImpl.createDocument(svgNS, "svg", null);
Element svgRoot = doc.getDocumentElement();
svgRoot.setAttributeNS(null, "width", "400");
svgRoot.setAttributeNS(null, "height", "450");
Element rect = doc.createElementNS(svgNS, "rect");
rect.setAttributeNS(null, "x", "0");
rect.setAttributeNS(null, "y", "0");
rect.setAttributeNS(null, "width", "100");
rect.setAttributeNS(null, "height", "50");
rect.setAttributeNS(null, "style", "fill:blue");
svgRoot.appendChild(rect);
Element circle = doc.createElementNS(svgNS, "circle");
circle.setAttributeNS(null, "cx", "225");
circle.setAttributeNS(null, "cy", "250");
circle.setAttributeNS(null, "r", "100");
circle.setAttributeNS(null, "style", "fill:green;fill-opacity:.5");
svgRoot.appendChild(circle);
dbg( doc2FormatString(doc));
JSVGCanvas canvas = new JSVGCanvas();
JFrame f = new JFrame();
f.getContentPane().add(canvas);
canvas.setDocument(doc);
f.pack();
f.setVisible(true);
}
public static void main(String[] args)
{
//loadVdrJson();
dom();
}
}