Java Code Examples for org.apache.batik.transcoder.TranscoderOutput

The following are top voted examples for showing how to use org.apache.batik.transcoder.TranscoderOutput. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: eurocarbdb   File: SvgFactory.java   View source code 7 votes vote down vote up
private byte[] createImage(ImageTranscoder t) throws Exception {
    //*** get the SVG document ***
    SVGDocument doc = this.getSvgDoc();
    org.w3c.dom.Element root = doc.getDocumentElement();
    this.getRoot(root);
    
    //*** set the transcoder input and output ***
    TranscoderInput input = new TranscoderInput(doc);
    ByteArrayOutputStream ostream = new ByteArrayOutputStream();
    TranscoderOutput output = new TranscoderOutput(ostream);

    //*** perform the transcoding ***
    t.transcode(input, output);
    ostream.flush();
    ostream.close();
    return ostream.toByteArray();
}
 
Example 2
Project: ARCLib   File: SvgConverter.java   View source code 6 votes vote down vote up
/**
 * Method for transforming SVG picture to PNG picture
 *
 * @param svgStream input stream of source SVG file
 * @param pngStream output stream of target PNG file
 * @param width     width of the target PNG file
 * @param height    height of the target PNG file
 */
public void convertSvgToPng(InputStream svgStream, OutputStream pngStream, Float width, Float height) {
    notNull(svgStream, IllegalArgumentException::new);
    notNull(pngStream, IllegalArgumentException::new);
    notNull(width, IllegalArgumentException::new);
    notNull(height, IllegalArgumentException::new);
    if (width <= 0 || height <= 0) {
        throw new IllegalArgumentException("Width and height muset be bigger than zero");
    }

    try {
        TranscoderInput input = new TranscoderInput(svgStream);
        TranscoderOutput output = new TranscoderOutput(pngStream);

        PNGTranscoder converter = new PNGTranscoder();
        converter.addTranscodingHint(PNGTranscoder.KEY_WIDTH, width);
        converter.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, height);

        converter.transcode(input, output);

    } catch (TranscoderException ex) {
        throw new SvgConverterException("Exception during transforming SVG to PNG", ex);
    }
}
 
Example 3
Project: etomica   File: GraphMap.java   View source code 6 votes vote down vote up
public void streamOut(OutputStream out) {

    // Stream out SVG to the standard output using UTF-8 encoding.
    try {
      TranscoderInput input = new TranscoderInput(svg);
      TranscoderOutput output = new TranscoderOutput(new BufferedWriter(new OutputStreamWriter(out, "UTF-8")));
      Transcoder t = new SVGTranscoder();
      t.transcode(input, output);
    }
    catch (TranscoderException e) {
      e.printStackTrace();
    }
    catch (UnsupportedEncodingException uee) {
      uee.printStackTrace();
    }
  }
 
Example 4
Project: Tarski   File: Main.java   View source code 6 votes vote down vote up
public static byte[] transcodeToSVG(Document doc) {
  try {
    // Determine output type:
    SVGTranscoder t = new SVGTranscoder();

    // Set transcoder input/output
    TranscoderInput input = new TranscoderInput(doc);
    ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
    OutputStreamWriter ostream = new OutputStreamWriter(bytestream);
    TranscoderOutput output = new TranscoderOutput(ostream);

    // Perform transcoding
    t.transcode(input, output);
    ostream.flush();
    ostream.close();

    return bytestream.toByteArray();

  } catch (IOException | TranscoderException e) {
    e.printStackTrace();
  }
  return null;
}
 
Example 5
Project: bdf2   File: DesignerServiceController.java   View source code 6 votes vote down vote up
private void saveImage(String svgData) throws Exception{
	PNGTranscoder coder=new PNGTranscoder();
	svgData="<?xml version=\"1.0\" encoding=\"utf-8\"?>"+svgData;
	//ByteArrayInputStream fin=new ByteArrayInputStream(svgData.getBytes());
	StringReader reader=new StringReader(svgData);
	TranscoderInput input=new TranscoderInput(reader);
	String tempDir=ContextHolder.getBdfTempFileStorePath()+DIR;
	File f=new File(tempDir);
	if(!f.exists())f.mkdirs();
	FileOutputStream fout=new FileOutputStream(tempDir+"/process.png");
	TranscoderOutput output=new TranscoderOutput(fout);
	try{
		coder.transcode(input, output);			
	}finally{
		reader.close();
		//fin.close();
		fout.close();			
	}
}
 
Example 6
Project: Push2Display   File: SVGTranscoder.java   View source code 6 votes vote down vote up
/**
 * Transcodes the specified input in the specified output.
 * @param input the input to transcode
 * @param output the ouput where to transcode
 * @exception TranscoderException if an error occured while transcoding
 */
