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
This commit is contained in:
parent
3d60c67151
commit
e48e4e771d
@ -38,11 +38,11 @@ public class Tutorial5 {
|
|||||||
XMLSlideShow ppt = new XMLSlideShow();
|
XMLSlideShow ppt = new XMLSlideShow();
|
||||||
|
|
||||||
XSLFSlide slide = ppt.createSlide();
|
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");
|
FileOutputStream out = new FileOutputStream("images.pptx");
|
||||||
ppt.write(out);
|
ppt.write(out);
|
||||||
|
@ -18,9 +18,7 @@
|
|||||||
package org.apache.poi.sl.usermodel;
|
package org.apache.poi.sl.usermodel;
|
||||||
|
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
import java.io.Closeable;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.poi.sl.usermodel.PictureData.PictureType;
|
import org.apache.poi.sl.usermodel.PictureData.PictureType;
|
||||||
@ -64,17 +62,36 @@ public interface SlideShow<
|
|||||||
*/
|
*/
|
||||||
List<? extends PictureData> getPictureData();
|
List<? extends PictureData> getPictureData();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a picture to the workbook.
|
* Adds a picture to the presentation.
|
||||||
*
|
*
|
||||||
* @param pictureData The bytes of the picture
|
* @param pictureData The bytes of the picture
|
||||||
* @param format The format 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;
|
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
|
* Writes out the slideshow file the is represented by an instance of this
|
||||||
* class
|
* class
|
||||||
|
@ -19,6 +19,8 @@ package org.apache.poi.xslf.usermodel;
|
|||||||
import static org.apache.poi.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
|
import static org.apache.poi.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
|
||||||
|
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
@ -202,7 +204,7 @@ implements SlideShow<XSLFShape,XSLFTextParagraph> {
|
|||||||
/**
|
/**
|
||||||
* Create a slide and initialize it from the specified layout.
|
* 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
|
* @return created slide
|
||||||
*/
|
*/
|
||||||
public XSLFSlide createSlide(XSLFSlideLayout layout) {
|
public XSLFSlide createSlide(XSLFSlideLayout layout) {
|
||||||
@ -461,6 +463,45 @@ implements SlideShow<XSLFShape,XSLFTextParagraph> {
|
|||||||
return img;
|
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
|
* check if a picture with this picture data already exists in this presentation
|
||||||
*/
|
*/
|
||||||
|
@ -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.PictureData.PictureType;
|
||||||
import org.apache.poi.sl.usermodel.Resources;
|
import org.apache.poi.sl.usermodel.Resources;
|
||||||
import org.apache.poi.sl.usermodel.SlideShow;
|
import org.apache.poi.sl.usermodel.SlideShow;
|
||||||
|
import org.apache.poi.util.IOUtils;
|
||||||
import org.apache.poi.util.POILogFactory;
|
import org.apache.poi.util.POILogFactory;
|
||||||
import org.apache.poi.util.POILogger;
|
import org.apache.poi.util.POILogger;
|
||||||
import org.apache.poi.util.Units;
|
import org.apache.poi.util.Units;
|
||||||
@ -268,10 +269,10 @@ public final class HSLFSlideShow implements SlideShow<HSLFShape,HSLFTextParagrap
|
|||||||
_documentRecord = (Document) record;
|
_documentRecord = (Document) record;
|
||||||
_fonts = _documentRecord.getEnvironment().getFontCollection();
|
_fonts = _documentRecord.getEnvironment().getFontCollection();
|
||||||
}
|
}
|
||||||
} else {
|
} /*else {
|
||||||
// No record at this number
|
// No record at this number
|
||||||
// Odd, but not normally a problem
|
// Odd, but not normally a problem
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -295,8 +296,7 @@ public final class HSLFSlideShow implements SlideShow<HSLFShape,HSLFTextParagrap
|
|||||||
private Record getCoreRecordForRefID(int refID) {
|
private Record getCoreRecordForRefID(int refID) {
|
||||||
Integer coreRecordId = _sheetIdToCoreRecordsLookup.get(refID);
|
Integer coreRecordId = _sheetIdToCoreRecordsLookup.get(refID);
|
||||||
if (coreRecordId != null) {
|
if (coreRecordId != null) {
|
||||||
Record r = _mostRecentCoreRecords[coreRecordId];
|
return _mostRecentCoreRecords[coreRecordId];
|
||||||
return r;
|
|
||||||
}
|
}
|
||||||
logger.log(POILogger.ERROR,
|
logger.log(POILogger.ERROR,
|
||||||
"We tried to look up a reference to a core record, but there was no core ID for reference ID "
|
"We tried to look up a reference to a core record, but there was no core ID for reference ID "
|
||||||
@ -705,12 +705,10 @@ public final class HSLFSlideShow implements SlideShow<HSLFShape,HSLFTextParagrap
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public HSLFSlide createSlide() {
|
public HSLFSlide createSlide() {
|
||||||
SlideListWithText slist = null;
|
|
||||||
|
|
||||||
// We need to add the records to the SLWT that deals
|
// We need to add the records to the SLWT that deals
|
||||||
// with Slides.
|
// with Slides.
|
||||||
// Add it, if it doesn't exist
|
// Add it, if it doesn't exist
|
||||||
slist = _documentRecord.getSlideSlideListWithText();
|
SlideListWithText slist = _documentRecord.getSlideSlideListWithText();
|
||||||
if (slist == null) {
|
if (slist == null) {
|
||||||
// Need to add a new one
|
// Need to add a new one
|
||||||
slist = new SlideListWithText();
|
slist = new SlideListWithText();
|
||||||
@ -828,24 +826,43 @@ public final class HSLFSlideShow implements SlideShow<HSLFShape,HSLFTextParagrap
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
* @param pict
|
||||||
* the file containing the image to add
|
* the file containing the image to add
|
||||||
* @param format
|
* @param format
|
||||||
* the format of the picture. One of constans defined in the
|
* The format of the picture.
|
||||||
* <code>Picture</code> class.
|
*
|
||||||
* @return the index to this picture (1 based).
|
* @return the picture data.
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public HSLFPictureData addPicture(File pict, PictureType format) throws IOException {
|
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();
|
int length = (int) pict.length();
|
||||||
byte[] data = new byte[length];
|
byte[] data = new byte[length];
|
||||||
FileInputStream is = null;
|
FileInputStream is = new FileInputStream(pict);
|
||||||
try {
|
try {
|
||||||
is = new FileInputStream(pict);
|
IOUtils.readFully(is, data);
|
||||||
is.read(data);
|
|
||||||
} finally {
|
} finally {
|
||||||
if(is != null) is.close();
|
is.close();
|
||||||
}
|
}
|
||||||
return addPicture(data, format);
|
return addPicture(data, format);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user