public void update(URL svg) { g.setColor(new Color(0, 0, 0, 255)); g.setColor(new Color(20, 20, 20, 200)); g.setBackground(new Color(0, 0, 0, 0)); g.clearRect(0, 0, OSDParams.W, OSDParams.H); Transcoder t = new ImageTranscoder() { @Override public BufferedImage createImage(int arg0, int arg1) { return bi; } @Override public void writeImage(BufferedImage arg0, TranscoderOutput arg1) throws TranscoderException { // TODO Auto-generated method stub } }; try { TranscoderInput ti = new TranscoderInput(new FileReader(svg.getFile())); ti.setURI(svg.toURI().toString()); TranscoderOutput to = new TranscoderOutput(); t.transcode(ti, to); } catch (Exception e) { System.out.println("Failed to transcode"); e.printStackTrace(); throw new RuntimeException(e); } }
public void convert(InputStream in, OutputStream out, Integer size) throws TranscoderException { Transcoder t = createTranscoder(); if (size != null) { final float sizeBound = Math.max(Math.min(size, 2000.0f), 32.0f); t.addTranscodingHint(SVGAbstractTranscoder.KEY_WIDTH, sizeBound); } t.transcode(new TranscoderInput(in), new TranscoderOutput(out)); }
public static void setTranscoderStringHint( Transcoder transcoder, String property, TranscodingHints.Key key) { String str = System.getProperty(property); if (str != null) { transcoder.addTranscodingHint(key, str); } }
public static void setTranscoderRectangleHint( Transcoder transcoder, String property, TranscodingHints.Key key) { String str = System.getProperty(property); if (str != null) { StringTokenizer st = new StringTokenizer(str, " ,"); if (st.countTokens() != 4) { handleValueError(property, str); } try { String x = st.nextToken(); String y = st.nextToken(); String width = st.nextToken(); String height = st.nextToken(); Rectangle2D r = new Rectangle2D.Float( Float.parseFloat(x), Float.parseFloat(y), Float.parseFloat(width), Float.parseFloat(height)); transcoder.addTranscodingHint(key, r); } catch (NumberFormatException e) { handleValueError(property, str); } } }
public static void setTranscoderBooleanHint( Transcoder transcoder, String property, TranscodingHints.Key key) { String str = System.getProperty(property); if (str != null) { Boolean value = new Boolean("true".equalsIgnoreCase(str)); transcoder.addTranscodingHint(key, value); } }
@Override protected void execute(Search search, SearchKey key, SolrDocumentList documents) throws ActionException { Parameter param = em.find(Parameter.class, "SEARCH_REPO"); String repo = param.getValue(); File repoDir = new File( repo + File.separator + search.getConfig().getId() + File.separator + search.getId()); repoDir.mkdirs(); ImageRenderer imageRenderer = new ImageRenderer(); Transcoder transcoder = new PDFTranscoder(); for (SolrDocument doc : documents) { String docUrl = (String) doc.getFieldValue(URL.toString()); String docTitle = (String) doc.getFieldValue(TITLE.toString()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); File outputFile = new File(repoDir.getAbsolutePath() + File.separator + docTitle + ".pdf"); try { imageRenderer.renderURL(docUrl, outputStream, ImageRenderer.Type.SVG); } catch (IOException | SAXException e) { log.error("SVG rendering problem", e); createErrorFile(docUrl, outputFile, e); continue; } byte[] svgBytes = outputStream.toByteArray(); ByteArrayInputStream inputStream = new ByteArrayInputStream(svgBytes); TranscoderInput transcoderInput = new TranscoderInput(inputStream); TranscoderOutput transcoderOutput = null; try { transcoderOutput = new TranscoderOutput(new FileOutputStream(outputFile)); } catch (FileNotFoundException e) { log.warn("this shouldn't happen!", e); createErrorFile(docUrl, outputFile, e); continue; } try { transcoder.transcode(transcoderInput, transcoderOutput); } catch (TranscoderException e) { log.warn("Transcoding problem", e); createErrorFile(docUrl, outputFile, e); continue; } } }
public static void setTranscoderFloatHint( Transcoder transcoder, String property, TranscodingHints.Key key) { String str = System.getProperty(property); if (str != null) { try { Float value = new Float(Float.parseFloat(str)); transcoder.addTranscodingHint(key, value); } catch (NumberFormatException e) { handleValueError(property, str); } } }
/** * 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(); } } }
protected Transcoder createTranscoder() { Transcoder t = new JPEGTranscoder(); t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, 0.95f); return t; }