FileDocCategorySizeDatePackage
TestHSSFPictureData.javaAPI DocApache Poi 3.0.12935Thu May 31 18:46:28 BST 2007org.apache.poi.hssf.usermodel

TestHSSFPictureData

public class TestHSSFPictureData extends TestCase
Test HSSFPictureData. 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)

Fields Summary
static String
cwd
Constructors Summary
Methods Summary
public voidtestPictures()


         
        FileInputStream is = new FileInputStream(new File(cwd, "SimpleWithImages.xls"));
        HSSFWorkbook wb = new HSSFWorkbook(is);
        is.close();

        List lst = wb.getAllPictures();
        assertEquals(2, lst.size());

        for (Iterator it = lst.iterator(); it.hasNext(); ) {
            HSSFPictureData pict = (HSSFPictureData)it.next();
            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());
            } 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());

            } else {
                fail("unexpected picture type: " + ext);
            }
        }