You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BitmapProducerJava2D.java 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright 2005 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.visual;
  18. import java.awt.image.BufferedImage;
  19. import java.io.BufferedOutputStream;
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.OutputStream;
  23. import javax.xml.transform.Transformer;
  24. import javax.xml.transform.sax.SAXResult;
  25. import javax.xml.transform.stream.StreamSource;
  26. import org.apache.avalon.framework.configuration.Configurable;
  27. import org.apache.avalon.framework.configuration.Configuration;
  28. import org.apache.avalon.framework.configuration.ConfigurationException;
  29. import org.apache.commons.io.IOUtils;
  30. import org.apache.fop.apps.FOUserAgent;
  31. import org.apache.fop.apps.Fop;
  32. import org.apache.fop.apps.MimeConstants;
  33. import org.apache.fop.fo.Constants;
  34. /**
  35. * BitmapProducer implementation that uses the Java2DRenderer to create bitmaps.
  36. * <p>
  37. * Here's what the configuration element looks like for the class:
  38. * <p>
  39. * <pre>
  40. * <producer classname="org.apache.fop.visual.BitmapProducerJava2D">
  41. * <delete-temp-files>false</delete-temp-files>
  42. * </producer>
  43. * </pre>
  44. * <p>
  45. * The "delete-temp-files" element is optional and defaults to true.
  46. */
  47. public class BitmapProducerJava2D extends AbstractBitmapProducer implements Configurable {
  48. private boolean deleteTempFiles;
  49. /** @see org.apache.avalon.framework.configuration.Configurable */
  50. public void configure(Configuration cfg) throws ConfigurationException {
  51. this.deleteTempFiles = cfg.getChild("delete-temp-files").getValueAsBoolean(true);
  52. }
  53. /** @see org.apache.fop.visual.BitmapProducer */
  54. public BufferedImage produce(File src, ProducerContext context) {
  55. try {
  56. FOUserAgent userAgent = new FOUserAgent();
  57. userAgent.setResolution(context.getResolution());
  58. userAgent.setBaseURL(src.getParentFile().toURL().toString());
  59. File outputFile = new File(context.getTargetDir(), src.getName() + ".java2d.png");
  60. OutputStream out = new FileOutputStream(outputFile);
  61. out = new BufferedOutputStream(out);
  62. try {
  63. Fop fop = new Fop(MimeConstants.MIME_PNG, userAgent);
  64. fop.setOutputStream(out);
  65. SAXResult res = new SAXResult(fop.getDefaultHandler());
  66. Transformer transformer = getTransformer(context);
  67. transformer.transform(new StreamSource(src), res);
  68. } finally {
  69. IOUtils.closeQuietly(out);
  70. }
  71. BufferedImage img = BitmapComparator.getImage(outputFile);
  72. if (deleteTempFiles) {
  73. if (!outputFile.delete()) {
  74. log.warn("Cannot delete " + outputFile);
  75. outputFile.deleteOnExit();
  76. }
  77. }
  78. return img;
  79. } catch (Exception e) {
  80. e.printStackTrace();
  81. log.error(e);
  82. return null;
  83. }
  84. }
  85. }