Я немного продвинулся в этом вопросе. Но есть одна проблема. Только, думаю, помочь смогут те, кто имел дело с апачевской библиотекой batik.
В сервлете я по урлу получаю xml-документ, в котором прописан svg-код. Создаю из него sgv-документ и далее отдаю в другой метод, в котором и происходит конвертирование. В заголовке приходит JPEG, только картинка просто белая, а не такая, как SVG прописано. Всё делал примерно так, как в документации по batik'у и примерах, котороые в интернете нашёл. В чём может быть проблема?
Вот код их xml-файла, в котором svg-график.
| Код | <?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <title>Example quad01 - quadratic Bezier commands in path data</title> <desc>Picture showing a "Q" a "T" command, along with annotations showing the control points and end points</desc> <g transform="scale(0.5) translate(30,30)"> <rect x="1" y="1" width="1198" height="598" fill="none" stroke="blue" stroke-width="1"/> <path d="M200,300 Q400,50 600,300 T1000,300" fill="none" stroke="red" stroke-width="5"/> <!-- End points -->
<g fill="black"> <circle cx="200" cy="300" r="10"/> <circle cx="600" cy="300" r="10"/> <circle cx="1000" cy="300" r="10"/> </g> <!-- Control points and lines from end points to control points --> <g fill="#888888"> <circle cx="400" cy="50" r="10"/> <circle cx="800" cy="550" r="10"/>
</g> <path d="M200,300 L400,50 L600,300 L800,550 L1000,300" fill="none" stroke="#888888" stroke-width="2"/> </g> </svg>
|
Вот код сервлета:
| Код | public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try { String parser = XMLResourceDescriptor.getXMLParserClassName(); SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser); String uri = "http://localhost/image.xml"; Document doc = factory.createDocument(uri); SVGGraphics2D svgGraphicsEnvironment = new SVGGraphics2D(doc); ByteArrayOutputStream baos = new ByteArrayOutputStream(8192); Writer svgOutput = new OutputStreamWriter(baos, "UTF-8"); svgGraphicsEnvironment.stream(svgOutput, true); emitJPG(request, response, baos.toString()); } catch (Exception e) { e.getMessage(); e.printStackTrace(); } }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ doPost(request, response); } public void emitJPG(HttpServletRequest request, HttpServletResponse response, String svgString) throws IOException{ response.setContentType("image/jpeg");
JPEGTranscoder t = new JPEGTranscoder(); t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(.8)); TranscoderInput input = new TranscoderInput(new StringReader(svgString)); try { TranscoderOutput output = new TranscoderOutput(response.getOutputStream()); t.transcode(input, output); response.getOutputStream().close(); } catch (Exception e){ e.printStackTrace(); } }
|
|