public void transcode(TranscoderInput input, TranscoderOutput output)
    throws TranscoderException {
    Reader r = input.getReader();
    Writer w = output.getWriter();

    if (r == null) {
        Document d = input.getDocument();
        if (d == null) {
            throw new Error("Reader or Document expected");
        }
        StringWriter sw = new StringWriter( 1024 );
        try {
            DOMUtilities.writeDocument(d, sw);
        } catch ( IOException ioEx ) {
            throw new Error("IO:" + ioEx.getMessage() );
        }
        r = new StringReader(sw.toString());
    }
    if (w == null) {
        throw new Error("Writer expected");
    }
    prettyPrint(r, w);
}
 
Example 7
Project: Push2Display   File: BackgroundColorTest.java   View source code 6 votes vote down vote up
/**
 * Returns the reference image for this test.
 */
protected byte [] getReferenceImageData() {
    try {
        BufferedImage img = new BufferedImage
            (400, 400, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = img.createGraphics();
        g2d.setColor(Color.blue);
        g2d.fillRect(0, 0, 400, 400);
        g2d.setColor(Color.red);
        g2d.fillRect(100, 50, 100, 50);
        ByteArrayOutputStream ostream = new ByteArrayOutputStream();
        PNGTranscoder t = new PNGTranscoder();
        TranscoderOutput output = new TranscoderOutput(ostream);
        t.writeImage(img, output);
        return ostream.toByteArray();
    } catch (Exception ex) {
        throw new RuntimeException("BackgroundColorTest error");
    }
}
 
Example 8
Project: Push2Display   File: DOMTest.java   View source code 6 votes vote down vote up
/**
 * Returns the reference image for this test.
 */
protected byte [] getReferenceImageData() {
    try {
        BufferedImage img = new BufferedImage
            (400, 400, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = img.createGraphics();
        g2d.setColor(Color.black);
        g2d.fillRect(0, 0, 400, 400);
        g2d.setColor(Color.red);
        g2d.drawRect(100, 50, 100, 50);
        ByteArrayOutputStream ostream = new ByteArrayOutputStream();
        PNGTranscoder t = new PNGTranscoder();
        TranscoderOutput output = new TranscoderOutput(ostream);
        t.writeImage(img, output);
        return ostream.toByteArray();
    } catch (Exception ex) {
        throw new RuntimeException("DOMTest error");
    }
}
 
Example 9
Project: domui   File: PartUtil.java   View source code 6 votes vote down vote up
private static BufferedImage loadSvg(DomApplication da, IResourceDependencyList rdl, String image, IParameterInfo param) throws Exception {
	//-- 1. Get the input as a theme-replaced resource
	String svg = da.internalGetThemeManager().getThemeReplacedString(rdl, image);

	//-- 2. Now generate the thingy using the Batik transcoder:
	BufferedImageTranscoder bit = new BufferedImageTranscoder();
	TranscoderInput in = new TranscoderInput(new StringReader(svg));

	int w = PartUtil.getInt(param, "w", -1);
	int h = PartUtil.getInt(param, "h", -1);

	if(w != -1 && h != -1) {
		bit.addTranscodingHint(SVGAbstractTranscoder.KEY_WIDTH, Float.valueOf(w));
		bit.addTranscodingHint(SVGAbstractTranscoder.KEY_HEIGHT, Float.valueOf(h));
	}
	bit.transcode(in, new TranscoderOutput());
	return bit.getRendered();
}
 
Example 10
Project: domui   File: SvgPartFactory.java   View source code 6 votes vote down vote up
@Override
public void generate(@Nonnull PartResponse pr, @Nonnull DomApplication da, @Nonnull SvgKey k, @Nonnull IResourceDependencyList rdl) throws Exception {
	//-- 1. Get the input as a theme-replaced resource
	String svg = da.internalGetThemeManager().getThemeReplacedString(rdl, k.getRurl());

	//-- 2. Now generate the thingy using the Batik transcoder:
	PNGTranscoder coder = new PNGTranscoder();
	//		coder.addTranscodingHint(PNGTranscoder., null);
	TranscoderInput in = new TranscoderInput(new StringReader(svg));
	TranscoderOutput out = new TranscoderOutput(pr.getOutputStream());

	if(k.getWidth() != -1 && k.getHeight() != -1) {
		coder.addTranscodingHint(SVGAbstractTranscoder.KEY_WIDTH, Float.valueOf(k.getWidth()));
		coder.addTranscodingHint(SVGAbstractTranscoder.KEY_HEIGHT, Float.valueOf(k.getHeight()));
	}

	coder.transcode(in, out);

	if(!da.inDevelopmentMode()) { // Not gotten from WebContent or not in DEBUG mode? Then we may cache!
		pr.setCacheTime(da.getDefaultExpiryTime());
	}
	pr.setMime("image/png");
}
 
Example 11
Project: NBModeler   File: SvgImage.java   View source code 6 votes vote down vote up
public Image getImage(double width, double height) throws IOException {

        PNGTranscoder t = new PNGTranscoder();
        t.addTranscodingHint(PNGTranscoder.KEY_WIDTH, (float) width);
        t.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, (float) height);

        t.addTranscodingHint(PNGTranscoder.KEY_FORCE_TRANSPARENT_WHITE, true);

        TranscoderInput input = new TranscoderInput(svgDocument);
        ByteArrayOutputStream ostream = new ByteArrayOutputStream(1000);
        TranscoderOutput output2 = new TranscoderOutput(ostream);
        try {
            // Save the image.
            t.transcode(input, output2);
        } catch (TranscoderException ex) {
            Exceptions.printStackTrace(ex);
        }

        BufferedImage imag = ImageIO.read(new ByteArrayInputStream(ostream.toByteArray()));
        // ImageIO.write(imag, "png", new File(new Date().getTime()+".png"));
        ostream.flush();
        ostream.close();

        return imag;
    }
 
Example 12
Project: triquetrum   File: SimpleImageTranscoder.java   View source code 6 votes vote down vote up
private void updateImage() {
  if (document == null) {
    return;
  }
  try {
    if (canvasWidth >= 0) {
      addTranscodingHint(SVGAbstractTranscoder.KEY_WIDTH, new Float(canvasWidth));
    } else {
      removeTranscodingHint(SVGAbstractTranscoder.KEY_WIDTH);
    }
    if (canvasHeight >= 0) {
      addTranscodingHint(SVGAbstractTranscoder.KEY_HEIGHT, new Float(canvasHeight));
    } else {
      removeTranscodingHint(SVGAbstractTranscoder.KEY_HEIGHT);
    }
    if (canvasAOI != null) {
      addTranscodingHint(SVGAbstractTranscoder.KEY_AOI, canvasAOI);
    } else {
      removeTranscodingHint(SVGAbstractTranscoder.KEY_AOI);
    }
    transcode(new TranscoderInput(document), new TranscoderOutput());
  } catch (TranscoderException e) {
    TriqEditorPlugin.logError("Error transcoding SVG image", e);
  }
}
 
Example 13
Project: triquetrum   File: SimpleImageTranscoder.java   View source code 6 votes vote down vote up
@Override
protected void transcode(Document document, String uri, TranscoderOutput output) throws TranscoderException {
  super.transcode(document, uri, output);
  int w = (int) (width + 0.5);
  int h = (int) (height + 0.5);
  curTxf.translate(translateX, translateY);
  ImageRenderer renderer = createImageRenderer();
  renderer.updateOffScreen(w, h);
  renderer.setTransform(curTxf);
  renderer.setTree(this.root);
  this.root = null; // We're done with it...
  try {
    Shape raoi = new Rectangle2D.Float(0, 0, width, height);
    // Warning: the renderer's AOI must be in user space
    renderer.repaint(curTxf.createInverse().createTransformedShape(raoi));
    bufferedImage = renderer.getOffScreen();
  } catch (Exception ex) {
    throw new TranscoderException(ex);
  }
}
 
Example 14
Project: cm-cloud-generator   File: CloudGenerator.java   View source code 6 votes vote down vote up
private static void convertSvgToPng(String svg, String png, int origSize, int dstSize, Color bg) throws Exception {
    String svg_URI_input = Paths.get(svg).toUri().toURL().toString();
    TranscoderInput input_svg_image = new TranscoderInput(svg_URI_input);
    OutputStream png_ostream = new FileOutputStream(png);
    TranscoderOutput output_png_image = new TranscoderOutput(png_ostream);
    PNGTranscoder my_converter = new PNGTranscoder();
    my_converter.addTranscodingHint( PNGTranscoder.KEY_WIDTH, new Float( dstSize ) );
    my_converter.addTranscodingHint( PNGTranscoder.KEY_HEIGHT, new Float( dstSize ) );
    my_converter.addTranscodingHint( PNGTranscoder.KEY_AOI, new Rectangle( 0, 0, origSize, origSize) );
    if (bg != null) {
        my_converter.addTranscodingHint( PNGTranscoder.KEY_BACKGROUND_COLOR, bg);
    }
    my_converter.transcode(input_svg_image, output_png_image);
    png_ostream.flush();
    png_ostream.close();
}
 
Example 15
Project: Push2Display   File: SVGTranscoder.java   View source code 6 votes vote down vote up
/**
 * Transcodes the specified input in the specified output.
 * @param input the input to transcode
 * @param output the ouput where to transcode
 * @exception TranscoderException if an error occured while transcoding
 */
public void transcode(TranscoderInput input, TranscoderOutput output)
    throws TranscoderException {
    Reader r = input.getReader();
    Writer w = output.getWriter();

    if (r == null) {
        Document d = input.getDocument();
        if (d == null) {
            throw new Error("Reader or Document expected");
        }
        StringWriter sw = new StringWriter( 1024 );
        try {
            DOMUtilities.writeDocument(d, sw);
        } catch ( IOException ioEx ) {
            throw new Error("IO:" + ioEx.getMessage() );
        }
        r = new StringReader(sw.toString());
    }
    if (w == null) {
        throw new Error("Writer expected");
    }
    prettyPrint(r, w);
}
 
Example 16
Project: Push2Display   File: BackgroundColorTest.java   View source code 6 votes vote down vote up
/**
 * Returns the reference image for this test.
 */
protected byte [] getReferenceImageData() {
    try {
        BufferedImage img = new BufferedImage
            (400, 400, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = img.createGraphics();
        g2d.setColor(Color.blue);
        g2d.fillRect(0, 0, 400, 400);
        g2d.setColor(Color.red);
        g2d.fillRect(100, 50, 100, 50);
        ByteArrayOutputStream ostream = new ByteArrayOutputStream();
        PNGTranscoder t = new PNGTranscoder();
        TranscoderOutput output = new TranscoderOutput(ostream);
        t.writeImage(img, output);
        return ostream.toByteArray();
    } catch (Exception ex) {
        throw new RuntimeException("BackgroundColorTest error");
    }
}
 
Example 17
Project: Push2Display   File: DOMTest.java   View source code 6 votes vote down vote up
/**
 * Returns the reference image for this test.
 */
protected byte [] getReferenceImageData() {
    try {
        BufferedImage img = new BufferedImage
            (400, 400, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = img.createGraphics();
        g2d.setColor(Color.black);
        g2d.fillRect(0, 0, 400, 400);
        g2d.setColor(Color.red);
        g2d.drawRect(100, 50, 100, 50);
        ByteArrayOutputStream ostream = new ByteArrayOutputStream();
        PNGTranscoder t = new PNGTranscoder();
        TranscoderOutput output = new TranscoderOutput(ostream);
        t.writeImage(img, output);
        return ostream.toByteArray();
    } catch (Exception ex) {
        throw new RuntimeException("DOMTest error");
    }
}
 
Example 18
Project: PDFReporter-Studio   File: SimpleImageTranscoder.java   View source code 6 votes vote down vote up
private void updateImage() {
	if (document == null) {
		return;
	}
	try {
		if (canvasWidth >= 0) {
			addTranscodingHint(ImageTranscoder.KEY_WIDTH, new Float(canvasWidth));
		} else {
			removeTranscodingHint(ImageTranscoder.KEY_WIDTH);
		}
		if (canvasHeight >= 0) {
			addTranscodingHint(ImageTranscoder.KEY_HEIGHT, new Float(canvasHeight));
		} else {
			removeTranscodingHint(ImageTranscoder.KEY_HEIGHT);
		}
		if (canvasAOI != null) {
			addTranscodingHint(ImageTranscoder.KEY_AOI, canvasAOI);
		} else {
			removeTranscodingHint(ImageTranscoder.KEY_AOI);
		}
		transcode(new TranscoderInput(document), new TranscoderOutput());
	} catch (TranscoderException e) {
		JaspersoftStudioPlugin.getInstance().logError("Error transcoding SVG image", e);
	}
}
 
Example 19
Project: PDFReporter-Studio   File: SimpleImageTranscoder.java   View source code 6 votes vote down vote up
protected void transcode(Document document, String uri, TranscoderOutput output) throws TranscoderException {
	super.transcode(document, uri, output);
	int w = (int) (width + 0.5);
	int h = (int) (height + 0.5);
	ImageRenderer renderer = createImageRenderer();
	renderer.updateOffScreen(w, h);
	// curTxf.translate(0.5, 0.5);
	renderer.setTransform(curTxf);
	renderer.setTree(this.root);
	this.root = null; // We're done with it...
	try {
		Shape raoi = new Rectangle2D.Float(0, 0, width, height);
		// Warning: the renderer's AOI must be in user space
		renderer.repaint(curTxf.createInverse().createTransformedShape(raoi));
		bufferedImage = renderer.getOffScreen();
	} catch (Exception ex) {
		throw new TranscoderException(ex);
	}
}
 
Example 20
Project: donkirkby   File: PaintWriter.java   View source code 6 votes vote down vote up
private void convert(String svgFileName, String pngFileName)
{
	Transcoder transcoder = new PNGTranscoder();
       Map<TranscodingHints.Key, Float> hints = 
       	new HashMap<TranscodingHints.Key, Float>();
       hints.put(ImageTranscoder.KEY_MAX_HEIGHT, new Float(750));
       hints.put(ImageTranscoder.KEY_MAX_WIDTH, new Float(750));
       transcoder.setTranscodingHints(hints);
	
       try {
        TranscoderInput input = 
        	new TranscoderInput(new File(svgFileName).toURI().toString());
		TranscoderOutput output =
			new TranscoderOutput(new FileOutputStream(pngFileName));
	
		transcoder.transcode(input, output);
	} catch (Exception e) {
		throw new RuntimeException("Transcoding failed.", e);
	}
}
 
Example 21
Project: donkirkby   File: PaintWriter.java   View source code 6 votes vote down vote up
private void convert(String svgFileName, String pngFileName)
{
	Transcoder transcoder = new PNGTranscoder();
       Map<TranscodingHints.Key, Float> hints = 
       	new HashMap<TranscodingHints.Key, Float>();
       hints.put(ImageTranscoder.KEY_MAX_HEIGHT, new Float(200));
       hints.put(ImageTranscoder.KEY_MAX_WIDTH, new Float(750));
       transcoder.setTranscodingHints(hints);
	
       try {
        TranscoderInput input = 
        	new TranscoderInput(new File(svgFileName).toURI().toString());
		TranscoderOutput output =
			new TranscoderOutput(new FileOutputStream(pngFileName));
	
		transcoder.transcode(input, output);
	} catch (Exception e) {
		throw new RuntimeException("Transcoding failed.", e);
	}
}
 
Example 22
Project: WP3   File: Main.java   View source code 6 votes vote down vote up
public static byte[] transcodeToSVG(Document doc) {
  try {
    // Determine output type:
    SVGTranscoder t = new SVGTranscoder();

    // Set transcoder input/output
    TranscoderInput input = new TranscoderInput(doc);
    ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
    OutputStreamWriter ostream = new OutputStreamWriter(bytestream);
    TranscoderOutput output = new TranscoderOutput(ostream);

    // Perform transcoding
    t.transcode(input, output);
    ostream.flush();
    ostream.close();

    return bytestream.toByteArray();

  } catch (IOException | TranscoderException e) {
    e.printStackTrace();
  }
  return null;
}
 
Example 23
Project: swcv   File: DownloadCloudServlet.java   View source code 6 votes vote down vote up
private byte[] convertToPNG(WordCloud cloud) throws TranscoderException, IOException
{
    // Create a PNG transcoder
    PNGTranscoder t = new PNGTranscoder();

    // Set the transcoding hints
    t.addTranscodingHint(PNGTranscoder.KEY_WIDTH, new Float(cloud.getWidth() + 20));
    t.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, new Float(cloud.getHeight() + 20));

    // Create the transcoder input
    InputStream is = new ByteArrayInputStream(cloud.getSvg().getBytes());
    TranscoderInput input = new TranscoderInput(is);

    // Create the transcoder output
    ByteArrayOutputStream ostream = new ByteArrayOutputStream();
    TranscoderOutput output = new TranscoderOutput(ostream);

    // Save the image
    t.transcode(input, output);

    // Flush and close the stream
    ostream.flush();
    ostream.close();
    return ostream.toByteArray();
}
 
Example 24
Project: swcv   File: DownloadCloudServlet.java   View source code 6 votes vote down vote up
private byte[] convertToPDF(WordCloud cloud) throws TranscoderException, IOException
{
    // Create a JPEG transcoder
    PDFTranscoder t = new PDFTranscoder();

    // Set the transcoding hints
    t.addTranscodingHint(PDFTranscoder.KEY_WIDTH, new Float(cloud.getWidth() + 20));
    t.addTranscodingHint(PDFTranscoder.KEY_HEIGHT, new Float(cloud.getHeight() + 20));

    // Create the transcoder input
    InputStream is = new ByteArrayInputStream(cloud.getSvg().getBytes());
    TranscoderInput input = new TranscoderInput(is);

    // Create the transcoder output
    ByteArrayOutputStream ostream = new ByteArrayOutputStream();
    TranscoderOutput output = new TranscoderOutput(ostream);

    // Save the image
    t.transcode(input, output);

    // Flush and close the stream
    ostream.flush();
    ostream.close();
    return ostream.toByteArray();
}
 
Example 25
Project: birt   File: PDFRendererImpl.java   View source code 6 votes vote down vote up
/**
 * Transcode a SVG format Reader and write it to an output stream as a PDF image.
 *
 * @param dom  the svg document object model
 * @param ostream  the output stream
 *
 * @throws TranscoderException
 */
protected void transcode2PDF(Document dom, OutputStream ostream)
    throws TranscoderException
{
    // transcode the data
    TranscoderInput tcin = new TranscoderInput(dom);
    TranscoderOutput tcout = new TranscoderOutput(ostream);
    transcode2PDF(tcin, tcout);

    // flush the output stream
    try
    {
        ostream.flush();
    } catch (IOException ioe)
    {
        // ignore output stream flush error
    }
}
 
Example 26
Project: birt   File: PDFRendererImpl.java   View source code 6 votes vote down vote up
/**
 * Transcode a SVG format Reader and write it to an output stream as a PDF image.
 *
 * @param dom  the svg document object model
 * @param ostream  the output stream
 *
 * @throws TranscoderException
 */
protected void transcode2PDF(Reader r, OutputStream ostream)
    throws TranscoderException
{
    // transcode the data
    TranscoderInput tcin = new TranscoderInput(r);
    TranscoderOutput tcout = new TranscoderOutput(ostream);
    transcode2PDF(tcin, tcout);

    // flush the output stream
    try
    {
        ostream.flush();
    } catch (IOException ioe)
    {
        // ignore output stream flush error
    }
}
 
Example 27
Project: GEO-label-java   File: PngEncoder.java   View source code 6 votes vote down vote up
public InputStream encode(final InputStream in) throws IOException {
    if (in == null)
        throw new IOException("Input stream is null!");

    TranscoderInput input = new TranscoderInput(in);
    PNGTranscoder t = new PNGTranscoder();

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    TranscoderOutput output = new TranscoderOutput(outputStream);
    try {
        t.transcode(input, output);
    }
    catch (TranscoderException e) {
        throw new IOException(e);
    }

    outputStream.flush();

    return new ByteArrayInputStream(outputStream.toByteArray());
}
 
Example 28
Project: fop   File: Main.java   View source code 6 votes vote down vote up
/**
 * Runs the conversion
 * @param params command-line arguments
 */
public void convert(String[] params) {
    if (params.length != 2) {
        System.out.println("arguments: plan.xml output.svg");
        return;
    }
    try {
        InputStream is = new java.io.FileInputStream(params[0]);
        Document doc = createSVGDocument(is);
        SVGTranscoder svgT = new SVGTranscoder();
        TranscoderInput input = new TranscoderInput(doc);
        Writer ostream = new java.io.FileWriter(params[1]);
        TranscoderOutput output = new TranscoderOutput(ostream);
        svgT.transcode(input, output);
        ostream.flush();
        ostream.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 29
Project: projectforge-webapp   File: BatikImageRenderer.java   View source code 6 votes vote down vote up
private static byte[] getPDFByteArray(final Document document, final int width)
{
  // Create a pdf transcoder
  final PDFTranscoder t = new PDFTranscoder();
  t.addTranscodingHint(PDFTranscoder.KEY_AUTO_FONTS, false);
  t.addTranscodingHint(PDFTranscoder.KEY_WIDTH, new Float(width));
  TranscoderInput input = new TranscoderInput(document);
  final ByteArrayOutputStream baos = new ByteArrayOutputStream();
  final TranscoderOutput output = new TranscoderOutput(baos);
  // Save the image.
  try {
    t.transcode(input, output);
  } catch (TranscoderException ex) {
    log.fatal("Exception encountered " + ex, ex);
  }
  return baos.toByteArray();
}
 
Example 30
Project: orbeon-forms   File: SVGSerializer.java   View source code 6 votes vote down vote up
protected void readInput(final PipelineContext context, final ProcessorInput input, Config config, OutputStream outputStream) {
    try {

        //JPEGTranscoder t = new JPEGTranscoder();
        final ImageTranscoder imageTranscoder = new PNGTranscoder();
        imageTranscoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(0.8));
        //t.addTranscodingHint(JPEGTranscoder.KEY_WIDTH, new Integer(100));
        //trans.addTranscodingHint(JPEGTranscoder.KEY_AOI, aoi);
        //Document document = readInputAsDOM4J(context, input);
        //String documentString = XMLUtils.domToString(document);
        //TranscoderInput ti = new TranscoderInput(new StringReader(documentString));
        //TranscoderInput tixxx1 = new TranscoderInput(document);

        final TranscoderInput transcoderInput = new TranscoderInput(new XMLReaderToReceiver() {
            @Override
            public void parse(String systemId) throws SAXException {
                readInputAsSAX(context, (input != null) ? input : getInputByName(INPUT_DATA), createXMLReceiver());
            }
        });
        final TranscoderOutput transcoderOutput = new TranscoderOutput(outputStream);
        imageTranscoder.transcode(transcoderInput, transcoderOutput);
    } catch (Exception e) {
        throw new OXFException(e);
    }
}
 
Example 31
Project: Con-Badges   File: BadgePrinter.java   View source code 6 votes vote down vote up
private File generatePNG(BadgeInfo badgeInfo, SVGDocument doc, File outDir) throws TranscoderException, IOException {
    String fileName = getBadgeFilename(badgeInfo, ".png");
    PNGTranscoder t = new PNGTranscoder();
    setupTranscoder(t, doc);
    // Set the transcoder input and output.
    TranscoderInput input = new TranscoderInput(doc);
    File outFile = new File(outDir, fileName);
    LOGGER.info("Saving badge as " + outFile.getAbsolutePath());
    OutputStream outStream = new FileOutputStream(outFile);
    TranscoderOutput output = new TranscoderOutput(outStream);
    // Perform the transcoding.
    t.transcode(input, output);
    outStream.flush();
    outStream.close();
    t("save png");
    return outFile;
}
 
Example 32
Project: tephra   File: SvgParserImpl.java   View source code 5 votes vote down vote up
private ByteArrayOutputStream readSvg(String image) throws IOException, TranscoderException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Reader reader = new StringReader(image);
    new PNGTranscoder().transcode(new TranscoderInput(reader), new TranscoderOutput(outputStream));
    reader.close();
    outputStream.flush();
    outputStream.close();

    return outputStream;
}
 
Example 33
Project: Svg2Drawable   File: BatikConverter.java   View source code 5 votes vote down vote up
@Override
public void convert(final List<OutputPayload> outputPayloadList) throws Exception {
    // Create the transcoder input.

    for (final OutputPayload payload : outputPayloadList) {
        System.out.println(strings.getString("status_exporting_file", payload.getTargetFile()));

        final String svgPath = payload.getSvgPath();
        final String svgURI = new File(svgPath).toURL().toString();
        final TranscoderInput input = new TranscoderInput(svgURI);

        // Create a JPEG transcoder
        final PNGTranscoder t = new PNGTranscoder();

        final float floatWidth = Float.valueOf(payload.getWidth());
        final float floatHeight = Float.valueOf(payload.getHeight());

        t.addTranscodingHint(PNGTranscoder.KEY_WIDTH, floatWidth);
        t.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, floatHeight);

        // Create the transcoder output.
        final OutputStream ostream = new FileOutputStream(payload.getTargetFile());
        final TranscoderOutput output = new TranscoderOutput(ostream);
        // Save the image.
        t.transcode(input, output);

        // Flush and close the stream.
        ostream.flush();
        ostream.close();
    }
}
 
Example 34
Project: svg2png   File: Svg2Png.java   View source code 5 votes vote down vote up
private static List<File> convertFile(File input, OutputConfig cfg) throws IOException, TranscoderException, FileNotFoundException {
    TranscoderInput ti = new TranscoderInput(input.toURI().toString());
    PNGTranscoder t = new PNGTranscoder();
    List<File> generated = new ArrayList<>();

    StringBuilder info = new StringBuilder();
    for (FileOutput out : cfg.getFiles()) {
        info.setLength(0);
        info.append(input.getName());

        if (out.getWidth() > 0) {
            t.addTranscodingHint(PNGTranscoder.KEY_WIDTH, new Float(out.getWidth()));
            info.append(" w").append(out.getWidth());
        } else t.removeTranscodingHint(PNGTranscoder.KEY_WIDTH);

        if (out.getHeight() > 0) {
            t.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, new Float(out.getHeight()));
            info.append(" h").append(out.getHeight());
        } else t.removeTranscodingHint(PNGTranscoder.KEY_HEIGHT);
        
        File outputFile = out.toOutputFile(input, cfg.getOutputDirectory(), cfg.getOutputName());
        if (outputFile.exists()) {
            outputFile.delete();
        }
        outputFile.getParentFile().mkdirs();
        outputFile.createNewFile();
        try (FileOutputStream outStram = new FileOutputStream(outputFile)) {
            t.transcode(ti, new TranscoderOutput(outStram));
            generated.add(outputFile);
            info.append(" ").append(outputFile.getAbsolutePath());
        }
        System.out.println(info.toString());
    }
    return generated;
}
 
