首页>代码>Highcharts:非常漂亮的图表 带各类示例~>/exporting-server/java/highcharts-export/src/main/java/com/highcharts/export/util/SVGRasterizer.java
package com.highcharts.export.util;

import java.io.ByteArrayOutputStream;
import java.io.StringReader;

import org.apache.batik.transcoder.SVGAbstractTranscoder;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.fop.svg.PDFTranscoder;

public class SVGRasterizer {

	private static final SVGRasterizer INSTANCE = new SVGRasterizer();

	public static final SVGRasterizer getInstance() {
		return INSTANCE;
	}

	private SVGRasterizer() {
	}

	public synchronized ByteArrayOutputStream transcode(
			ByteArrayOutputStream stream, String svg, MimeType mime, Float width)
			throws SVGRasterizerException, TranscoderException {

		TranscoderInput input = new TranscoderInput(new StringReader(svg));
		// Create the transcoder output
		TranscoderOutput transOutput = new TranscoderOutput(stream);
		// get right Transcoder, depends on mime
		SVGAbstractTranscoder transcoder = SVGRasterizer.getTranscoder(mime);
		if (width != null) {
			/*
			 * If the raster image height is not provided (using the
			 * KEY_HEIGHT), the transcoder will compute the raster image height
			 * by keeping the aspect ratio of the SVG document.
			 */
			transcoder.addTranscodingHint(SVGAbstractTranscoder.KEY_WIDTH,
					width);
		}
		transcoder.transcode(input, transOutput);

		return stream;

	}

	public static SVGAbstractTranscoder getTranscoder(MimeType mime)
			throws SVGRasterizerException {

		SVGAbstractTranscoder transcoder = null;

		switch (mime) {
		case PNG:
			transcoder = new PNGTranscoder();
			break;
		case JPEG:
			transcoder = new JPEGTranscoder();
			transcoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,
					new Float(0.9));
			break;
		case PDF:
			transcoder = new PDFTranscoder();
			break;
		default:
			// do nothing
			break;
		}
		
		if(transcoder == null){
			throw new SVGRasterizerException("MimeType not supported");
		}
		
		return transcoder;
	}
}
最近下载更多
wusiyin  LV14 2022年9月15日
zy198879  LV1 2021年7月6日
jckj315  LV1 2021年3月17日
incmanmeng  LV1 2020年9月14日
我就是一个小号  LV1 2020年7月4日
testtsf123  LV2 2020年3月3日
Nina is  LV1 2019年11月23日
诗经1234  LV1 2018年9月11日
7735wz  LV8 2018年8月28日
wanliai  LV13 2018年8月23日
最近浏览更多
zhengpeng8961  LV1 2023年11月16日
fesfefe  LV13 2023年10月28日
edpwyg  LV14 2023年10月22日
pojun_km  LV2 2022年11月30日
Tg171017  LV12 2022年11月30日
wusiyin  LV14 2022年9月15日
zhy1989wz  LV6 2022年4月20日
heifenglei  LV7 2022年2月17日
yanliang2377912054 2021年12月22日
暂无贡献等级
wannacheese  LV4 2021年11月30日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友