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.

HSSFPicture.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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.hssf.usermodel;
  16. import java.awt.Dimension;
  17. import java.io.ByteArrayInputStream;
  18. import java.io.UnsupportedEncodingException;
  19. import org.apache.poi.ddf.DefaultEscherRecordFactory;
  20. import org.apache.poi.ddf.EscherBSERecord;
  21. import org.apache.poi.ddf.EscherBlipRecord;
  22. import org.apache.poi.ddf.EscherClientDataRecord;
  23. import org.apache.poi.ddf.EscherComplexProperty;
  24. import org.apache.poi.ddf.EscherContainerRecord;
  25. import org.apache.poi.ddf.EscherOptRecord;
  26. import org.apache.poi.ddf.EscherProperties;
  27. import org.apache.poi.ddf.EscherSimpleProperty;
  28. import org.apache.poi.ddf.EscherTextboxRecord;
  29. import org.apache.poi.hssf.model.InternalWorkbook;
  30. import org.apache.poi.hssf.record.CommonObjectDataSubRecord;
  31. import org.apache.poi.hssf.record.EscherAggregate;
  32. import org.apache.poi.hssf.record.ObjRecord;
  33. import org.apache.poi.ss.usermodel.Picture;
  34. import org.apache.poi.ss.util.ImageUtils;
  35. import org.apache.poi.util.POILogFactory;
  36. import org.apache.poi.util.POILogger;
  37. /**
  38. * Represents a escher picture. Eg. A GIF, JPEG etc...
  39. */
  40. public class HSSFPicture extends HSSFSimpleShape implements Picture {
  41. private static POILogger logger = POILogFactory.getLogger(HSSFPicture.class);
  42. public static final int PICTURE_TYPE_EMF = HSSFWorkbook.PICTURE_TYPE_EMF; // Windows Enhanced Metafile
  43. public static final int PICTURE_TYPE_WMF = HSSFWorkbook.PICTURE_TYPE_WMF; // Windows Metafile
  44. public static final int PICTURE_TYPE_PICT = HSSFWorkbook.PICTURE_TYPE_PICT; // Macintosh PICT
  45. public static final int PICTURE_TYPE_JPEG = HSSFWorkbook.PICTURE_TYPE_JPEG; // JFIF
  46. public static final int PICTURE_TYPE_PNG = HSSFWorkbook.PICTURE_TYPE_PNG; // PNG
  47. public static final int PICTURE_TYPE_DIB = HSSFWorkbook.PICTURE_TYPE_DIB; // Windows DIB
  48. public HSSFPicture(EscherContainerRecord spContainer, ObjRecord objRecord) {
  49. super(spContainer, objRecord);
  50. }
  51. /**
  52. * Constructs a picture object.
  53. */
  54. public HSSFPicture( HSSFShape parent, HSSFAnchor anchor )
  55. {
  56. super( parent, anchor );
  57. super.setShapeType(OBJECT_TYPE_PICTURE);
  58. CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) getObjRecord().getSubRecords().get(0);
  59. cod.setObjectType(CommonObjectDataSubRecord.OBJECT_TYPE_PICTURE);
  60. }
  61. public int getPictureIndex()
  62. {
  63. EscherSimpleProperty property = getOptRecord().lookup(EscherProperties.BLIP__BLIPTODISPLAY);
  64. if (null == property){
  65. return -1;
  66. }
  67. return property.getPropertyValue();
  68. }
  69. public void setPictureIndex( int pictureIndex )
  70. {
  71. setPropertyValue(new EscherSimpleProperty( EscherProperties.BLIP__BLIPTODISPLAY, false, true, pictureIndex));
  72. }
  73. @Override
  74. protected EscherContainerRecord createSpContainer() {
  75. EscherContainerRecord spContainer = super.createSpContainer();
  76. EscherOptRecord opt = spContainer.getChildById(EscherOptRecord.RECORD_ID);
  77. opt.removeEscherProperty(EscherProperties.LINESTYLE__LINEDASHING);
  78. opt.removeEscherProperty(EscherProperties.LINESTYLE__NOLINEDRAWDASH);
  79. spContainer.removeChildRecord(spContainer.getChildById(EscherTextboxRecord.RECORD_ID));
  80. return spContainer;
  81. }
  82. /**
  83. * Reset the image to the dimension of the embedded image
  84. *
  85. * <p>
  86. * Please note, that this method works correctly only for workbooks
  87. * with default font size (Arial 10pt for .xls).
  88. * If the default font is changed the resized image can be streched vertically or horizontally.
  89. * </p>
  90. */
  91. public void resize(){
  92. resize(Double.MAX_VALUE);
  93. }
  94. /**
  95. * Resize the image proportionally.
  96. *
  97. * @see #resize(double, double)
  98. */
  99. public void resize(double scale) {
  100. resize(scale,scale);
  101. }
  102. /**
  103. * Resize the image
  104. * <p>
  105. * Please note, that this method works correctly only for workbooks
  106. * with default font size (Arial 10pt for .xls).
  107. * If the default font is changed the resized image can be streched vertically or horizontally.
  108. * </p>
  109. * <p>
  110. * <code>resize(1.0,1.0)</code> keeps the original size,<br/>
  111. * <code>resize(0.5,0.5)</code> resize to 50% of the original,<br/>
  112. * <code>resize(2.0,2.0)</code> resizes to 200% of the original.<br/>
  113. * <code>resize({@link Double#MAX_VALUE},{@link Double#MAX_VALUE})</code> resizes to the dimension of the embedded image.
  114. * </p>
  115. *
  116. * @param scaleX the amount by which the image width is multiplied relative to the original width.
  117. * @param scaleY the amount by which the image height is multiplied relative to the original height.
  118. */
  119. public void resize(double scaleX, double scaleY) {
  120. HSSFClientAnchor anchor = getClientAnchor();
  121. anchor.setAnchorType(2);
  122. HSSFClientAnchor pref = getPreferredSize(scaleX,scaleY);
  123. int row2 = anchor.getRow1() + (pref.getRow2() - pref.getRow1());
  124. int col2 = anchor.getCol1() + (pref.getCol2() - pref.getCol1());
  125. anchor.setCol2((short)col2);
  126. // anchor.setDx1(0);
  127. anchor.setDx2(pref.getDx2());
  128. anchor.setRow2(row2);
  129. // anchor.setDy1(0);
  130. anchor.setDy2(pref.getDy2());
  131. }
  132. /**
  133. * Calculate the preferred size for this picture.
  134. *
  135. * @return HSSFClientAnchor with the preferred size for this image
  136. * @since POI 3.0.2
  137. */
  138. public HSSFClientAnchor getPreferredSize(){
  139. return getPreferredSize(1.0);
  140. }
  141. /**
  142. * Calculate the preferred size for this picture.
  143. *
  144. * @param scale the amount by which image dimensions are multiplied relative to the original size.
  145. * @return HSSFClientAnchor with the preferred size for this image
  146. * @since POI 3.0.2
  147. */
  148. public HSSFClientAnchor getPreferredSize(double scale){
  149. return getPreferredSize(scale, scale);
  150. }
  151. /**
  152. * Calculate the preferred size for this picture.
  153. *
  154. * @param scaleX the amount by which image width is multiplied relative to the original width.
  155. * @param scaleY the amount by which image height is multiplied relative to the original height.
  156. * @return HSSFClientAnchor with the preferred size for this image
  157. * @since POI 3.11
  158. */
  159. public HSSFClientAnchor getPreferredSize(double scaleX, double scaleY){
  160. ImageUtils.setPreferredSize(this, scaleX, scaleY);
  161. return getClientAnchor();
  162. }
  163. /**
  164. * Return the dimension of the embedded image in pixel
  165. *
  166. * @return image dimension in pixels
  167. */
  168. public Dimension getImageDimension(){
  169. InternalWorkbook iwb = getPatriarch().getSheet().getWorkbook().getWorkbook();
  170. EscherBSERecord bse = iwb.getBSERecord(getPictureIndex());
  171. byte[] data = bse.getBlipRecord().getPicturedata();
  172. int type = bse.getBlipTypeWin32();
  173. return ImageUtils.getImageDimension(new ByteArrayInputStream(data), type);
  174. }
  175. /**
  176. * Return picture data for this shape
  177. *
  178. * @return picture data for this shape
  179. */
  180. public HSSFPictureData getPictureData(){
  181. InternalWorkbook iwb = getPatriarch().getSheet().getWorkbook().getWorkbook();
  182. EscherBSERecord bse = iwb.getBSERecord(getPictureIndex());
  183. EscherBlipRecord blipRecord = bse.getBlipRecord();
  184. return new HSSFPictureData(blipRecord);
  185. }
  186. @Override
  187. void afterInsert(HSSFPatriarch patriarch) {
  188. EscherAggregate agg = patriarch._getBoundAggregate();
  189. agg.associateShapeToObjRecord(getEscherContainer().getChildById(EscherClientDataRecord.RECORD_ID), getObjRecord());
  190. EscherBSERecord bse =
  191. patriarch.getSheet().getWorkbook().getWorkbook().getBSERecord(getPictureIndex());
  192. bse.setRef(bse.getRef() + 1);
  193. }
  194. /**
  195. * The color applied to the lines of this shape.
  196. */
  197. public String getFileName() {
  198. EscherComplexProperty propFile = (EscherComplexProperty) getOptRecord().lookup(
  199. EscherProperties.BLIP__BLIPFILENAME);
  200. try {
  201. if (null == propFile){
  202. return "";
  203. }
  204. return new String(propFile.getComplexData(), "UTF-16LE").trim();
  205. } catch (UnsupportedEncodingException e) {
  206. return "";
  207. }
  208. }
  209. public void setFileName(String data){
  210. try {
  211. EscherComplexProperty prop = new EscherComplexProperty(EscherProperties.BLIP__BLIPFILENAME, true, data.getBytes("UTF-16LE"));
  212. setPropertyValue(prop);
  213. } catch (UnsupportedEncodingException e) {
  214. logger.log( POILogger.ERROR, "Unsupported encoding: UTF-16LE");
  215. }
  216. }
  217. @Override
  218. public void setShapeType(int shapeType) {
  219. throw new IllegalStateException("Shape type can not be changed in "+this.getClass().getSimpleName());
  220. }
  221. @Override
  222. protected HSSFShape cloneShape() {
  223. EscherContainerRecord spContainer = new EscherContainerRecord();
  224. byte [] inSp = getEscherContainer().serialize();
  225. spContainer.fillFields(inSp, 0, new DefaultEscherRecordFactory());
  226. ObjRecord obj = (ObjRecord) getObjRecord().cloneViaReserialise();
  227. return new HSSFPicture(spContainer, obj);
  228. }
  229. /**
  230. * @return the anchor that is used by this picture.
  231. */
  232. @Override
  233. public HSSFClientAnchor getClientAnchor() {
  234. HSSFAnchor a = getAnchor();
  235. return (a instanceof HSSFClientAnchor) ? (HSSFClientAnchor)a : null;
  236. }
  237. /**
  238. * @return the sheet which contains the picture shape
  239. */
  240. @Override
  241. public HSSFSheet getSheet() {
  242. return getPatriarch().getSheet();
  243. }
  244. }