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.

TestHSSFPictureData.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 static org.junit.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertFalse;
  18. import static org.junit.jupiter.api.Assertions.assertNotNull;
  19. import java.awt.image.BufferedImage;
  20. import java.io.ByteArrayInputStream;
  21. import java.io.IOException;
  22. import java.util.List;
  23. import javax.imageio.IIOException;
  24. import javax.imageio.ImageIO;
  25. import org.apache.poi.POITestCase;
  26. import org.apache.poi.hssf.HSSFTestDataSamples;
  27. import org.junit.jupiter.api.BeforeAll;
  28. import org.junit.jupiter.api.Test;
  29. /**
  30. * Test <code>HSSFPictureData</code>.
  31. * The code to retrieve images from a workbook provided by Trejkaz (trejkaz at trypticon dot org) in Bug 41223.
  32. */
  33. final class TestHSSFPictureData {
  34. @BeforeAll
  35. public static void setUpClass() {
  36. POITestCase.setImageIOCacheDir();
  37. }
  38. @Test
  39. void testPictures() throws IOException {
  40. HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("SimpleWithImages.xls");
  41. // TODO - add getFormat() to interface PictureData and genericise wb.getAllPictures()
  42. List<HSSFPictureData> lst = wb.getAllPictures();
  43. //assertEquals(2, lst.size());
  44. try {
  45. for (final HSSFPictureData pict : lst) {
  46. String ext = pict.suggestFileExtension();
  47. byte[] data = pict.getData();
  48. if (ext.equals("jpeg")) {
  49. //try to read image data using javax.imageio.* (JDK 1.4+)
  50. BufferedImage jpg = ImageIO.read(new ByteArrayInputStream(data));
  51. assertNotNull(jpg);
  52. assertEquals(192, jpg.getWidth());
  53. assertEquals(176, jpg.getHeight());
  54. assertEquals(HSSFWorkbook.PICTURE_TYPE_JPEG, pict.getFormat());
  55. assertEquals("image/jpeg", pict.getMimeType());
  56. } else if (ext.equals("png")) {
  57. //try to read image data using javax.imageio.* (JDK 1.4+)
  58. BufferedImage png = ImageIO.read(new ByteArrayInputStream(data));
  59. assertNotNull(png);
  60. assertEquals(300, png.getWidth());
  61. assertEquals(300, png.getHeight());
  62. assertEquals(HSSFWorkbook.PICTURE_TYPE_PNG, pict.getFormat());
  63. assertEquals("image/png", pict.getMimeType());
  64. /*} else {
  65. //TODO: test code for PICT, WMF and EMF*/
  66. }
  67. }
  68. } catch (IIOException e) {
  69. assertFalse(e.getMessage().contains("Can't create cache file"), e.getMessage());
  70. }
  71. }
  72. @Test
  73. void testMacPicture() throws IOException {
  74. HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("53446.xls");
  75. try{
  76. List<HSSFPictureData> lst = wb.getAllPictures();
  77. assertEquals(1, lst.size());
  78. HSSFPictureData pict = lst.get(0);
  79. String ext = pict.suggestFileExtension();
  80. assertEquals("png", ext, "Expected a PNG.");
  81. //try to read image data using javax.imageio.* (JDK 1.4+)
  82. byte[] data = pict.getData();
  83. BufferedImage png = ImageIO.read(new ByteArrayInputStream(data));
  84. assertNotNull(png);
  85. assertEquals(78, png.getWidth());
  86. assertEquals(76, png.getHeight());
  87. assertEquals(HSSFWorkbook.PICTURE_TYPE_PNG, pict.getFormat());
  88. assertEquals("image/png", pict.getMimeType());
  89. } catch (IIOException e) {
  90. assertFalse(e.getMessage().contains("Can't create cache file"), e.getMessage());
  91. }
  92. }
  93. @Test
  94. void testNotNullPictures() {
  95. HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("SheetWithDrawing.xls");
  96. // TODO - add getFormat() to interface PictureData and genericise wb.getAllPictures()
  97. List<HSSFPictureData> lst = wb.getAllPictures();
  98. for(HSSFPictureData pict : lst){
  99. assertNotNull(pict);
  100. }
  101. }
  102. }