本文整理匯總了Java中org.apache.batik.transcoder.image.PNGTranscoder類的典型用法代碼示例。如果您正苦於以下問題:Java PNGTranscoder類的具體用法?Java PNGTranscoder怎麽用?Java PNGTranscoder使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
PNGTranscoder類屬於org.apache.batik.transcoder.image包,在下文中一共展示了PNGTranscoder類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: convertSvgToPng
import org.apache.batik.transcoder.image.PNGTranscoder; //導入依賴的package包/類
/**
* 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);
}
}
示例2: getTranscoder
import org.apache.batik.transcoder.image.PNGTranscoder; //導入依賴的package包/類
/**
* Returns a transcoder object of the result image type.
*
* @return Transcoder object or <tt>null</tt> 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;
}
}
示例3: saveImage
import org.apache.batik.transcoder.image.PNGTranscoder; //導入依賴的package包/類
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();
}
}
示例4: getTranscoder
import org.apache.batik.transcoder.image.PNGTranscoder; //導入依賴的package包/類
/**
* 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;
}
}
示例5: getTestImageTranscoder
import org.apache.batik.transcoder.image.PNGTranscoder; //導入依賴的package包/類
/**
* Returns the <code>ImageTranscoder</code> the Test should
* use
*/
public ImageTranscoder getTestImageTranscoder(){
ImageTranscoder t = new InternalPNGTranscoder();
t.addTranscodingHint(PNGTranscoder.KEY_FORCE_TRANSPARENT_WHITE,
Boolean.FALSE);
t.addTranscodingHint(PNGTranscoder.KEY_BACKGROUND_COLOR,
new Color(0,0,0,0));
t.addTranscodingHint(PNGTranscoder.KEY_EXECUTE_ONLOAD,
Boolean.TRUE);
if (validate){
t.addTranscodingHint(PNGTranscoder.KEY_XML_PARSER_VALIDATING,
Boolean.TRUE);
t.addTranscodingHint(PNGTranscoder.KEY_XML_PARSER_CLASSNAME,
VALIDATING_PARSER);
}
if (userLanguage != null){
t.addTranscodingHint(PNGTranscoder.KEY_LANGUAGE,
userLanguage);
}
return t;
}
示例6: generate
import org.apache.batik.transcoder.image.PNGTranscoder; //導入依賴的package包/類
@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");
}
示例7: getImage
import org.apache.batik.transcoder.image.PNGTranscoder; //導入依賴的package包/類
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;
}
示例8: convertSvgToPng
import org.apache.batik.transcoder.image.PNGTranscoder; //導入依賴的package包/類
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();
}
示例9: convert
import org.apache.batik.transcoder.image.PNGTranscoder; //導入依賴的package包/類
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);
}
}
示例10: convert
import org.apache.batik.transcoder.image.PNGTranscoder; //導入依賴的package包/類
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);
}
}
示例11: convertToPNG
import org.apache.batik.transcoder.image.PNGTranscoder; //導入依賴的package包/類
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();
}
示例12: encode
import org.apache.batik.transcoder.image.PNGTranscoder; //導入依賴的package包/類
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());
}
示例13: readInput
import org.apache.batik.transcoder.image.PNGTranscoder; //導入依賴的package包/類
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);
}
}
示例14: setupTranscoder
import org.apache.batik.transcoder.image.PNGTranscoder; //導入依賴的package包/類
private void setupTranscoder(ImageTranscoder t, SVGDocument doc) throws TranscoderException {
UserAgent userAgent = new UserAgentAdapter();
DocumentLoader loader = new DocumentLoader(userAgent);
BridgeContext ctx = new BridgeContext(userAgent, loader);
ctx.setDynamicState(BridgeContext.DYNAMIC);
GVTBuilder builder = new GVTBuilder();
GraphicsNode rootGN = builder.build(ctx, doc);
Rectangle2D bounds = rootGN.getBounds();
double pageWidth = settings.getPropertyDouble(PROPERTY_PAGE_WIDTH, DEFAULT_PAGE_WIDTH) * dpi;
double pageHeight = settings.getPropertyDouble(PROPERTY_PAGE_HEIGHT, DEFAULT_PAGE_HEIGHT) * dpi;
Point2D.Double scaledSize = getScaledSize(bounds.getWidth(), bounds.getHeight(), pageWidth, pageHeight);
System.out.println("Target size = " + pageWidth + "x" + pageHeight);
System.out.println("Scaled size = " + scaledSize.getX() + "x" + scaledSize.getY());
t.addTranscodingHint(PNGTranscoder.KEY_WIDTH, (float) scaledSize.getX());
t.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, (float)scaledSize.getY());
t.addTranscodingHint(PNGTranscoder.KEY_PIXEL_UNIT_TO_MILLIMETER, 25.4f / 300.0f);
}
示例15: generatePNG
import org.apache.batik.transcoder.image.PNGTranscoder; //導入依賴的package包/類
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;
}