2009-05-18 15:11:45 -04:00
|
|
|
/* ====================================================================
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
==================================================================== */
|
|
|
|
|
|
|
|
package org.apache.poi.hssf.usermodel;
|
|
|
|
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
|
|
|
import junit.framework.TestCase;
|
|
|
|
|
|
|
|
import org.apache.poi.hssf.HSSFTestDataSamples;
|
2018-11-16 10:08:59 -05:00
|
|
|
import org.junit.AfterClass;
|
2018-11-16 06:25:59 -05:00
|
|
|
import org.junit.BeforeClass;
|
2009-05-18 15:11:45 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test <code>HSSFPictureData</code>.
|
|
|
|
* The code to retrieve images from a workbook provided by Trejkaz (trejkaz at trypticon dot org) in Bug 41223.
|
|
|
|
*
|
|
|
|
* @author Yegor Kozlov (yegor at apache dot org)
|
|
|
|
* @author Trejkaz (trejkaz at trypticon dot org)
|
|
|
|
*/
|
2018-11-16 10:08:59 -05:00
|
|
|
public final class TestHSSFPictureData extends TestCase {
|
|
|
|
private static boolean cacheBefore = ImageIO.getUseCache();
|
|
|
|
|
2018-11-16 06:25:59 -05:00
|
|
|
@BeforeClass
|
|
|
|
public static void setUpClass() {
|
2018-11-16 10:08:59 -05:00
|
|
|
// disable cache to avoid strange errors related to temporary directories in CI-builds
|
|
|
|
ImageIO.setUseCache(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@AfterClass
|
|
|
|
public static void tearDownClass() {
|
|
|
|
// reset image cache to previous state
|
|
|
|
ImageIO.setUseCache(cacheBefore);
|
2018-11-16 06:25:59 -05:00
|
|
|
}
|
2009-05-18 15:11:45 -04:00
|
|
|
|
|
|
|
public void testPictures() throws IOException {
|
|
|
|
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("SimpleWithImages.xls");
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") // TODO - add getFormat() to interface PictureData and genericise wb.getAllPictures()
|
2017-09-16 04:21:28 -04:00
|
|
|
List<HSSFPictureData> lst = wb.getAllPictures();
|
2009-05-18 15:11:45 -04:00
|
|
|
//assertEquals(2, lst.size());
|
|
|
|
|
2016-10-19 21:54:56 -04:00
|
|
|
for (final HSSFPictureData pict : lst) {
|
2009-05-18 15:11:45 -04:00
|
|
|
String ext = pict.suggestFileExtension();
|
|
|
|
byte[] data = pict.getData();
|
|
|
|
if (ext.equals("jpeg")){
|
|
|
|
//try to read image data using javax.imageio.* (JDK 1.4+)
|
|
|
|
BufferedImage jpg = ImageIO.read(new ByteArrayInputStream(data));
|
|
|
|
assertNotNull(jpg);
|
|
|
|
assertEquals(192, jpg.getWidth());
|
|
|
|
assertEquals(176, jpg.getHeight());
|
|
|
|
assertEquals(HSSFWorkbook.PICTURE_TYPE_JPEG, pict.getFormat());
|
2010-09-09 05:28:04 -04:00
|
|
|
assertEquals("image/jpeg", pict.getMimeType());
|
2009-05-18 15:11:45 -04:00
|
|
|
} else if (ext.equals("png")){
|
|
|
|
//try to read image data using javax.imageio.* (JDK 1.4+)
|
|
|
|
BufferedImage png = ImageIO.read(new ByteArrayInputStream(data));
|
|
|
|
assertNotNull(png);
|
|
|
|
assertEquals(300, png.getWidth());
|
|
|
|
assertEquals(300, png.getHeight());
|
|
|
|
assertEquals(HSSFWorkbook.PICTURE_TYPE_PNG, pict.getFormat());
|
2010-09-09 05:28:04 -04:00
|
|
|
assertEquals("image/png", pict.getMimeType());
|
2018-11-16 06:25:59 -05:00
|
|
|
/*} else {
|
|
|
|
//TODO: test code for PICT, WMF and EMF*/
|
2009-05-18 15:11:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-04 01:30:19 -04:00
|
|
|
|
|
|
|
public void testMacPicture() throws IOException {
|
|
|
|
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("53446.xls");
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
2017-09-16 04:21:28 -04:00
|
|
|
List<HSSFPictureData> lst = wb.getAllPictures();
|
2012-08-04 01:30:19 -04:00
|
|
|
assertEquals(1, lst.size());
|
|
|
|
|
|
|
|
HSSFPictureData pict = lst.get(0);
|
|
|
|
String ext = pict.suggestFileExtension();
|
|
|
|
if (!ext.equals("png")) {
|
|
|
|
fail("Expected a PNG.");
|
|
|
|
}
|
|
|
|
|
|
|
|
//try to read image data using javax.imageio.* (JDK 1.4+)
|
|
|
|
byte[] data = pict.getData();
|
|
|
|
BufferedImage png = ImageIO.read(new ByteArrayInputStream(data));
|
|
|
|
assertNotNull(png);
|
|
|
|
assertEquals(78, png.getWidth());
|
|
|
|
assertEquals(76, png.getHeight());
|
|
|
|
assertEquals(HSSFWorkbook.PICTURE_TYPE_PNG, pict.getFormat());
|
|
|
|
assertEquals("image/png", pict.getMimeType());
|
|
|
|
}
|
2010-10-20 12:38:06 -04:00
|
|
|
|
2018-11-16 06:25:59 -05:00
|
|
|
public void testNotNullPictures() {
|
2010-10-20 12:38:06 -04:00
|
|
|
|
|
|
|
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("SheetWithDrawing.xls");
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") // TODO - add getFormat() to interface PictureData and genericise wb.getAllPictures()
|
2017-09-16 04:21:28 -04:00
|
|
|
List<HSSFPictureData> lst = wb.getAllPictures();
|
2010-10-20 12:38:06 -04:00
|
|
|
for(HSSFPictureData pict : lst){
|
|
|
|
assertNotNull(pict);
|
|
|
|
}
|
|
|
|
}
|
2009-05-18 15:11:45 -04:00
|
|
|
}
|