MyTranscoder transcoder = new MyTransCoder(); TranscodingHints hints = new TranscodingHints(); hints.put(ImageTranscoder.KEY_WIDTH, width); hints.put(ImageTranscoder.KEY_HEIGHT, height); transcoder.setTranscodingHints(hints); transcoder.transcode(new TranscoderInput(url), null); BufferedImage image = transcoder.getImage();
@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(); }
private static Throwable unwrapException(TranscoderException ex) { // The TranscoderException is generally useless... return ex.getException() != null ? ex.getException() : ex; }
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import org.apache.batik.transcoder.Transcoder; import org.apache.batik.transcoder.TranscoderException; import org.apache.batik.transcoder.TranscoderInput; import org.apache.batik.transcoder.TranscoderOutput; import org.apache.fop.svg.PDFTranscoder; public class Test { public static void main(String[] argv) throws TranscoderException, FileNotFoundException { Transcoder transcoder = new PDFTranscoder(); TranscoderInput transcoderInput = new TranscoderInput(new FileInputStream(new File("/tmp/test.svg"))); TranscoderOutput transcoderOutput = new TranscoderOutput(new FileOutputStream(new File("/tmp/test.pdf"))); transcoder.transcode(transcoderInput, transcoderOutput); } }
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()))); } }
File cssFile = new File(getSvgPathToCss()); TranscodingHints transcoderHints = new TranscodingHints(); transcoderHints.put(ImageTranscoder.KEY_WIDTH, (float) resizeToWidth); //your image width transcoderHints.put(ImageTranscoder.KEY_HEIGHT, (float) resizeToHeight); //your image height transcoderHints.put(ImageTranscoder.KEY_XML_PARSER_VALIDATING, Boolean.FALSE); transcoderHints.put(ImageTranscoder.KEY_DOM_IMPLEMENTATION, SVGDOMImplementation.getDOMImplementation()); transcoderHints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT_NAMESPACE_URI, SVGConstants.SVG_NAMESPACE_URI); transcoderHints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT, "svg"); transcoderHints.put(ImageTranscoder.KEY_USER_STYLESHEET_URI, cssFile.toURI().toString()); TranscoderInput input = new TranscoderInput(new FileInputStream(svgFile)); ImageTranscoder t = new ImageTranscoder() { t.setTranscodingHints(transcoderHints); t.transcode(input, null); } catch (Exception e) { throw new IOException("Couldn't convert svg file: " + svgFile + " , reason: " + e.getMessage());
FileUtils.writeStringToFile(cssFile, css); TranscodingHints transcoderHints = new TranscodingHints(); transcoderHints.put(ImageTranscoder.KEY_XML_PARSER_VALIDATING, Boolean.FALSE); transcoderHints.put(ImageTranscoder.KEY_DOM_IMPLEMENTATION, SVGDOMImplementation.getDOMImplementation()); transcoderHints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT_NAMESPACE_URI, SVGConstants.SVG_NAMESPACE_URI); transcoderHints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT, "svg"); transcoderHints.put(ImageTranscoder.KEY_USER_STYLESHEET_URI, cssFile.toURI().toString()); TranscoderInput input = new TranscoderInput(new FileInputStream(svgFile));
@Override public void setInput(Object pInput, boolean seekForwardOnly, boolean ignoreMetadata) { super.setInput(pInput, seekForwardOnly, ignoreMetadata); if (imageInput != null) { TranscoderInput input = new TranscoderInput(IIOUtil.createStreamAdapter(imageInput)); rasterizer.setInput(input); } }
if (uri != transcoderInput.getURI()) { try { context.dispose(); document.setURLObject(new URL(transcoderInput.getURI())); transcode(document, transcoderInput.getURI(), null); throw new TranscoderException(ex); if (hints.containsKey(KEY_AOI)) { Rectangle2D aoi = (Rectangle2D) hints.get(KEY_AOI); throw new TranscoderException(ex); if (hints.containsKey(ImageTranscoder.KEY_BACKGROUND_COLOR)) { Paint bgcolor = (Paint) hints.get(ImageTranscoder.KEY_BACKGROUND_COLOR); g2d.setComposite(AlphaComposite.SrcOver); g2d.setPaint(bgcolor); TranscoderException exception = new TranscoderException(ex.getMessage()); exception.initCause(ex); throw exception;
DOMImplementation impl = (DOMImplementation) hints.get(KEY_DOM_IMPLEMENTATION); document = DOMUtilities.deepCloneDocument(document, impl); (hints.containsKey(KEY_EXECUTE_ONLOAD) && (Boolean) hints.get(KEY_EXECUTE_ONLOAD) && BaseScriptingEnvironment.isDynamicDocument(ctx, svgDoc)); exception = new TranscoderException(ex);
/** * Constructor. */ public ThumbnailTranscoder() { super(); hints.put(KEY_FORCE_TRANSPARENT_WHITE, Boolean.FALSE); }
/** * Creates a transcoder exception. */ protected TranscoderException fatalError(String key, Object[] params) throws TranscoderException { TranscoderException result = new TranscoderException(key); errorHandler.fatalError(result); return result; } }
public int getWidth(int pIndex) throws IOException { checkBounds(pIndex); try { return rasterizer.getDefaultWidth(); } catch (TranscoderException e) { throw new IIOException(e.getMessage(), e); } }
private synchronized void init() throws TranscoderException { if (!initialized) { if (transcoderInput == null) { throw new IllegalStateException("input == null"); } initialized = true; super.transcode(transcoderInput, null); } }
rasterizer.transcoderInput.setURI(baseURI);
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); transcoder.transcode(new TranscoderInput(in), new TranscoderOutput(out));
private TranscodingHints paramsToHints(SVGReadParam pParam) throws IOException { TranscodingHints hints = new TranscodingHints(); hints.put(ImageTranscoder.KEY_WIDTH, (float) size.getWidth()); hints.put(ImageTranscoder.KEY_HEIGHT, (float) size.getHeight()); hints.put(ImageTranscoder.KEY_AOI, region); hints.put(ImageTranscoder.KEY_WIDTH, (float) region.getWidth()); hints.put(ImageTranscoder.KEY_HEIGHT, (float) region.getHeight()); double yScale = size.getHeight() / origSize.getHeight(); hints.put(ImageTranscoder.KEY_WIDTH, (float) (region.getWidth() * xScale)); hints.put(ImageTranscoder.KEY_HEIGHT, (float) (region.getHeight() * yScale)); hints.put(ImageTranscoder.KEY_AOI, new Rectangle(origSize)); hints.put(ImageTranscoder.KEY_BACKGROUND_COLOR, bg);
/** * Creates a transcoder exception. */ protected TranscoderException fatalError(String key, Object[] params) throws TranscoderException { TranscoderException result = new TranscoderException(key); errorHandler.fatalError(result); return result; } }
public int getHeight(int pIndex) throws IOException { checkBounds(pIndex); try { return rasterizer.getDefaultHeight(); } catch (TranscoderException e) { throw new IIOException(e.getMessage(), e); } }
TranscoderInput input = new TranscoderInput(svgStream); PNGTranscoder transcoder = new PNGTranscoder(); TranscoderOutput output = new TranscoderOutput(outStream); transcoder.transcode(input, output); final byte[] result = outStream.toByteArray(); repositoryService.addModelEditorSourceExtra(model.getId(), result);