Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* ====================================================================
     Licensed to the Apache Software Foundation (ASF) under one or more
     contributor license agreements.  See the NOTICE file distributed with
     this work for additional information regarding copyright ownership.
     The ASF licenses this file to You under the Apache License, Version 2.0
     (the "License"); you may not use this file except in compliance with
     the License.  You may obtain a copy of the License at
  
         http://www.apache.org/licenses/LICENSE-2.0
 
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 ==================================================================== */
 
 package org.apache.poi.hslf.model;
 
 
 import java.awt.*;
 import java.util.List;


Represents a picture in a PowerPoint document.

Author(s):
Yegor Kozlov
 
 public class Picture extends SimpleShape {

    
Windows Enhanced Metafile (EMF)
 
     public static final int EMF = 2;

    
Windows Metafile (WMF)
 
     public static final int WMF = 3;

    
Macintosh PICT
 
     public static final int PICT = 4;

    
JPEG
 
     public static final int JPEG = 5;

    
PNG
 
     public static final int PNG = 6;

    
Windows DIB (BMP)
 
     public static final byte DIB = 7;

    
Create a new Picture

Parameters:
idx the index of the picture
 
     public Picture(int idx){
         this(idxnull);
     }

    
Create a new Picture

Parameters:
idx the index of the picture
parent the parent shape
 
     public Picture(int idxShape parent) {
         super(nullparent);
          = createSpContainer(idxparent instanceof ShapeGroup);
     }

    
Create a Picture object

Parameters:
escherRecord the EscherSpContainer record which holds information about this picture in the Slide
parent the parent shape of this picture
     protected Picture(EscherContainerRecord escherRecordShape parent){
        super(escherRecordparent);
    }

    
Returns index associated with this picture. Index starts with 1 and points to a EscherBSE record which holds information about this picture.

Returns:
the index to this picture (1 based).
    public int getPictureIndex(){
        return prop == null ? 0 : prop.getPropertyValue();
    }

    
Create a new Picture and populate the inital structure of the EscherSp record which holds information about this picture.

Parameters:
idx the index of the picture which refers to EscherBSE container.
Returns:
the create Picture object
    protected EscherContainerRecord createSpContainer(int idxboolean isChild) {
         = super.createSpContainer(isChild);
        .setOptions((short)15);
        spRecord.setOptions((short)((. << 4) | 0x2));
        //set default properties for a picture
        //another weird feature of powerpoint: for picture id we must add 0x4000.
        setEscherProperty(opt, (short)(. + 0x4000), idx);
        return ;
    }

    
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.
    public void setDefaultSize(){
        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()*/img.getHeight()*/));
            } 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));
        }
    }

    
Returns the picture data for this picture.

Returns:
the picture data for this picture.
    public PictureData getPictureData(){
        SlideShow ppt = getSheet().getSlideShow();
        PictureData[] pict = ppt.getPictureData();
        EscherBSERecord bse = getEscherBSERecord();
        if (bse == null){
            .log(."no reference to picture data found ");
        } else {
            for ( int i = 0; i < pict.lengthi++ ) {
                if (pict[i].getOffset() ==  bse.getOffset()){
                    return pict[i];
                }
            }
            .log(."no picture found for our BSE offset " + bse.getOffset());
        }
        return null;
    }
    protected EscherBSERecord getEscherBSERecord(){
        SlideShow ppt = getSheet().getSlideShow();
        Document doc = ppt.getDocumentRecord();
        EscherContainerRecord dggContainer = doc.getPPDrawingGroup().getDggContainer();
        if(bstore == null) {
            .log(."EscherContainerRecord.BSTORE_CONTAINER was not found ");
            return null;
        }
        List lst = bstore.getChildRecords();
        int idx = getPictureIndex();
        if (idx == 0){
            .log(."picture index was not found, returning ");
            return null;
        }
        return (EscherBSERecord)lst.get(idx-1);
    }

    
Name of this picture.

Returns:
name of this picture
    public String getPictureName(){
        String name = null;
        if(prop != null){
            try {
                name = new String(prop.getComplexData(), "UTF-16LE");
                int idx = name.indexOf('\u0000');
                return idx == -1 ? name : name.substring(0, idx);
            } catch (UnsupportedEncodingException e){
                throw new HSLFException(e);
            }
        }
        return name;
    }

    
Name of this picture.

Parameters:
name of this picture
    public void setPictureName(String name){
        try {
            byte[] data = (name + '\u0000').getBytes("UTF-16LE");
            EscherComplexProperty prop = new EscherComplexProperty(.falsedata);
            opt.addEscherProperty(prop);
        } catch (UnsupportedEncodingException e){
            throw new HSLFException(e);
        }
    }

    
By default set the orininal image size
    protected void afterInsert(Sheet sh){
        super.afterInsert(sh);
        EscherBSERecord bse = getEscherBSERecord();
        bse.setRef(bse.getRef() + 1);
        java.awt.Rectangle anchor = getAnchor();
        if (anchor.equals(new java.awt.Rectangle())){
            setDefaultSize();
        }
    }
    public void draw(Graphics2D graphics){
        AffineTransform at = graphics.getTransform();
        ShapePainter.paint(thisgraphics);
        PictureData data = getPictureData();
        if(data != nulldata.draw(graphicsthis);
        graphics.setTransform(at);
    }
New to GrepCode? Check out our FAQ X