Example 35
Project: workcraft   File: EpsExporter.java   View source code 5 votes vote down vote up
@Override
public void export(Model model, OutputStream out) throws IOException, SerialisationException {
    InputStream svg = SvgExportUtils.stream(model);
    Transcoder transcoder = new EPSTranscoder();
    TranscoderInput transcoderInput = new TranscoderInput(svg);
    TranscoderOutput transcoderOutput = new TranscoderOutput(out);
    try {
        transcoder.transcode(transcoderInput, transcoderOutput);
    } catch (TranscoderException e) {
        throw new SerialisationException(e);
    }
}
 
Example 36
Project: workcraft   File: PsExporter.java   View source code 5 votes vote down vote up
@Override
public void export(Model model, OutputStream out) throws IOException, SerialisationException {
    InputStream svg = SvgExportUtils.stream(model);
    Transcoder transcoder = new PSTranscoder();
    TranscoderInput transcoderInput = new TranscoderInput(svg);
    TranscoderOutput transcoderOutput = new TranscoderOutput(out);
    try {
        transcoder.transcode(transcoderInput, transcoderOutput);
    } catch (TranscoderException e) {
        throw new SerialisationException(e);
    }
}
 
Example 37
Project: workcraft   File: PdfExporter.java   View source code 5 votes vote down vote up
@Override
public void export(Model model, OutputStream out) throws IOException, SerialisationException {
    InputStream svg = SvgExportUtils.stream(model);
    Transcoder transcoder = new PDFTranscoder();
    TranscoderInput transcoderInput = new TranscoderInput(svg);
    TranscoderOutput transcoderOutput = new TranscoderOutput(out);
    try {
        transcoder.transcode(transcoderInput, transcoderOutput);
    } catch (TranscoderException e) {
        throw new SerialisationException(e);
    }
}
 
