FileDocCategorySizeDatePackage
Picture.javaAPI DocApache Poi 3.0.16612Thu May 31 18:45:28 BST 2007org.apache.poi.hslf.model

Picture

public class Picture extends SimpleShape
Represents a picture in a PowerPoint document.
author
Yegor Kozlov

Fields Summary
public static final int
EMF
Windows Enhanced Metafile (EMF)
public static final int
WMF
Windows Metafile (WMF)
public static final int
PICT
Macintosh PICT
public static final int
JPEG
JPEG
public static final int
PNG
PNG
public static final byte
DIB
Windows DIB (BMP)
Constructors Summary
public Picture(int idx)
Create a new Picture

param
idx the index of the picture

    
                    
      
        super(null, null);
        _escherContainer = createSpContainer(idx);
    
protected Picture(EscherContainerRecord escherRecord, Shape parent)
Create a Picture object

param
escherRecord the EscherSpContainer record which holds information about this picture in the Slide
param
parent the parent shape of this picture

        super(escherRecord, parent);
    
Methods Summary
protected voidafterInsert(org.apache.poi.hslf.model.Sheet sh)
By default set the orininal image size

        java.awt.Rectangle anchor = getAnchor();
        if (anchor.equals(new java.awt.Rectangle())){
            setDefaultSize();
        }
    
protected org.apache.poi.ddf.EscherContainerRecordcreateSpContainer(int idx)
Create a new Picture and populate the inital structure of the EscherSp record which holds information about this picture.

param
idx the index of the picture which referes to EscherBSE container.
return
the create Picture object

        EscherContainerRecord spContainer = super.createSpContainer(false);
        spContainer.setOptions((short)15);

        EscherSpRecord spRecord = spContainer.getChildById(EscherSpRecord.RECORD_ID);
        spRecord.setOptions((short)((ShapeTypes.PictureFrame << 4) | 0x2));

        //set default properties for a picture
        EscherOptRecord opt = (EscherOptRecord)getEscherChild(spContainer, EscherOptRecord.RECORD_ID);
        setEscherProperty(opt, EscherProperties.PROTECTION__LOCKAGAINSTGROUPING, 8388736);

        //another weird feature of powerpoint: for picture id we must add 0x4000.
        setEscherProperty(opt, (short)(EscherProperties.BLIP__BLIPTODISPLAY + 0x4000), idx);

        return spContainer;
    
public org.apache.poi.hslf.usermodel.PictureDatagetPictureData()
Returns the picture data for this picture.

return
the picture data for this picture.

        SlideShow ppt = getSheet().getSlideShow();
        PictureData[] pict = ppt.getPictureData();
        Document doc = ppt.getDocumentRecord();
        EscherContainerRecord dggContainer = doc.getPPDrawingGroup().getDggContainer();
        EscherContainerRecord bstore = (EscherContainerRecord)Shape.getEscherChild(dggContainer, EscherContainerRecord.BSTORE_CONTAINER);

        List lst = bstore.getChildRecords();
        int idx = getPictureIndex();
        if (idx == 0){
            logger.log(POILogger.ERROR, "no reference to picture data found ");
        } else {
            EscherBSERecord bse = (EscherBSERecord)lst.get(idx-1);
            for ( int i = 0; i < pict.length; i++ ) {
                if (pict[i].getOffset() ==  bse.getOffset()){
                    return pict[i];
                }
            }
            logger.log(POILogger.ERROR, "no picture found for our BSE offset " + bse.getOffset());
        }
        return null;
    
public intgetPictureIndex()
Returns index associated with this picture. Index starts with 1 and points to a EscherBSE record which holds information about this picture.

return
the index to this picture (1 based).

        EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
        EscherSimpleProperty prop = (EscherSimpleProperty)getEscherProperty(opt, EscherProperties.BLIP__BLIPTODISPLAY + 0x4000);
        return prop == null ? 0 : prop.getPropertyValue();
    
public voidsetDefaultSize()
Resize this picture to the default size. For PNG and JPEG resizes the image to 100%, for other types sets the default size of 200x200 pixels.

        PictureData pict = getPictureData();
        if (pict  instanceof Bitmap){
            BufferedImage img = null;
            try {
               	img = ImageIO.read(new ByteArrayInputStream(pict.getData()));
            } 
            catch (IOException e){}
        	catch (NegativeArraySizeException ne) {}
        	
            if(img != null) {
            	// Valid image, set anchor from it
            	setAnchor(new java.awt.Rectangle(0, 0, img.getWidth()*POINT_DPI/PIXEL_DPI, img.getHeight()*POINT_DPI/PIXEL_DPI));
            } else {
            	// Invalid image, go with the default metafile size
            	setAnchor(new java.awt.Rectangle(0, 0, 200, 200));
            }
        } else {
            //default size of a metafile picture is 200x200 
            setAnchor(new java.awt.Rectangle(50, 50, 200, 200));
        }