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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 javax.xml.transform.Transformer;
  25. import javax.xml.transform.sax.SAXResult;
  26. import javax.xml.transform.stream.StreamSource;
  27. import org.apache.avalon.framework.configuration.Configurable;
  28. import org.apache.avalon.framework.configuration.Configuration;
  29. import org.apache.avalon.framework.configuration.ConfigurationException;
  30. import org.apache.commons.io.IOUtils;
  31. import org.apache.fop.apps.FOUserAgent;
  32. import org.apache.fop.apps.Fop;
  33. import org.apache.fop.apps.FopFactory;
  34. import org.apache.fop.apps.MimeConstants;
  35. /**
  36. * BitmapProducer implementation that uses the Java2DRenderer to create bitmaps.
  37. * <p>
  38. * Here's what the configuration element looks like for the class:
  39. * <p>
  40. * <pre>
  41. * <producer classname="org.apache.fop.visual.BitmapProducerJava2D">
  42. * <delete-temp-files>false</delete-temp-files>
  43. * </producer>
  44. * </pre>
  45. * <p>
  46. * The "delete-temp-files" element is optional and defaults to true.
  47. */
  48. public class BitmapProducerJava2D extends AbstractBitmapProducer implements Configurable {
  49. // configure fopFactory as desired
  50. private FopFactory fopFactory = FopFactory.newInstance();
  51. private boolean deleteTempFiles;
  52. /** @see org.apache.avalon.framework.configuration.Configurable */
  53. public void configure(Configuration cfg) throws ConfigurationException {
  54. this.deleteTempFiles = cfg.getChild("delete-temp-files").getValueAsBoolean(true);
  55. }
  56. /** @see org.apache.fop.visual.BitmapProducer */
  57. public BufferedImage produce(File src, int index, ProducerContext context) {
  58. try {
  59. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  60. userAgent.setTargetResolution(context.getTargetResolution());
  61. userAgent.setBaseURL(src.getParentFile().toURL().toString());
  62. File outputFile = new File(context.getTargetDir(),
  63. src.getName() + "." + index + ".java2d.png");
  64. OutputStream out = new FileOutputStream(outputFile);
  65. out = new BufferedOutputStream(out);
  66. try {
  67. Fop fop = fopFactory.newFop(MimeConstants.MIME_PNG, userAgent, out);
  68. SAXResult res = new SAXResult(fop.getDefaultHandler());
  69. Transformer transformer = getTransformer(context);
  70. transformer.transform(new StreamSource(src), res);
  71. } finally {
  72. IOUtils.closeQuietly(out);
  73. }
  74. BufferedImage img = BitmapComparator.getImage(outputFile);
  75. if (deleteTempFiles) {
  76. if (!outputFile.delete()) {
  77. log.warn("Cannot delete " + outputFile);
  78. outputFile.deleteOnExit();
  79. }
  80. }
  81. return img;
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. log.error(e);
  85. return null;
  86. }
  87. }
  88. }