Example 38
Project: workcraft   File: PngExporter.java   View source code 5 votes vote down vote up
@Override
public void export(Model model, OutputStream out) throws IOException, SerialisationException {
    InputStream svg = SvgExportUtils.stream(model);
    Transcoder transcoder = new PNGTranscoder();
    TranscoderInput transcoderInput = new TranscoderInput(svg);
    TranscoderOutput transcoderOutput = new TranscoderOutput(out);
    try {
        transcoder.transcode(transcoderInput, transcoderOutput);
    } catch (TranscoderException e) {
        throw new SerialisationException(e);
    }
}
 
Example 39
Project: Grimoire   File: SetCommand.java   View source code 5 votes vote down vote up
@Override
public void exec(String[] args, String rawArgs, MessageReceivedEvent e, GuildPreferences guildPreferences) {
	// Quit and error out if none provided
	if (args.length == 0) {
		sendErrorEmbed(e.getChannel(), "Please provide a set name.");
		return;
	}

	ScryfallSet set = Grimoire.getInstance().getCardProvider().getSetByNameOrCode(rawArgs);
	if (set == null) {
		sendErrorEmbedFormat(e.getChannel(), "I couldn't find any sets with **'%s'** as its name or code.", rawArgs);
		return;
	}

	EmbedBuilder eb = new EmbedBuilder(set.getEmbed());

	try {
		// Attempt sending with set symbol
		ByteArrayOutputStream resultByteStream = new ByteArrayOutputStream();
		TranscoderInput transcoderInput = new TranscoderInput(set.getIconSvgUri());
		TranscoderOutput transcoderOutput = new TranscoderOutput(resultByteStream);
		PNGTranscoder pngTranscoder = new PNGTranscoder();
		pngTranscoder.addTranscodingHint(SVGAbstractTranscoder.KEY_HEIGHT, 64f);
		pngTranscoder.addTranscodingHint(SVGAbstractTranscoder.KEY_WIDTH, 64f);
		pngTranscoder.transcode(transcoderInput, transcoderOutput);
		resultByteStream.flush();
		e.getChannel().sendFile(resultByteStream.toByteArray(), "set.png", new MessageBuilder().setEmbed(eb.build()).build()).submit();
	} catch (Exception ex) {
		// Fall back to no set symbol if needed
		e.getChannel().sendMessage(eb.build());
	}
}
 
Example 40
Project: Push2Display   File: PrintTranscoder.java   View source code 5 votes vote down vote up
/**
 * 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 {
    super.transcode(document, uri, output);

    // We do this to hide 'ctx' from the SVGAbstractTranscoder
    // otherwise it will dispose of the context before we can
    // print the document.
    theCtx = ctx;
    ctx = null;
}