ASF Bugzilla – Attachment 32954 Details for
Bug 58190
[PATCH] The current picture handling uses raw integers for types and index, replace with enum and reference
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
addPicture() with File and InputStream parameters
0001-Consistency-and-convenience-for-adding-pictures-to-p.patch (text/plain), 7.65 KB, created by
mark.o
on 2015-08-04 07:06:46 UTC
(
hide
)
Description:
addPicture() with File and InputStream parameters
Filename:
MIME Type:
Creator:
mark.o
Created:
2015-08-04 07:06:46 UTC
Size:
7.65 KB
patch
obsolete
>From 6804895553a49ea9d27232738f490cd4bc9ebbb0 Mon Sep 17 00:00:00 2001 >From: Mark Olesen <Mark.Olesen@faurecia.com> >Date: Tue, 4 Aug 2015 08:51:53 +0200 >Subject: [PATCH] Consistency and convenience for adding pictures to presentations > >- support direct reading possibilities: > * File (already implemented in HSLFSlideShow) > * InputStream (fully flexibility) > >Reading from a file is probably a very common usage case >-> changed XMLSlideShow tutorial accordingly. >--- > .../org/apache/poi/xslf/usermodel/Tutorial5.java | 9 ++-- > .../org/apache/poi/sl/usermodel/SlideShow.java | 26 ++++++++++- > .../apache/poi/xslf/usermodel/XMLSlideShow.java | 45 ++++++++++++++++++++ > .../apache/poi/hslf/usermodel/HSLFSlideShow.java | 29 +++++++++++-- > 4 files changed, 98 insertions(+), 11 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 ae134de..26eac84 100644 >--- a/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial5.java >+++ b/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial5.java >@@ -19,13 +19,13 @@ > > package org.apache.poi.xslf.usermodel; > >-import org.apache.poi.util.IOUtils; >- > import java.io.File; > import java.io.FileInputStream; > import java.io.FileOutputStream; > import java.io.IOException; >+ > import org.apache.poi.sl.usermodel.PictureData.PictureType; >+import org.apache.poi.util.IOUtils; > > /** > * Images >@@ -39,15 +39,14 @@ public class Tutorial5 { > > 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); >+ XSLFPictureData pictureIndex = ppt.addPicture(img, PictureType.PNG); > > /*XSLFPictureShape shape =*/ slide.createPicture(pictureIndex); > > FileOutputStream out = new FileOutputStream("images.pptx"); > ppt.write(out); > out.close(); >- >+ > ppt.close(); > } > } >diff --git a/src/java/org/apache/poi/sl/usermodel/SlideShow.java b/src/java/org/apache/poi/sl/usermodel/SlideShow.java >index 5ced5b2..b5ea8ab 100644 >--- a/src/java/org/apache/poi/sl/usermodel/SlideShow.java >+++ b/src/java/org/apache/poi/sl/usermodel/SlideShow.java >@@ -18,7 +18,9 @@ > package org.apache.poi.sl.usermodel; > > import java.awt.Dimension; >+import java.io.File; > import java.io.IOException; >+import java.io.InputStream; > import java.util.List; > > import org.apache.poi.sl.usermodel.PictureData; >@@ -48,12 +50,32 @@ public interface SlideShow { > > > /** >- * 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; > } >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 453c1b1..adc8622 100644 >--- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java >+++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java >@@ -17,6 +17,8 @@ > package org.apache.poi.xslf.usermodel; > > import java.awt.Dimension; >+import java.io.File; >+import java.io.FileInputStream; > import java.io.IOException; > import java.io.InputStream; > import java.io.OutputStream; >@@ -463,6 +465,49 @@ public class XMLSlideShow extends POIXMLDocument implements SlideShow { > return img; > } > >+ >+ /** >+ * Adds a picture to the slideshow. >+ * >+ * @param is The stream to read image from >+ * @param type 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 = null; >+ try >+ { >+ is = new FileInputStream(pict); >+ is.read(data); >+ } >+ finally >+ { >+ if (is != null) 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 9f4d311..7bd7bea 100644 >--- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShow.java >+++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShow.java >@@ -50,6 +50,7 @@ import org.apache.poi.sl.usermodel.PictureData.PictureType; > import org.apache.poi.sl.usermodel.Resources; > import org.apache.poi.sl.usermodel.Shape; > 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; >@@ -797,16 +798,36 @@ public final class HSLFSlideShow implements SlideShow { > } > > /** >- * Adds a picture to this presentation and returns the associated index. >+ * 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. >+ */ >+ @Override >+ public HSLFPictureData addPicture(InputStream is, PictureType format) throws IOException { >+ if (format == null || format.nativeId == -1) { // fail early >+ throw new IllegalArgumentException("Unsupported picture format: " + format); >+ } >+ 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. One of constans defined in the >- * <code>Picture</code> 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; >-- >1.7.1 >
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 58190
:
32942
|
32952
| 32954