Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

PNGRenderer_onthefly.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package org.apache.fop.render.bitmap;
  2. import java.awt.image.RenderedImage;
  3. import java.io.BufferedOutputStream;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.OutputStream;
  9. import org.apache.batik.ext.awt.image.codec.PNGEncodeParam;
  10. import org.apache.batik.ext.awt.image.codec.PNGImageEncoder;
  11. import org.apache.fop.apps.FOPException;
  12. import org.apache.fop.area.PageViewport;
  13. import org.apache.fop.render.java2d.Java2DRenderer;
  14. /**
  15. * PNG Renderer This class actually does not render itself, instead it extends
  16. * <code>org.apache.fop.render.java2D.Java2DRenderer</code> and just encode
  17. * rendering results into PNG format using Batik's image codec
  18. */
  19. public class PNGRenderer_onthefly extends Java2DRenderer {
  20. /** The MIME type for png-Rendering */
  21. public static final String MIME_TYPE = "image/png";
  22. /** The file syntax prefix, eg. "page" will output "page1.png" etc */
  23. private String fileSyntax;
  24. /** The output directory where images are to be written */
  25. private File outputDir;
  26. /** The PNGEncodeParam for the image */
  27. private PNGEncodeParam renderParams;
  28. /** The OutputStream for the first Image */
  29. private OutputStream firstOutputStream;
  30. /** default constructor */
  31. public PNGRenderer_onthefly() {}
  32. /** @see org.apache.fop.render.AbstractRenderer */
  33. public String getMimeType() {
  34. return MIME_TYPE;
  35. }
  36. public boolean supportsOutOfOrder() {
  37. return true;
  38. }
  39. /** @see org.apache.fop.render.Renderer#startRenderer(java.io.OutputStream) */
  40. public void startRenderer(OutputStream outputStream) throws IOException {
  41. log.info("rendering areas to PNG");
  42. setOutputDirectory();
  43. this.firstOutputStream = outputStream;
  44. }
  45. /**
  46. * Sets the output directory, either from the outfile specified on the
  47. * command line, or from the directory specified in configuration file. Also
  48. * sets the file name syntax, eg. "page"
  49. */
  50. private void setOutputDirectory() {
  51. // the file provided on the command line
  52. File f = getUserAgent().getOutputFile();
  53. outputDir = f.getParentFile();
  54. // extracting file name syntax
  55. String s = f.getName();
  56. int i = s.lastIndexOf(".");
  57. if (s.charAt(i - 1) == '1') {
  58. i--; // getting rid of the "1"
  59. }
  60. fileSyntax = s.substring(0, i);
  61. }
  62. public void renderPage(PageViewport pageViewport) throws IOException,
  63. FOPException {
  64. // Do the rendering: get the image for this page
  65. RenderedImage image = (RenderedImage) getPageImage(pageViewport);
  66. // Encode this image
  67. log.debug("Encoding page" + (getCurrentPageNumber() + 1));
  68. renderParams = PNGEncodeParam.getDefaultEncodeParam(image);
  69. OutputStream os = getCurrentOutputStream(getCurrentPageNumber());
  70. PNGImageEncoder encoder = new PNGImageEncoder(os, renderParams);
  71. encoder.encode(image);
  72. os.flush();
  73. setCurrentPageNumber(getCurrentPageNumber() + 1);
  74. }
  75. /**
  76. * Builds the OutputStream corresponding to this page
  77. * @param 0-based pageNumber
  78. * @return the corresponding OutputStream
  79. */
  80. private OutputStream getCurrentOutputStream(int pageNumber) {
  81. if (pageNumber == 0) {
  82. return firstOutputStream;
  83. }
  84. File f = new File(outputDir + File.separator + fileSyntax
  85. + (pageNumber + 1) + ".png");
  86. try {
  87. OutputStream os = new BufferedOutputStream(new FileOutputStream(f));
  88. return os;
  89. } catch (FileNotFoundException e) {
  90. new FOPException("Can't build the OutputStream\n" + e);
  91. return null;
  92. }
  93. }
  94. }