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.

XSLFImageRenderer.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.xslf.usermodel;
  20. import java.awt.Graphics2D;
  21. import java.awt.Insets;
  22. import java.awt.Shape;
  23. import java.awt.geom.AffineTransform;
  24. import java.awt.geom.Rectangle2D;
  25. import java.awt.image.BufferedImage;
  26. import java.io.IOException;
  27. import javax.imageio.ImageIO;
  28. import org.apache.poi.openxml4j.opc.PackagePart;
  29. import org.apache.poi.util.Beta;
  30. /**
  31. * For now this class renders only images supported by the javax.imageio.ImageIO
  32. * framework. Subclasses can override this class to support other formats, for
  33. * example, Use Apache batik to render WMF:
  34. *
  35. * <pre>
  36. * <code>
  37. * public class MyImageRendener extends XSLFImageRendener{
  38. * public boolean drawImage(Graphics2D graphics, XSLFPictureData data, Rectangle2D anchor){
  39. * boolean ok = super.drawImage(graphics, data, anchor);
  40. * if(!ok){
  41. * // see what type of image we are
  42. * String contentType = data.getPackagePart().getContentType();
  43. * if(contentType.equals("image/wmf")){
  44. * // use Apache Batik to handle WMF
  45. * // see http://xmlgraphics.apache.org/batik/
  46. * }
  47. *
  48. * }
  49. * return ok;
  50. * }
  51. * }
  52. * </code>
  53. * </pre>
  54. *
  55. * and then pass this class to your instance of java.awt.Graphics2D:
  56. *
  57. * <pre>
  58. * <code>
  59. * graphics.setRenderingHint(XSLFRenderingHint.IMAGE_RENDERER, new MyImageRendener());
  60. * </code>
  61. * </pre>
  62. *
  63. * @author Yegor Kozlov
  64. */
  65. @Beta
  66. public class XSLFImageRenderer {
  67. /**
  68. * Render picture data into the supplied graphics
  69. *
  70. * @return true if the picture data was successfully rendered
  71. */
  72. public boolean drawImage(Graphics2D graphics, XSLFPictureData data,
  73. Rectangle2D anchor) {
  74. return drawImage(graphics, data, anchor, null);
  75. }
  76. /**
  77. * Render picture data into the supplied graphics
  78. *
  79. * @return true if the picture data was successfully rendered
  80. */
  81. public boolean drawImage(Graphics2D graphics, XSLFPictureData data,
  82. Rectangle2D anchor, Insets clip) {
  83. boolean isClipped = true;
  84. if (clip == null) {
  85. isClipped = false;
  86. clip = new Insets(0,0,0,0);
  87. }
  88. BufferedImage img;
  89. try {
  90. img = ImageIO.read(data.getPackagePart().getInputStream());
  91. } catch (Exception e) {
  92. return false;
  93. }
  94. if(img == null) {
  95. return false;
  96. }
  97. int iw = img.getWidth();
  98. int ih = img.getHeight();
  99. double cw = (100000-clip.left-clip.right) / 100000.0;
  100. double ch = (100000-clip.top-clip.bottom) / 100000.0;
  101. double sx = anchor.getWidth()/(iw*cw);
  102. double sy = anchor.getHeight()/(ih*ch);
  103. double tx = anchor.getX()-(iw*sx*clip.left/100000.0);
  104. double ty = anchor.getY()-(ih*sy*clip.top/100000.0);
  105. AffineTransform at = new AffineTransform(sx, 0, 0, sy, tx, ty) ;
  106. Shape clipOld = graphics.getClip();
  107. if (isClipped) graphics.clip(anchor.getBounds2D());
  108. graphics.drawRenderedImage(img, at);
  109. graphics.setClip(clipOld);
  110. return true;
  111. }
  112. /**
  113. * Create a buffered image from the supplied package part.
  114. * This method is called to create texture paints.
  115. *
  116. * @return a <code>BufferedImage</code> containing the decoded
  117. * contents of the input, or <code>null</code>.
  118. */
  119. public BufferedImage readImage(PackagePart packagePart) throws IOException {
  120. return ImageIO.read(packagePart.getInputStream());
  121. }
  122. }