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.9KB

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