private synchronized void init() throws IOException { // Need the extra test, to avoid throwing an IOException from the Transcoder if (imageInput == null) { throw new IllegalStateException("input == null"); } if (reader == null) { WMFTranscoder transcoder = new WMFTranscoder(); ByteArrayOutputStream output = new ByteArrayOutputStream(); Writer writer = new OutputStreamWriter(output, "UTF8"); try { TranscoderInput in = new TranscoderInput(IIOUtil.createStreamAdapter(imageInput)); TranscoderOutput out = new TranscoderOutput(writer); // TODO: Transcodinghints? transcoder.transcode(in, out); } catch (TranscoderException e) { throw new IIOException(e.getMessage(), e); } reader = new SVGImageReader(getOriginatingProvider()); reader.setInput( ImageIO.createImageInputStream(new ByteArrayInputStream(output.toByteArray()))); } }
public TestReport encode(URL srcURL, FileOutputStream fos) { DefaultTestReport report = new DefaultTestReport(this); try { ImageTranscoder transcoder = getTestImageTranscoder(); TranscoderInput src = new TranscoderInput(svgURL.toString()); TranscoderOutput dst = new TranscoderOutput(fos); transcoder.transcode(src, dst); return null; } catch (TranscoderException e) { StringWriter trace = new StringWriter(); e.printStackTrace(new PrintWriter(trace)); report.setErrorCode(ERROR_CANNOT_TRANSCODE_SVG); report.setDescription( new TestReport.Entry[] { new TestReport.Entry( Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null), Messages.formatMessage( ERROR_CANNOT_TRANSCODE_SVG, new String[] { svgURL.toString(), e.getClass().getName(), e.getMessage(), trace.toString() })) }); } catch (Exception e) { StringWriter trace = new StringWriter(); e.printStackTrace(new PrintWriter(trace)); report.setErrorCode(ERROR_CANNOT_TRANSCODE_SVG); report.setDescription( new TestReport.Entry[] { new TestReport.Entry( Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null), Messages.formatMessage( ERROR_CANNOT_TRANSCODE_SVG, new String[] { svgURL.toString(), e.getClass().getName(), e.getMessage(), trace.toString() })) }); } report.setPassed(false); return report; }
/** * Show a "Save as" dialog. * * @param plot The plot to be exported. * @param width The width of the exported image (when export to JPEG/PNG). * @param height The height of the exported image (when export to JPEG/PNG). * @return Result from {@link JFileChooser#showSaveDialog} */ public static int showSaveDialog(SVGPlot plot, int width, int height) { double quality = 1.0; int ret = -1; JFileChooser fc = new JFileChooser(new File(".")); fc.setDialogTitle(DEFAULT_TITLE); // fc.setFileFilter(new ImageFilter()); SaveOptionsPanel optionsPanel = new SaveOptionsPanel(fc, width, height); fc.setAccessory(optionsPanel); ret = fc.showSaveDialog(null); fc.setDialogTitle("Saving... Please wait."); if (ret == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); String format = optionsPanel.getSelectedFormat(); if (format == null || automagic_format.equals(format)) { format = guessFormat(file.getName()); } try { if (format == null) { showError(fc, "Error saving image.", "File format not recognized."); } else if ("jpeg".equals(format) || "jpg".equals(format)) { quality = optionsPanel.getJPEGQuality(); plot.saveAsJPEG(file, width, height, quality); } else if ("png".equals(format)) { plot.saveAsPNG(file, width, height); } else if ("ps".equals(format)) { plot.saveAsPS(file); } else if ("eps".equals(format)) { plot.saveAsEPS(file); } else if ("pdf".equals(format)) { plot.saveAsPDF(file); } else if ("svg".equals(format)) { plot.saveAsSVG(file); } else { showError(fc, "Error saving image.", "Unsupported format: " + format); } } catch (java.lang.IncompatibleClassChangeError e) { showError( fc, "Error saving image.", "It seems that your Java version is incompatible with this version of Batik and Jpeg writing. Sorry."); } catch (ClassNotFoundException e) { showError( fc, "Error saving image.", "A class was not found when saving this image. Maybe installing Apache FOP will help (for PDF, PS and EPS output).\n" + e.toString()); } catch (IOException e) { LOG.exception(e); showError(fc, "Error saving image.", e.toString()); } catch (TranscoderException e) { LOG.exception(e); showError(fc, "Error saving image.", e.toString()); } catch (TransformerFactoryConfigurationError e) { LOG.exception(e); showError(fc, "Error saving image.", e.toString()); } catch (TransformerException e) { LOG.exception(e); showError(fc, "Error saving image.", e.toString()); } catch (Exception e) { LOG.exception(e); showError(fc, "Error saving image.", e.toString()); } } else if (ret == JFileChooser.ERROR_OPTION) { showError(fc, "Error in file dialog.", "Unknown Error."); } else if (ret == JFileChooser.CANCEL_OPTION) { // do nothing - except return result } return ret; }
/** * hightchart导出图片 * * @param request * @param response * @throws IOException */ @RequestMapping(params = "export") public void export(HttpServletRequest request, HttpServletResponse response) throws IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String type = request.getParameter("type"); String svg = request.getParameter("svg"); String filename = request.getParameter("filename"); filename = filename == null ? "chart" : filename; ServletOutputStream out = response.getOutputStream(); try { if (null != type && null != svg) { svg = svg.replaceAll(":rect", "rect"); String ext = ""; Transcoder t = null; if (type.equals("image/png")) { ext = "png"; t = new PNGTranscoder(); } else if (type.equals("image/jpeg")) { ext = "jpg"; t = new JPEGTranscoder(); } else if (type.equals("application/pdf")) { ext = "pdf"; t = (Transcoder) new PDFTranscoder(); } else if (type.equals("image/svg+xml")) ext = "svg"; response.addHeader( "Content-Disposition", "attachment; filename=" + new String(filename.getBytes("GBK"), "ISO-8859-1") + "." + ext); response.addHeader("Content-Type", type); if (null != t) { TranscoderInput input = new TranscoderInput(new StringReader(svg)); TranscoderOutput output = new TranscoderOutput(out); try { t.transcode(input, output); } catch (TranscoderException e) { out.print("Problem transcoding stream. See the web logs for more details."); e.printStackTrace(); } } else if (ext.equals("svg")) { // out.print(svg); OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8"); writer.append(svg); writer.close(); } else out.print("Invalid type: " + type); } else { response.addHeader("Content-Type", "text/html"); out.println( "Usage:\n\tParameter [svg]: The DOM Element to be converted." + "\n\tParameter [type]: The destination MIME type for the elment to be transcoded."); } } finally { if (out != null) { out.flush(); out.close(); } } }