From 00b6716c191f078b9b4608365d412d1be007c6cb Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Sun, 22 May 2016 16:08:51 +0000 Subject: [PATCH] Bug 58190: Add more overloaded methods for adding pictures from streams and files git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1745073 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xslf/usermodel/Tutorial5.java | 8 +-- .../apache/poi/sl/usermodel/SlideShow.java | 29 ++++++++--- .../poi/xslf/usermodel/XMLSlideShow.java | 43 +++++++++++++++- .../poi/hslf/usermodel/HSLFSlideShow.java | 49 +++++++++++++------ 4 files changed, 102 insertions(+), 27 deletions(-) diff --git a/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial5.java b/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial5.java index ae134de765..56ffcbc505 100644 --- a/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial5.java +++ b/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial5.java @@ -38,11 +38,11 @@ public class Tutorial5 { XMLSlideShow ppt = new XMLSlideShow(); XSLFSlide slide = ppt.createSlide(); - File img = new File(System.getProperty("POI.testdata.path"), "slideshow/clock.jpg"); - byte[] data = IOUtils.toByteArray(new FileInputStream(img)); - XSLFPictureData pictureIndex = ppt.addPicture(data, PictureType.PNG); - /*XSLFPictureShape shape =*/ slide.createPicture(pictureIndex); + File img = new File(System.getProperty("POI.testdata.path", "test-data"), "slideshow/clock.jpg"); + XSLFPictureData pictureData = ppt.addPicture(img, PictureType.PNG); + + /*XSLFPictureShape shape =*/ slide.createPicture(pictureData); FileOutputStream out = new FileOutputStream("images.pptx"); ppt.write(out); diff --git a/src/java/org/apache/poi/sl/usermodel/SlideShow.java b/src/java/org/apache/poi/sl/usermodel/SlideShow.java index 9d4a5cf9dc..10f442ae68 100644 --- a/src/java/org/apache/poi/sl/usermodel/SlideShow.java +++ b/src/java/org/apache/poi/sl/usermodel/SlideShow.java @@ -18,9 +18,7 @@ package org.apache.poi.sl.usermodel; import java.awt.Dimension; -import java.io.Closeable; -import java.io.IOException; -import java.io.OutputStream; +import java.io.*; import java.util.List; import org.apache.poi.sl.usermodel.PictureData.PictureType; @@ -64,17 +62,36 @@ public interface SlideShow< */ List getPictureData(); - /** - * Adds a picture to the workbook. + * Adds a picture to the presentation. * * @param pictureData The bytes of the picture * @param format The format of the picture. * - * @return the new picture reference + * @return the picture data reference. */ PictureData addPicture(byte[] pictureData, PictureType format) throws IOException; + /** + * Adds a picture to the presentation. + * + * @param is The stream to read the image from + * @param format The format of the picture. + * + * @return the picture data reference. + */ + PictureData addPicture(InputStream is, PictureType format) throws IOException; + + /** + * Adds a picture to the presentation. + * + * @param pict The file containing the image to add + * @param format The format of the picture. + * + * @return the picture data reference + */ + PictureData addPicture(File pict, PictureType format) throws IOException; + /** * Writes out the slideshow file the is represented by an instance of this * class diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java index a4e2a670b1..17a736859d 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java @@ -19,6 +19,8 @@ package org.apache.poi.xslf.usermodel; import static org.apache.poi.POIXMLTypeLoader.DEFAULT_XML_OPTIONS; import java.awt.Dimension; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -202,7 +204,7 @@ implements SlideShow { /** * Create a slide and initialize it from the specified layout. * - * @param layout + * @param layout The layout to use for the new slide. * @return created slide */ public XSLFSlide createSlide(XSLFSlideLayout layout) { @@ -461,6 +463,45 @@ implements SlideShow { return img; } + + /** + * Adds a picture to the slideshow. + * + * @param is The stream to read image from + * @param format The format of the picture + * + * @return the picture data + */ + @Override + public XSLFPictureData addPicture(InputStream is, PictureType format) throws IOException + { + return addPicture(IOUtils.toByteArray(is), format); + } + + + /** + * Adds a picture to the presentation. + * + * @param pict The file containing the image to add + * @param format The format of the picture. + * + * @return the picture data + */ + @Override + public XSLFPictureData addPicture(File pict, PictureType format) throws IOException + { + int length = (int) pict.length(); + byte[] data = new byte[length]; + FileInputStream is = new FileInputStream(pict); + try { + IOUtils.readFully(is, data); + } finally { + is.close(); + } + return addPicture(data, format); + } + + /** * check if a picture with this picture data already exists in this presentation */ diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShow.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShow.java index 5a94b08f72..10990eaf4c 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShow.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShow.java @@ -78,6 +78,7 @@ import org.apache.poi.sl.usermodel.MasterSheet; import org.apache.poi.sl.usermodel.PictureData.PictureType; import org.apache.poi.sl.usermodel.Resources; import org.apache.poi.sl.usermodel.SlideShow; +import org.apache.poi.util.IOUtils; import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogger; import org.apache.poi.util.Units; @@ -268,10 +269,10 @@ public final class HSLFSlideShow implements SlideShowPicture class. - * @return the index to this picture (1 based). + * The format of the picture. + * + * @return the picture data. */ + @Override public HSLFPictureData addPicture(File pict, PictureType format) throws IOException { + if (format == null || format.nativeId == -1) { // fail early + throw new IllegalArgumentException("Unsupported picture format: " + format); + } int length = (int) pict.length(); byte[] data = new byte[length]; - FileInputStream is = null; - try { - is = new FileInputStream(pict); - is.read(data); + FileInputStream is = new FileInputStream(pict); + try { + IOUtils.readFully(is, data); } finally { - if(is != null) is.close(); + is.close(); } return addPicture(data, format); } -- 2.39.5