@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(); }
transcoder.addTranscodingHint(PNGTranscoder.KEY_WIDTH, w); transcoder.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, h); transcoder.transcode(new TranscoderInput(in), new TranscoderOutput(out));
transcoder.transcode(input, output); final byte[] result = outStream.toByteArray(); repositoryService.addModelEditorSourceExtra(model.getId(), result);
/** * Transcodes the specified Document as an image in the specified output. * * @param document the document to transcode * @param uri the uri of the document or null if any * @param output the ouput where to transcode * @exception TranscoderException if an error occured while transcoding */ protected void transcode(Document document, String uri, TranscoderOutput output) throws TranscoderException { SVGRenderingAccuracyTest.this.manipulateSVGDocument(document); super.transcode(document, uri, output); } }
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 {
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; }
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(); } }
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();
private void transcodeAndTag(final Avatar avatar, final Avatar.Size size, final File transcoded) throws IOException { try (final OutputStream outputStream = new FileOutputStream(transcoded); final InputStream svgFileStream = AvatarManagerImpl.class.getResourceAsStream("/avatars/" + avatar.getFileName())) { final TranscoderInput transcoderInput = new TranscoderInput(svgFileStream); final TranscoderOutput transcoderOutput = new TranscoderOutput(outputStream); final PNGTranscoder transcoder = newPNGTranscoder(size); transcoder.transcode(transcoderInput, transcoderOutput); avatarTagger.tagSingleAvatarFile(transcoded, transcoded); } catch (TranscoderException e) { throw new RuntimeException(e); } }
/** * Converts given SVG data to PNG format. The default avatar size will be used which is * {@link Avatar.Size}.MEDIUM. * * @param inputStream to be converted * @param outputStream to which the result should be written * @throws IOException */ public void transcodeAndTag(final InputStream inputStream, final OutputStream outputStream) throws IOException { try { final TranscoderInput transcoderInput = new TranscoderInput(inputStream); final ByteArrayOutputStream transcoded = new ByteArrayOutputStream(); final TranscoderOutput transcoderOutput = new TranscoderOutput(transcoded); final PNGTranscoder transcoder = newPNGTranscoder(Avatar.Size.MEDIUM); transcoder.transcode(transcoderInput, transcoderOutput); avatarTagger.tag(new ByteArrayInputStream(transcoded.toByteArray()), outputStream); } catch (TranscoderException e) { throw new RuntimeException(e); } }
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()); } }
@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(); }
outputFile.createNewFile(); try (FileOutputStream outStram = new FileOutputStream(outputFile)) { t.transcode(ti, new TranscoderOutput(outStram)); generated.add(outputFile); info.append(" ").append(outputFile.getAbsolutePath());
t.transcode( input, output ); out.flush(); in = new ByteArrayInputStream( out.toByteArray() );
@RequestMapping(value = "/model/{modelId}/save", method = RequestMethod.PUT) @ResponseStatus(value = HttpStatus.OK) public void saveModel(@PathVariable String modelId , String name, String description , String json_xml, String svg_xml) { try { Model model = repositoryService.getModel(modelId); ObjectNode modelJson = (ObjectNode) objectMapper.readTree(model.getMetaInfo()); modelJson.put(MODEL_NAME, name); modelJson.put(MODEL_DESCRIPTION, description); model.setMetaInfo(modelJson.toString()); model.setName(name); repositoryService.saveModel(model); repositoryService.addModelEditorSource(model.getId(), json_xml.getBytes("utf-8")); InputStream svgStream = new ByteArrayInputStream(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(); } catch (Exception e) { LOGGER.error("Error saving model", e); throw new ActivitiException("Error saving model", e); } }
transcoder.transcode(input, output); final byte[] result = outStream.toByteArray(); repositoryService.addModelEditorSourceExtra(model.getId(), result);
transcoder.transcode(input, output);
t.transcode(in, out); return image[0]; } catch (TranscoderException e) {
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()); }