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.

HemfImageRenderer.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hemf.draw;
  16. import java.awt.Dimension;
  17. import java.awt.Graphics2D;
  18. import java.awt.Insets;
  19. import java.awt.RenderingHints;
  20. import java.awt.geom.Dimension2D;
  21. import java.awt.geom.Rectangle2D;
  22. import java.awt.image.BufferedImage;
  23. import java.awt.image.RescaleOp;
  24. import java.io.ByteArrayInputStream;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import org.apache.poi.hemf.usermodel.HemfPicture;
  28. import org.apache.poi.sl.draw.ImageRenderer;
  29. import org.apache.poi.sl.usermodel.PictureData;
  30. import org.apache.poi.util.Units;
  31. public class HemfImageRenderer implements ImageRenderer {
  32. HemfPicture image;
  33. double alpha;
  34. @Override
  35. public boolean canRender(String contentType) {
  36. return PictureData.PictureType.EMF.contentType.equalsIgnoreCase(contentType);
  37. }
  38. @Override
  39. public void loadImage(InputStream data, String contentType) throws IOException {
  40. if (!PictureData.PictureType.EMF.contentType.equals(contentType)) {
  41. throw new IOException("Invalid picture type");
  42. }
  43. image = new HemfPicture(data);
  44. }
  45. @Override
  46. public void loadImage(byte[] data, String contentType) throws IOException {
  47. if (!PictureData.PictureType.EMF.contentType.equals(contentType)) {
  48. throw new IOException("Invalid picture type");
  49. }
  50. image = new HemfPicture(new ByteArrayInputStream(data));
  51. }
  52. @Override
  53. public Dimension getDimension() {
  54. int width = 0, height = 0;
  55. if (image != null) {
  56. Dimension2D dim = image.getSize();
  57. width = Units.pointsToPixel(dim.getWidth());
  58. // keep aspect ratio for height
  59. height = Units.pointsToPixel(dim.getHeight());
  60. }
  61. return new Dimension(width, height);
  62. }
  63. @Override
  64. public void setAlpha(double alpha) {
  65. this.alpha = alpha;
  66. }
  67. @Override
  68. public BufferedImage getImage() {
  69. return getImage(getDimension());
  70. }
  71. @Override
  72. public BufferedImage getImage(Dimension dim) {
  73. if (image == null) {
  74. return new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
  75. }
  76. BufferedImage bufImg = new BufferedImage((int)dim.getWidth(), (int)dim.getHeight(), BufferedImage.TYPE_INT_ARGB);
  77. Graphics2D g = bufImg.createGraphics();
  78. g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  79. g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  80. g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
  81. g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  82. image.draw(g, new Rectangle2D.Double(0,0,dim.getWidth(),dim.getHeight()));
  83. g.dispose();
  84. if (alpha != 0) {
  85. BufferedImage newImg = new BufferedImage((int)dim.getWidth(), (int)dim.getHeight(), BufferedImage.TYPE_INT_ARGB);
  86. g = newImg.createGraphics();
  87. RescaleOp op = new RescaleOp(new float[]{1.0f, 1.0f, 1.0f, (float)alpha}, new float[]{0,0,0,0}, null);
  88. g.drawImage(bufImg, op, 0, 0);
  89. g.dispose();
  90. bufImg = newImg;
  91. }
  92. return bufImg;
  93. }
  94. @Override
  95. public boolean drawImage(Graphics2D graphics, Rectangle2D anchor) {
  96. return drawImage(graphics, anchor, null);
  97. }
  98. @Override
  99. public boolean drawImage(Graphics2D graphics, Rectangle2D anchor, Insets clip) {
  100. if (image == null) {
  101. return false;
  102. } else {
  103. image.draw(graphics, anchor);
  104. return true;
  105. }
  106. }
  107. }