1 17 18 19 20 package org.apache.fop.render.bitmap; 21 22 import java.awt.image.RenderedImage ; 23 import java.io.BufferedOutputStream ; 24 import java.io.File ; 25 import java.io.FileNotFoundException ; 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 import java.io.OutputStream ; 29 30 import org.apache.xmlgraphics.image.codec.png.PNGEncodeParam; 31 import org.apache.xmlgraphics.image.codec.png.PNGImageEncoder; 32 33 import org.apache.commons.io.IOUtils; 34 35 import org.apache.fop.apps.FOPException; 36 import org.apache.fop.area.PageViewport; 37 import org.apache.fop.render.java2d.Java2DRenderer; 38 39 44 public class PNGRenderer_onthefly extends Java2DRenderer { 45 46 47 public static final String MIME_TYPE = "image/png"; 48 49 50 private String fileSyntax; 51 52 53 private File outputDir; 54 55 56 private PNGEncodeParam renderParams; 57 58 59 private OutputStream firstOutputStream; 60 61 62 public String getMimeType() { 63 return MIME_TYPE; 64 } 65 66 67 public boolean supportsOutOfOrder() { 68 return true; 69 } 70 71 72 public void startRenderer(OutputStream outputStream) throws IOException { 73 log.info("rendering areas to PNG"); 74 setOutputDirectory(); 75 this.firstOutputStream = outputStream; 76 } 77 78 83 private void setOutputDirectory() { 84 85 File f = getUserAgent().getOutputFile(); 87 88 outputDir = f.getParentFile(); 89 90 String s = f.getName(); 92 int i = s.lastIndexOf("."); 93 if (s.charAt(i - 1) == '1') { 94 i--; } 96 fileSyntax = s.substring(0, i); 97 } 98 99 102 public void renderPage(PageViewport pageViewport) throws IOException { 103 104 RenderedImage image = (RenderedImage ) getPageImage(pageViewport); 106 107 log.debug("Encoding page" + (getCurrentPageNumber() + 1)); 109 renderParams = PNGEncodeParam.getDefaultEncodeParam(image); 110 OutputStream os = getCurrentOutputStream(getCurrentPageNumber()); 111 if (os != null) { 112 try { 113 PNGImageEncoder encoder = new PNGImageEncoder(os, renderParams); 114 encoder.encode(image); 115 } finally { 116 if (os != firstOutputStream) { 118 IOUtils.closeQuietly(os); 119 } 120 } 121 } 122 123 setCurrentPageNumber(getCurrentPageNumber() + 1); 124 } 125 126 131 private OutputStream getCurrentOutputStream(int pageNumber) { 132 133 if (pageNumber == 0) { 134 return firstOutputStream; 135 } 136 137 File f = new File (outputDir + File.separator + fileSyntax 138 + (pageNumber + 1) + ".png"); 139 try { 140 OutputStream os = new BufferedOutputStream (new FileOutputStream (f)); 141 return os; 142 } catch (FileNotFoundException e) { 143 new FOPException("Can't build the OutputStream\n" + e); 144 return null; 145 } 146 } 147 } 148 | Popular Tags |