您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

XSLFImageRendener.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 org.apache.poi.openxml4j.opc.PackagePart;
  21. import org.apache.poi.util.Beta;
  22. import javax.imageio.ImageIO;
  23. import java.awt.Graphics2D;
  24. import java.awt.geom.Rectangle2D;
  25. import java.awt.image.BufferedImage;
  26. import java.io.ByteArrayInputStream;
  27. import java.io.IOException;
  28. import java.io.InputStream;
  29. /**
  30. * For now this class renders only images supported by the javax.imageio.ImageIO
  31. * framework. Subclasses can override this class to support other formats, for
  32. * example, Use Apache batik to render WMF:
  33. *
  34. * <pre>
  35. * <code>
  36. * @Override
  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 XSLFImageRendener {
  67. /**
  68. * Render picture data into the supplied graphics
  69. *
  70. * @return true if the picture data was succesfully renderered
  71. */
  72. public boolean drawImage(Graphics2D graphics, XSLFPictureData data,
  73. Rectangle2D anchor) {
  74. try {
  75. BufferedImage img = ImageIO.read(data.getPackagePart().getInputStream());
  76. graphics.drawImage(img,
  77. (int) anchor.getX(), (int) anchor.getY(),
  78. (int) anchor.getWidth(), (int) anchor.getHeight(), null);
  79. return true;
  80. } catch (Exception e) {
  81. return false;
  82. }
  83. }
  84. /**
  85. * Create a buffered image from the supplied package part.
  86. * This method is called to create texture paints.
  87. *
  88. * @return a <code>BufferedImage</code> containing the decoded
  89. * contents of the input, or <code>null</code>.
  90. */
  91. public BufferedImage readImage(PackagePart packagePart) throws IOException {
  92. return ImageIO.read(packagePart.getInputStream());
  93. }
  94. }