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.

HSSFPictureData.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 org.apache.poi.ddf.EscherBlipRecord;
  17. import org.apache.poi.ddf.EscherRecordTypes;
  18. import org.apache.poi.sl.image.ImageHeaderPNG;
  19. import org.apache.poi.ss.usermodel.PictureData;
  20. import org.apache.poi.ss.usermodel.Workbook;
  21. /**
  22. * Represents binary data stored in the file. Eg. A GIF, JPEG etc...
  23. */
  24. public class HSSFPictureData implements PictureData
  25. {
  26. // MSOBI constants for various formats.
  27. public static final short MSOBI_WMF = 0x2160;
  28. public static final short MSOBI_EMF = 0x3D40;
  29. public static final short MSOBI_PICT = 0x5420;
  30. public static final short MSOBI_PNG = 0x6E00;
  31. public static final short MSOBI_JPEG = 0x46A0;
  32. public static final short MSOBI_DIB = 0x7A80;
  33. // Mask of the bits in the options used to store the image format.
  34. public static final short FORMAT_MASK = (short) 0xFFF0;
  35. /**
  36. * Underlying escher blip record containing the bitmap data.
  37. */
  38. private EscherBlipRecord blip;
  39. /**
  40. * Constructs a picture object.
  41. *
  42. * @param blip the underlying blip record containing the bitmap data.
  43. */
  44. public HSSFPictureData( EscherBlipRecord blip )
  45. {
  46. this.blip = blip;
  47. }
  48. /* (non-Javadoc)
  49. * @see org.apache.poi.hssf.usermodel.PictureData#getData()
  50. */
  51. public byte[] getData() {
  52. return new ImageHeaderPNG(blip.getPicturedata()).extractPNG();
  53. }
  54. /**
  55. *
  56. * @return format of the picture.
  57. * @see HSSFWorkbook#PICTURE_TYPE_DIB
  58. * @see HSSFWorkbook#PICTURE_TYPE_WMF
  59. * @see HSSFWorkbook#PICTURE_TYPE_EMF
  60. * @see HSSFWorkbook#PICTURE_TYPE_PNG
  61. * @see HSSFWorkbook#PICTURE_TYPE_JPEG
  62. * @see HSSFWorkbook#PICTURE_TYPE_PICT
  63. */
  64. public int getFormat(){
  65. return blip.getRecordId() - (short)0xF018;
  66. }
  67. /**
  68. * @see #getFormat
  69. * @return 'wmf', 'jpeg' etc depending on the format. never <code>null</code>
  70. */
  71. public String suggestFileExtension() {
  72. switch (EscherRecordTypes.forTypeID(blip.getRecordId())) {
  73. case BLIP_WMF:
  74. return "wmf";
  75. case BLIP_EMF:
  76. return "emf";
  77. case BLIP_PICT:
  78. return "pict";
  79. case BLIP_PNG:
  80. return "png";
  81. case BLIP_JPEG:
  82. return "jpeg";
  83. case BLIP_DIB:
  84. return "dib";
  85. default:
  86. return "";
  87. }
  88. }
  89. /**
  90. * Returns the mime type for the image
  91. */
  92. public String getMimeType() {
  93. switch (EscherRecordTypes.forTypeID(blip.getRecordId())) {
  94. case BLIP_WMF:
  95. return "image/x-wmf";
  96. case BLIP_EMF:
  97. return "image/x-emf";
  98. case BLIP_PICT:
  99. return "image/x-pict";
  100. case BLIP_PNG:
  101. return "image/png";
  102. case BLIP_JPEG:
  103. return "image/jpeg";
  104. case BLIP_DIB:
  105. return "image/bmp";
  106. default:
  107. return "image/unknown";
  108. }
  109. }
  110. /**
  111. * @return the POI internal image type, -1 if not unknown image type
  112. *
  113. * @see Workbook#PICTURE_TYPE_DIB
  114. * @see Workbook#PICTURE_TYPE_EMF
  115. * @see Workbook#PICTURE_TYPE_JPEG
  116. * @see Workbook#PICTURE_TYPE_PICT
  117. * @see Workbook#PICTURE_TYPE_PNG
  118. * @see Workbook#PICTURE_TYPE_WMF
  119. */
  120. public int getPictureType() {
  121. switch (EscherRecordTypes.forTypeID(blip.getRecordId())) {
  122. case BLIP_WMF:
  123. return Workbook.PICTURE_TYPE_WMF;
  124. case BLIP_EMF:
  125. return Workbook.PICTURE_TYPE_EMF;
  126. case BLIP_PICT:
  127. return Workbook.PICTURE_TYPE_PICT;
  128. case BLIP_PNG:
  129. return Workbook.PICTURE_TYPE_PNG;
  130. case BLIP_JPEG:
  131. return Workbook.PICTURE_TYPE_JPEG;
  132. case BLIP_DIB:
  133. return Workbook.PICTURE_TYPE_DIB;
  134. default:
  135. return -1;
  136. }
  137. }
  138. }