support caching images in xslf
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1241896 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
583a2eb315
commit
83b3ab6124
@ -19,17 +19,10 @@ package org.apache.poi.xslf.usermodel;
|
|||||||
import org.apache.poi.POIXMLDocument;
|
import org.apache.poi.POIXMLDocument;
|
||||||
import org.apache.poi.POIXMLDocumentPart;
|
import org.apache.poi.POIXMLDocumentPart;
|
||||||
import org.apache.poi.POIXMLException;
|
import org.apache.poi.POIXMLException;
|
||||||
|
import org.apache.poi.POIXMLRelation;
|
||||||
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
|
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
|
||||||
import org.apache.poi.openxml4j.opc.OPCPackage;
|
import org.apache.poi.openxml4j.opc.*;
|
||||||
import org.apache.poi.openxml4j.opc.PackagePart;
|
import org.apache.poi.util.*;
|
||||||
import org.apache.poi.openxml4j.opc.PackagePartName;
|
|
||||||
import org.apache.poi.openxml4j.opc.TargetMode;
|
|
||||||
import org.apache.poi.util.Beta;
|
|
||||||
import org.apache.poi.util.Internal;
|
|
||||||
import org.apache.poi.util.POILogFactory;
|
|
||||||
import org.apache.poi.util.POILogger;
|
|
||||||
import org.apache.poi.util.PackageHelper;
|
|
||||||
import org.apache.poi.util.Units;
|
|
||||||
import org.apache.poi.xslf.XSLFSlideShow;
|
import org.apache.poi.xslf.XSLFSlideShow;
|
||||||
import org.apache.xmlbeans.XmlException;
|
import org.apache.xmlbeans.XmlException;
|
||||||
import org.apache.xmlbeans.XmlObject;
|
import org.apache.xmlbeans.XmlObject;
|
||||||
@ -338,11 +331,13 @@ public class XMLSlideShow extends POIXMLDocument {
|
|||||||
* @see XSLFPictureData#PICTURE_TYPE_DIB
|
* @see XSLFPictureData#PICTURE_TYPE_DIB
|
||||||
*/
|
*/
|
||||||
public int addPicture(byte[] pictureData, int format) {
|
public int addPicture(byte[] pictureData, int format) {
|
||||||
getAllPictures();
|
XSLFPictureData img = findPictureData(pictureData);
|
||||||
|
POIXMLRelation relDesc = XSLFPictureData.RELATIONS[format];
|
||||||
|
|
||||||
int imageNumber = _pictures.size() + 1;
|
if(img == null) {
|
||||||
XSLFPictureData img = (XSLFPictureData) createRelationship(
|
int imageNumber = _pictures.size();
|
||||||
XSLFPictureData.RELATIONS[format], XSLFFactory.getInstance(), imageNumber, true);
|
img = (XSLFPictureData) createRelationship(
|
||||||
|
XSLFPictureData.RELATIONS[format], XSLFFactory.getInstance(), imageNumber + 1, true);
|
||||||
_pictures.add(img);
|
_pictures.add(img);
|
||||||
try {
|
try {
|
||||||
OutputStream out = img.getPackagePart().getOutputStream();
|
OutputStream out = img.getPackagePart().getOutputStream();
|
||||||
@ -351,8 +346,25 @@ public class XMLSlideShow extends POIXMLDocument {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new POIXMLException(e);
|
throw new POIXMLException(e);
|
||||||
}
|
}
|
||||||
return imageNumber - 1;
|
return _pictures.size() - 1;
|
||||||
|
} else {
|
||||||
|
return _pictures.indexOf(img);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* check if a picture with this picture data already exists in this presentation
|
||||||
|
*/
|
||||||
|
XSLFPictureData findPictureData(byte[] pictureData){
|
||||||
|
long checksum = IOUtils.calculateChecksum(pictureData);
|
||||||
|
for(XSLFPictureData pic : getAllPictures()){
|
||||||
|
if(pic.getChecksum() == checksum) {
|
||||||
|
return pic;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public XSLFTableStyles getTableStyles(){
|
public XSLFTableStyles getTableStyles(){
|
||||||
return _tableStyles;
|
return _tableStyles;
|
||||||
}
|
}
|
||||||
|
@ -168,4 +168,11 @@ public final class XSLFPictureData extends POIXMLDocumentPart {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long getChecksum(){
|
||||||
|
if(checksum == null){
|
||||||
|
byte[] pictureData = getData();
|
||||||
|
checksum = IOUtils.calculateChecksum(pictureData);
|
||||||
|
}
|
||||||
|
return checksum;
|
||||||
|
}
|
||||||
}
|
}
|
@ -113,4 +113,21 @@ public class TestXSLFPictureShape extends TestCase {
|
|||||||
assertTrue(Arrays.equals(data1, data.getData()));
|
assertTrue(Arrays.equals(data1, data.getData()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testImageCaching() {
|
||||||
|
XMLSlideShow ppt = new XMLSlideShow();
|
||||||
|
byte[] img1 = new byte[]{1,2,3};
|
||||||
|
byte[] img2 = new byte[]{3,4,5};
|
||||||
|
int idx1 = ppt.addPicture(img1, XSLFPictureData.PICTURE_TYPE_PNG);
|
||||||
|
assertEquals(0, idx1);
|
||||||
|
assertEquals(0, ppt.addPicture(img1, XSLFPictureData.PICTURE_TYPE_PNG));
|
||||||
|
|
||||||
|
int idx2 = ppt.addPicture(img2, XSLFPictureData.PICTURE_TYPE_PNG);
|
||||||
|
assertEquals(1, idx2);
|
||||||
|
assertEquals(1, ppt.addPicture(img2, XSLFPictureData.PICTURE_TYPE_PNG));
|
||||||
|
|
||||||
|
XSLFSlide slide1 = ppt.createSlide();
|
||||||
|
XSLFSlide slide2 = ppt.createSlide();
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user