Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ImageRenderer.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.sl.draw;
  20. import java.awt.Graphics2D;
  21. import java.awt.Insets;
  22. import java.awt.geom.Dimension2D;
  23. import java.awt.geom.Rectangle2D;
  24. import java.awt.image.BufferedImage;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.nio.charset.Charset;
  28. import org.apache.poi.common.usermodel.GenericRecord;
  29. import org.apache.poi.util.Dimension2DDouble;
  30. /**
  31. * Classes can implement this interfaces to support other formats, for
  32. * example, use Apache Batik to render WMF (since POI 4.0, there's an internal WMF/EMF/EMF+ renderer in POI),
  33. * PICT can be rendered using Apple QuickTime API for Java:
  34. *
  35. * <pre>
  36. * <code>
  37. * public class MyImageRendener implements ImageRendener {
  38. * InputStream data;
  39. *
  40. * public boolean drawImage(Graphics2D graphics,Rectangle2D anchor,Insets clip) {
  41. * // draw image
  42. * DataInputStream is = new DataInputStream(data);
  43. * org.apache.batik.transcoder.wmf.tosvg.WMFRecordStore wmfStore =
  44. * new org.apache.batik.transcoder.wmf.tosvg.WMFRecordStore();
  45. * try {
  46. * wmfStore.read(is);
  47. * } catch (IOException e){
  48. * return;
  49. * }
  50. *
  51. * float scale = (float)anchor.width/wmfStore.getWidthPixels();
  52. *
  53. * org.apache.batik.transcoder.wmf.tosvg.WMFPainter painter =
  54. * new org.apache.batik.transcoder.wmf.tosvg.WMFPainter(wmfStore, 0, 0, scale);
  55. * graphics.translate(anchor.x, anchor.y);
  56. * painter.paint(graphics);
  57. * }
  58. *
  59. * public void loadImage(InputStream data, String contentType) throws IOException {
  60. * if ("image/wmf".equals(contentType)) {
  61. * this.data = data;
  62. * // use Apache Batik to handle WMF
  63. * } else {
  64. * super.loadImage(data,contentType);
  65. * }
  66. * }
  67. * }
  68. * </code>
  69. * </pre>
  70. *
  71. * and then pass this class to your instance of java.awt.Graphics2D:
  72. *
  73. * <pre>
  74. * <code>
  75. * graphics.setRenderingHint(Drawable.IMAGE_RENDERER, new MyImageRendener());
  76. * </code>
  77. * </pre>
  78. */
  79. public interface ImageRenderer {
  80. /**
  81. * Determines if this image renderer implementation supports the given contentType
  82. * @param contentType the image content type
  83. * @return if the content type is supported
  84. */
  85. boolean canRender(String contentType);
  86. /**
  87. * Load and buffer the image
  88. *
  89. * @param data the raw image stream
  90. * @param contentType the content type
  91. */
  92. void loadImage(InputStream data, String contentType) throws IOException;
  93. /**
  94. * Load and buffer the image
  95. *
  96. * @param data the raw image bytes
  97. * @param contentType the content type
  98. */
  99. void loadImage(byte[] data, String contentType) throws IOException;
  100. /**
  101. * @return the format-specific / not-normalized bounds of the image
  102. */
  103. Rectangle2D getNativeBounds();
  104. /**
  105. * @return the bounds of the buffered image in pixel
  106. */
  107. Rectangle2D getBounds();
  108. /**
  109. * @return the dimension of the buffered image in pixel
  110. */
  111. default Dimension2D getDimension() {
  112. Rectangle2D r = getBounds();
  113. return new Dimension2DDouble(Math.abs(r.getWidth()), Math.abs(r.getHeight()));
  114. }
  115. /**
  116. * @param alpha the alpha [0..1] to be added to the image (possibly already containing an alpha channel)
  117. */
  118. void setAlpha(double alpha);
  119. /**
  120. * @return the image as buffered image or null if image could not be loaded
  121. */
  122. BufferedImage getImage();
  123. /**
  124. * @param dim the dimension in pixels of the returned image
  125. * @return the image as buffered image or null if image could not be loaded
  126. *
  127. * @since POI 3.15-beta2
  128. */
  129. BufferedImage getImage(Dimension2D dim);
  130. /**
  131. * Render picture data into the supplied graphics
  132. *
  133. * @return true if the picture data was successfully rendered
  134. */
  135. boolean drawImage(Graphics2D graphics, Rectangle2D anchor);
  136. /**
  137. * Render picture data into the supplied graphics
  138. *
  139. * @return true if the picture data was successfully rendered
  140. */
  141. boolean drawImage(Graphics2D graphics, Rectangle2D anchor, Insets clip);
  142. default GenericRecord getGenericRecord() { return null; }
  143. /**
  144. * Sets the default charset to render text elements.
  145. * Opposed to other windows libraries in POI this simply defaults to Windows-1252.
  146. *
  147. * @param defaultCharset the default charset
  148. */
  149. default void setDefaultCharset(Charset defaultCharset) {}
  150. }