@PutMapping(value = "/{modelId}") @ResponseStatus(value = HttpStatus.OK) @Authorize(action = Permission.ACTION_UPDATE) public void saveModel(@PathVariable String modelId, @RequestParam Map<String, String> values) throws TranscoderException, IOException { Model model = repositoryService.getModel(modelId); JSONObject modelJson = JSON.parseObject(model.getMetaInfo()); modelJson.put(MODEL_NAME, values.get("name")); modelJson.put(MODEL_DESCRIPTION, values.get("description")); model.setMetaInfo(modelJson.toString()); model.setName(values.get("name")); repositoryService.saveModel(model); repositoryService.addModelEditorSource(model.getId(), values.get("json_xml").getBytes("utf-8")); InputStream svgStream = new ByteArrayInputStream(values.get("svg_xml").getBytes("utf-8")); TranscoderInput input = new TranscoderInput(svgStream); PNGTranscoder transcoder = new PNGTranscoder(); // Setup output ByteArrayOutputStream outStream = new ByteArrayOutputStream(); TranscoderOutput output = new TranscoderOutput(outStream); // Do the transformation transcoder.transcode(input, output); final byte[] result = outStream.toByteArray(); repositoryService.addModelEditorSourceExtra(model.getId(), result); outStream.close(); }
try (FileReader in = new FileReader(svgPath); OutputStream out = new FileOutputStream(pngPath)) { PNGTranscoder transcoder = new PNGTranscoder(); transcoder.addTranscodingHint(PNGTranscoder.KEY_WIDTH, w); transcoder.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, h);
TranscoderInput input = new TranscoderInput(svgStream); PNGTranscoder transcoder = new PNGTranscoder();
@VisibleForTesting PNGTranscoder createPngTranscoder() { return new PNGTranscoder(); }
private byte[] pngBytesFromSvg(String svg) throws TranscoderException { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final TranscoderInput input = new TranscoderInput(new StringReader(svg)); final TranscoderOutput output = new TranscoderOutput(outputStream); final PNGTranscoder converter = new PNGTranscoder(); converter.transcode(input, output); byte[] bytes = outputStream.toByteArray(); try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } return bytes; }
/** * Helper method: creates a valid reference image */ public static URL createValidReferenceImage(String svgContent) throws Exception{ TranscoderInput validSrc = new TranscoderInput(new StringReader(svgContent)); File tmpFile = File.createTempFile(SVGRenderingAccuracyTest.TEMP_FILE_PREFIX, SVGRenderingAccuracyTest.TEMP_FILE_SUFFIX); TranscoderOutput validDst = new TranscoderOutput(new FileOutputStream(tmpFile)); ImageTranscoder transcoder = new PNGTranscoder(); transcoder.transcode(validSrc, validDst); tmpFile.deleteOnExit(); return tmpFile.toURI().toURL(); }
public static byte[] convertToPngAsBytes(String path, Map<String, Object> paramMap) throws Exception { try { Document doc = SvgDocumentHelper.loadDocument(path, paramMap); PNGTranscoder t = new PNGTranscoder(); ByteArrayOutputStream output = saveAsBytes(t, doc); byte[] ans = output.toByteArray(); output.close(); // 加速内存回收 doc = null; return ans; } catch (Exception e) { log.error("render svg to png error! path:{}, data:{}, e: {}", path, paramMap, e); return null; } }
private void convertSvgToPng(File svg, File png) throws Exception { String svgUriInput = svg.toURI().toURL().toString(); TranscoderInput inputSvgImage = new TranscoderInput(svgUriInput); //Step-2: Define OutputStream to PNG Image and attach to TranscoderOutput OutputStream pngOStream = new FileOutputStream(png); TranscoderOutput outputPngImage = new TranscoderOutput(pngOStream); // Step-3: Create PNGTranscoder and define hints if required PNGTranscoder myConverter = new PNGTranscoder(); // Step-4: Convert and Write output myConverter.transcode(inputSvgImage, outputPngImage); // Step 5- close / flush Output Stream pngOStream.flush(); pngOStream.close(); } public String replaceSvgWithPng(File htmlFile) throws Exception {
/** * 将SVG转换成PNG * * @param path SVG文件路径 * @param paramMap 变更参数键值对,key为svg元素Id value为替换内容 * @throws TranscoderException * @throws IOException */ public static BufferedImage convertToPngAsImg(String path, Map<String, Object> paramMap) throws Exception { try { Document doc = SvgDocumentHelper.loadDocument(path, paramMap); PNGTranscoder t = new PNGTranscoder(); BufferedImage bf = saveAsImg(t, doc); // 加速内存回收 doc = null; return bf; } catch (Exception e) { log.error("render svg to png error! path:{}, data:{}, e: {}", path, paramMap, e); e.printStackTrace(); return null; } }
/** * Returns a transcoder object of the result image type. * * @return Transcoder object or <code>null</code> if there isn't a proper transcoder. */ protected Transcoder getTranscoder(){ switch(code) { case PNG_CODE: return new PNGTranscoder(); case JPEG_CODE: return new JPEGTranscoder(); case TIFF_CODE: return new TIFFTranscoder(); case PDF_CODE: try { Class pdfClass = Class.forName("org.apache.fop.svg.PDFTranscoder"); return (Transcoder)pdfClass.getDeclaredConstructor().newInstance(); } catch(Exception e) { return null; } default: return null; } }
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStream; import org.apache.batik.transcoder.TranscoderInput; import org.apache.batik.transcoder.TranscoderOutput; import org.apache.batik.transcoder.image.PNGTranscoder; public class Test { public static void main(String[] args) throws Exception { PNGTranscoder t = new PNGTranscoder(); t.addTranscodingHint(PNGTranscoder.KEY_WIDTH, new Float(12600)); t.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, new Float(12600)); FileInputStream fis = new FileInputStream("C:\\StackOverflow\\SVG-logo.svg"); TranscoderInput input = new TranscoderInput(fis); OutputStream ostream = new FileOutputStream("C:\\StackOverflow\\res.png"); TranscoderOutput output = new TranscoderOutput(ostream); System.out.println("AllocatedMemory: \t" + (Runtime.getRuntime().totalMemory() / 1024) + " Kb"); t.transcode(input, output); System.out.println("AllocatedMemory: \t" + (Runtime.getRuntime().totalMemory() / 1024) + " Kb"); ostream.flush(); ostream.close(); } }
/** * Transcode file to PNG. * * @param file Output filename * @param width Width * @param height Height * @throws IOException On write errors * @throws TranscoderException On input/parsing errors. */ public void saveAsPNG(File file, int width, int height) throws IOException, TranscoderException { PNGTranscoder t = new PNGTranscoder(); t.addTranscodingHint(PNGTranscoder.KEY_WIDTH, new Float(width)); t.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, new Float(height)); transcode(file, t); }
/** * Returns a transcoder object of the result image type. * * @return Transcoder object or <code>null</code> if there isn't a proper transcoder. */ protected Transcoder getTranscoder(){ switch(code) { case PNG_CODE: return new PNGTranscoder(); case JPEG_CODE: return new JPEGTranscoder(); case TIFF_CODE: return new TIFFTranscoder(); case PDF_CODE: try { Class pdfClass = Class.forName("org.apache.fop.svg.PDFTranscoder"); return (Transcoder)pdfClass.newInstance(); } catch(Exception e) { return null; } default: return null; } }
/** * Transcode file to PNG. * * @param file Output filename * @param width Width * @param height Height * @throws IOException On write errors * @throws TranscoderException On input/parsing errors. */ public void saveAsPNG(File file, int width, int height) throws IOException, TranscoderException { PNGTranscoder t = new PNGTranscoder(); t.addTranscodingHint(PNGTranscoder.KEY_WIDTH, new Float(width)); t.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, new Float(height)); transcode(file, t); }
BASE64Decoder decoder = new BASE64Decoder(); byte[] image = decoder.decodeBuffer(string); String fileLocation = "C:\temp"; String fileName = "New-" + System.currentTimeMillis(); File file = new File(fileLocation +File.separator+ fileName + ".svg"); FileOutputStream fop = new FileOutputStream(file); if (!file.exists()) { file.createNewFile(); } fop.write(image); fop.flush(); fop.close(); PNGTranscoder transcoder = new PNGTranscoder(); TranscoderInput tinput = new TranscoderInput(new FileInputStream(fileLocation + File.separator + fileName +".svg")); OutputStream ostream = new FileOutputStream(fileLocation + File.separator + fileName +".png"); TranscoderOutput toutput = new TranscoderOutput(ostream); try { transcoder.transcode(tinput, toutput); } catch (TranscoderException e) { System.out.println("error*"); e.printStackTrace(); } ostream.close();
/** * 将SVG输入流转换并写入到PNG输出流 * * @param reader SVG字符流 * @param output PNG输出流 * @throws TranscoderException 转换异常 */ public static void svg2png(Reader reader, OutputStream output) throws TranscoderException { if (reader == null) { throw new IllegalArgumentException("Reader must not be null"); } if (output == null) { throw new IllegalArgumentException("OutputStream must not be null"); } Transcoder transcoder = new PNGTranscoder(); transcoder.transcode(new TranscoderInput(reader), new TranscoderOutput(output)); }
/** * 将SVG输入流转换并写入到PNG输出流 * * @param input SVG输入流 * @param output PNG输出流 * @throws TranscoderException 转换异常 */ public static void svg2png(InputStream input, OutputStream output) throws TranscoderException { if (input == null) { throw new IllegalArgumentException("InputStream must not be null"); } if (output == null) { throw new IllegalArgumentException("OutputStream must not be null"); } Transcoder transcoder = new PNGTranscoder(); transcoder.transcode(new TranscoderInput(input), new TranscoderOutput(output)); }
private void rasterize(TranscoderInput input, TranscoderOutput output, int width, int height) { PNGTranscoder transcoder = new PNGTranscoder(); if (width > 0) { transcoder.addTranscodingHint(SVGAbstractTranscoder.KEY_WIDTH, new Float(width)); } if (height > 0) { transcoder.addTranscodingHint(SVGAbstractTranscoder.KEY_HEIGHT, new Float(height)); } // Set maximum width and height to 8k to avoid DoS attacks transcoder.addTranscodingHint(SVGAbstractTranscoder.KEY_MAX_WIDTH, new Float(8192)); transcoder.addTranscodingHint(SVGAbstractTranscoder.KEY_MAX_HEIGHT, new Float(8192)); try { transcoder.transcode(input, output); } catch (TranscoderException ex) { this.logger.warn("Failed to rasterize SVG image: {}", ex.getMessage()); } }
private void rasterize(TranscoderInput input, TranscoderOutput output, int width, int height) { PNGTranscoder transcoder = new PNGTranscoder(); if (width > 0) { transcoder.addTranscodingHint(SVGAbstractTranscoder.KEY_WIDTH, new Float(width)); } if (height > 0) { transcoder.addTranscodingHint(SVGAbstractTranscoder.KEY_HEIGHT, new Float(height)); } // Set maximum width and height to 8k to avoid DoS attacks transcoder.addTranscodingHint(SVGAbstractTranscoder.KEY_MAX_WIDTH, new Float(8192)); transcoder.addTranscodingHint(SVGAbstractTranscoder.KEY_MAX_HEIGHT, new Float(8192)); try { transcoder.transcode(input, output); } catch (TranscoderException ex) { this.logger.warn("Failed to rasterize SVG image: {}", ex.getMessage()); } }
private InputStream renderSVGToInputStream(RenderingContext context, ImageXMLDOM imageSVG) throws IOException { PNGTranscoder png = new PNGTranscoder(); Float width = getDimension(imageSVG.getDocument(), "width") * 8; png.addTranscodingHint(SVGAbstractTranscoder.KEY_WIDTH, width); Float height = getDimension(imageSVG.getDocument(), "height") * 8; png.addTranscodingHint(SVGAbstractTranscoder.KEY_HEIGHT, height); TranscoderInput input = new TranscoderInput(imageSVG.getDocument()); ByteArrayOutputStream os = new ByteArrayOutputStream(); TranscoderOutput output = new TranscoderOutput(os); try { png.transcode(input, output); } catch (TranscoderException ex) { SVGEventProducer eventProducer = SVGEventProducer.Provider.get( context.getUserAgent().getEventBroadcaster()); eventProducer.svgRenderingError(this, ex, imageSVG.getInfo().getOriginalURI()); } finally { os.flush(); os.close(); } return new ByteArrayInputStream(os.toByteArray()); }