選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

PNGRenderer.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.apps.MimeConstants;
  13. import org.apache.fop.area.PageViewport;
  14. import org.apache.fop.render.java2d.Java2DRenderer;
  15. /**
  16. * PNG Renderer This class actually does not render itself, instead it extends
  17. * <code>org.apache.fop.render.java2D.Java2DRenderer</code> and just encode
  18. * rendering results into PNG format using Batik's image codec
  19. */
  20. public class PNGRenderer extends Java2DRenderer {
  21. /** The MIME type for png-Rendering */
  22. public static final String MIME_TYPE = MimeConstants.MIME_PNG;
  23. /** The file syntax prefix, eg. "page" will output "page1.png" etc */
  24. private String filePrefix;
  25. /** The output directory where images are to be written */
  26. private File outputDir;
  27. /** The PNGEncodeParam for the image */
  28. private PNGEncodeParam renderParams;
  29. /** The OutputStream for the first Image */
  30. private OutputStream firstOutputStream;
  31. /** @see org.apache.fop.render.AbstractRenderer */
  32. public String getMimeType() {
  33. return MIME_TYPE;
  34. }
  35. /** default constructor */
  36. public PNGRenderer() {}
  37. /** @see org.apache.fop.render.Renderer#startRenderer(java.io.OutputStream) */
  38. public void startRenderer(OutputStream outputStream) throws IOException {
  39. log.info("rendering areas to PNG");
  40. setOutputDirectory();
  41. this.firstOutputStream = outputStream;
  42. }
  43. /**
  44. * Sets the output directory, either from the outfile specified on the
  45. * command line, or from the directory specified in configuration file. Also
  46. * sets the file name syntax, eg. "page"
  47. */
  48. private void setOutputDirectory() {
  49. // the file provided on the command line
  50. File f = getUserAgent().getOutputFile();
  51. if (f == null) {
  52. //No filename information available. Only the first page will be rendered.
  53. outputDir = null;
  54. filePrefix = null;
  55. } else {
  56. outputDir = f.getParentFile();
  57. // extracting file name syntax
  58. String s = f.getName();
  59. int i = s.lastIndexOf(".");
  60. if (s.charAt(i - 1) == '1') {
  61. i--; // getting rid of the "1"
  62. }
  63. filePrefix = s.substring(0, i);
  64. }
  65. }
  66. public void stopRenderer() throws IOException {
  67. super.stopRenderer();
  68. for (int i = 0; i < pageViewportList.size(); i++) {
  69. OutputStream os = getCurrentOutputStream(i);
  70. if (os == null) {
  71. log.warn("No filename information available."
  72. + " Stopping early after the first page.");
  73. break;
  74. }
  75. // Do the rendering: get the image for this page
  76. RenderedImage image = (RenderedImage) getPageImage((PageViewport) pageViewportList
  77. .get(i));
  78. // Encode this image
  79. log.debug("Encoding page " + (i + 1));
  80. renderParams = PNGEncodeParam.getDefaultEncodeParam(image);
  81. // Set resolution
  82. float pixSzMM = userAgent.getPixelUnitToMillimeter();
  83. // num Pixs in 1 Meter
  84. int numPix = (int)((1000 / pixSzMM) + 0.5);
  85. renderParams.setPhysicalDimension(numPix, numPix, 1); // 1 means 'pix/meter'
  86. // Encode PNG image
  87. PNGImageEncoder encoder = new PNGImageEncoder(os, renderParams);
  88. encoder.encode(image);
  89. os.flush();
  90. }
  91. }
  92. /**
  93. * Builds the OutputStream corresponding to this page
  94. * @param 0-based pageNumber
  95. * @return the corresponding OutputStream
  96. */
  97. private OutputStream getCurrentOutputStream(int pageNumber) {
  98. if (pageNumber == 0) {
  99. return firstOutputStream;
  100. }
  101. if (filePrefix == null) {
  102. return null;
  103. } else {
  104. File f = new File(outputDir,
  105. filePrefix + (pageNumber + 1) + ".png");
  106. try {
  107. OutputStream os = new BufferedOutputStream(new FileOutputStream(f));
  108. return os;
  109. } catch (FileNotFoundException e) {
  110. new FOPException("Can't build the OutputStream\n" + e);
  111. return null;
  112. }
  113. }
  114. }
  115. }