package org.apache.poi.hslf.usermodel;\r
\r
import org.apache.poi.hslf.*;\r
+import org.apache.poi.hslf.usermodel.PictureData;\r
+import org.apache.poi.hslf.usermodel.SlideShow;\r
+import org.apache.poi.hslf.model.Slide;\r
+import org.apache.poi.hslf.model.Shape;\r
+import org.apache.poi.hslf.model.Picture;\r
+import org.apache.poi.util.LittleEndian;\r
import junit.framework.TestCase;\r
\r
import javax.imageio.ImageIO;\r
import java.awt.image.BufferedImage;\r
import java.io.ByteArrayInputStream;\r
+import java.io.ByteArrayOutputStream;\r
+import java.io.File;\r
\r
/**\r
* Test extracting images from a ppt file\r
* @author Yegor Kozlov\r
*/\r
public class TestPictures extends TestCase{\r
+ public static String dirname = System.getProperty("HSLF.testdata.path");\r
+ public static String filename = dirname + "/ppt_with_png.ppt";\r
\r
- public void testPictures() throws Exception {\r
- String dirname = System.getProperty("HSLF.testdata.path");\r
- String filename = dirname + "/ppt_with_png.ppt";\r
+ public void testReadPictures() throws Exception {\r
\r
HSLFSlideShow ppt = new HSLFSlideShow(filename);\r
- Picture[] pict = ppt.getPictures();\r
+ PictureData[] pict = ppt.getPictures();\r
assertNotNull(pict);\r
for (int i = 0; i < pict.length; i++) {\r
byte[] data = pict[i].getData();\r
+\r
BufferedImage img = ImageIO.read(new ByteArrayInputStream(data));\r
assertNotNull(img);\r
}\r
ppt.close();\r
}\r
+\r
+ public void testSerializePictures() throws Exception {\r
+ HSLFSlideShow ppt = new HSLFSlideShow(filename);\r
+ PictureData[] pict = ppt.getPictures();\r
+ assertNotNull(pict);\r
+\r
+ ByteArrayOutputStream out = new ByteArrayOutputStream();\r
+ ppt.write(out);\r
+ out.close();\r
+\r
+ ppt = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray()));\r
+ pict = ppt.getPictures();\r
+ assertNotNull(pict);\r
+ }\r
+\r
+ public void testAddPictures() throws Exception {\r
+ int idx;\r
+ Slide slide;\r
+ Picture pict;\r
+\r
+ SlideShow ppt = new SlideShow();\r
+\r
+ idx = ppt.addPicture(new File(dirname + "/clock.jpg"), Picture.JPEG);\r
+ slide = ppt.createSlide();\r
+ pict = new Picture(idx);\r
+ pict.setDefaultSize(ppt);\r
+ slide.addShape(pict);\r
+\r
+ idx = ppt.addPicture(new File(dirname + "/painting.png"), Picture.PNG);\r
+ pict = new Picture(idx);\r
+ pict.setDefaultSize(ppt);\r
+ slide.addShape(pict);\r
+\r
+ ByteArrayOutputStream out = new ByteArrayOutputStream();\r
+ ppt.write(out);\r
+ out.close();\r
+\r
+ ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray())));\r
+ assertTrue(ppt.getPictures().length == 2 );\r
+ }\r
+\r
}\r