以文本方式查看主题 - 计算机科学论坛 (http://bbs.xml.org.cn/index.asp) -- 『 SVG/GML/VRML/X3D/XAML 』 (http://bbs.xml.org.cn/list.asp?boardid=21) ---- Server Side SVG (http://bbs.xml.org.cn/dispbbs.asp?boardid=21&rootid=&id=26589) |
-- 作者:卷积内核 -- 发布时间:1/19/2006 8:33:00 AM -- Server Side SVG If you've been using SVG or reading XML.com, you probably know about the Adobe SVG Viewer, and you may have heard of the Apache Batik project. Although Batik is most widely known for its SVG viewer component, it's more than that. Batik, according to its web site, is a “Java technology-based toolkit for applications that want to use images in the Scalable Vector Graphics (SVG) format for various purposes, such as viewing, generation or manipulation.” The Batik viewer application uses the JSVGCanvas component, which is a Java class that accepts SVG as input and displays it on screen. In this article, we'll use two of the other Batik components, SVGGraphics2D, and the Batik transcoders. The SVGGraphics2D class is the inverse of JSVGCanvas; you draw into an SVGGraphics2D environment using the standard Java two-dimensional graphics methods, and the result is an SVG document. The transcoders take an SVG document as input and produce either JPG or PNG as output. The context in which we'll use these tools is a servlet that generates geometric art in the style of Piet Mondrian. If the client supports SVG, the servlet will return an SVG document. Otherwise, it will return a JPEG or PNG image, depending upon client support for those image formats. Email article link Discuss Add to Project Server Side SVG <script></script> <body> <form id="artForm" <p> var hasSVGSupport = false; // does client have SVG support? /* <!-- <input type="hidden" name="imgType" value="jpg" />
|
-- 作者:卷积内核 -- 发布时间:1/19/2006 8:34:00 AM -- The Server Side Questions about this article? Share them with the author and other readers in our forum. Post your comments First, we need Java code to draw the actual painting into a Graphics2D context. You may see the Artist.java source. There's nothing particularly special about it; it's just a few draw and fill calls in a recursive function. The only thing to note is the constructor, which requires a width, height, and palette (which will be BRIGHT or PASTEL): public Artist( int width, int height, int colorType ); Now, on to the servlet. We'll do all the work in the doPost method. We start by sending out header information to keep the results from being cached: public class ArtMaker extends HttpServlet { public void doPost(HttpServletRequest request, Artist mondrian = null; if (agent.indexOf("explorer") > -1){ Artist mondrian = null; int width; if (picType.equals("landscape")) palette = (colorScheme.equals("bright")) ? mondrian = new Artist( width, height, palette ); response.getWriter().write( svgString ); |
-- 作者:卷积内核 -- 发布时间:1/19/2006 8:34:00 AM -- Transcoding To send back a JPEG or PNG file, we have to use Batik's transcoder. Input to the transcoder can be a document, a stream, or a Reader; output can be a URI, stream, or Writer. For the JPEG transcoder, you may set the output quality by calling the addTranscodingHint method. Since we're sending a series of bytes back to the client rather than a string, we're sending output to response.getOutputStream() rather than response.getWriter(). public void emitJPG( HttpServletRequest request, HttpServletResponse response, String svgString ) { response.setContentType("image/jpeg"); JPEGTranscoder t = new JPEGTranscoder(); TranscoderInput input = PNGTranscoder t = new PNGTranscoder(); TranscoderInput input = Thankyou for a great article on Server Side SVG. I have one question however. What if you wanted to alter any nodes in the SVG Document created when using SVGGraphics2D as a graphical context in Java2D ? What I am trying to say is, what if, when you have returned from creating the wanted graphics, you for instance would like to add a link to a square in the SVG image you have created. How would you go about doing that ? Regards Hi, I am trying to replicate this example and am having trouble compiling ArtMaker.java into a servlet. my command line looks like this: javac -sourcepath c:\xml-batik\sources -classpath c:\Progra~1\Allaire\JRun\lib\ext\servlet.jar;c:\Progra~1\Allaire\JRun\lib\ext\tools.jar;c:\Progra~1\Allaire\JRun\lib\ext\parser.jar;c:\xml-batik\sources;c:\Progra~1\Allaire\JRun\servers\default\default-app\WEB-INF\classes ArtMaker.java javac:\xml-batik\sources\org\apache\batik\transcoder\image\ImageTranscoder.java:413: any help would be greatly appreciated. |
W 3 C h i n a ( since 2003 ) 旗 下 站 点 苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》 |
67.383ms |