View | Details | Raw Unified | Return to bug 58207
Collapse All | Expand All

(-)a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureData.java (+34 lines)
Lines 19-27 Link Here
19
19
20
package org.apache.poi.xslf.usermodel;
20
package org.apache.poi.xslf.usermodel;
21
21
22
import java.awt.Dimension;
23
import java.awt.image.BufferedImage;
22
import java.io.IOException;
24
import java.io.IOException;
23
import java.io.InputStream;
25
import java.io.InputStream;
24
import java.io.OutputStream;
26
import java.io.OutputStream;
27
import javax.imageio.ImageIO;
25
28
26
import org.apache.poi.POIXMLDocumentPart;
29
import org.apache.poi.POIXMLDocumentPart;
27
import org.apache.poi.POIXMLException;
30
import org.apache.poi.POIXMLException;
Lines 37-42 import org.apache.poi.util.IOUtils; Link Here
37
@Beta
40
@Beta
38
public final class XSLFPictureData extends POIXMLDocumentPart implements PictureData {
41
public final class XSLFPictureData extends POIXMLDocumentPart implements PictureData {
39
    private Long checksum = null;
42
    private Long checksum = null;
43
44
    // original image dimensions (for formats supported by BufferedImage)
45
    private Dimension _origSize = null;
40
    private int index = -1;
46
    private int index = -1;
41
47
42
    /**
48
    /**
Lines 115-120 public final class XSLFPictureData extends POIXMLDocumentPart implements Picture Link Here
115
    }
121
    }
116
122
117
    /**
123
    /**
124
     * Return the original image dimensions
125
     * (for formats supported by BufferedImage).
126
     *
127
     * Will return a Dimension with zero width/height if the format unsupported.
128
     */
129
    public Dimension getImageDimension()
130
    {
131
        if (_origSize == null)
132
        {
133
            try
134
            {
135
                BufferedImage img = ImageIO.read(getInputStream());
136
                _origSize = new Dimension(img.getWidth(), img.getHeight());
137
            }
138
            catch (Exception failed)
139
            {
140
                // failed to get information, set as zero size
141
                _origSize = new Dimension();
142
            }
143
        }
144
145
        return _origSize;
146
    }
147
148
149
    /**
118
     * *PictureData objects store the actual content in the part directly without keeping a
150
     * *PictureData objects store the actual content in the part directly without keeping a
119
     * copy like all others therefore we need to handle them differently.
151
     * copy like all others therefore we need to handle them differently.
120
     */
152
     */
Lines 134-139 public final class XSLFPictureData extends POIXMLDocumentPart implements Picture Link Here
134
        os.close();
166
        os.close();
135
        // recalculate now since we already have the data bytes available anyhow
167
        // recalculate now since we already have the data bytes available anyhow
136
        checksum = IOUtils.calculateChecksum(data);
168
        checksum = IOUtils.calculateChecksum(data);
169
170
        _origSize = null; // need to recalculate image size
137
    }
171
    }
138
172
139
    @Override
173
    @Override
(-)a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java (-13 / +13 lines)
Lines 19-30 Link Here
19
19
20
package org.apache.poi.xslf.usermodel;
20
package org.apache.poi.xslf.usermodel;
21
21
22
import java.awt.Dimension;
22
import java.awt.Insets;
23
import java.awt.Insets;
23
import java.awt.geom.Rectangle2D;
24
import java.awt.Rectangle;
24
import java.awt.image.BufferedImage;
25
import java.net.URI;
25
import java.net.URI;
26
27
import javax.imageio.ImageIO;
28
import javax.xml.namespace.QName;
26
import javax.xml.namespace.QName;
29
27
30
import org.apache.poi.POIXMLException;
28
import org.apache.poi.POIXMLException;
Lines 86-105 public class XSLFPictureShape extends XSLFSimpleShape implements PictureShape { Link Here
86
84
87
    /**
85
    /**
88
     * Resize this picture to the default size.
86
     * Resize this picture to the default size.
87
     *
89
     * For PNG and JPEG resizes the image to 100%,
88
     * For PNG and JPEG resizes the image to 100%,
90
     * for other types sets the default size of 200x200 pixels.
89
     * for other types sets the default size to 200x200 pixels.
91
     */
90
     */
92
    public void resize() {
91
    public void resize() {
93
        try {
92
        Dimension dim = getPictureData().getImageDimension();
94
            BufferedImage img = ImageIO.read(getPictureData().getInputStream());
93
        if (dim.width > 0 && dim.height > 0)
95
            setAnchor(new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight()));
94
        {
95
            setAnchor(new Rectangle(0, 0, dim.width, dim.height));
96
        }
96
        }
97
        catch (Exception e) {
97
        else
98
            //default size is 200x200
98
        {
99
            setAnchor(new java.awt.Rectangle(50, 50, 200, 200));
99
            // unsupported/unknown formats
100
            setAnchor(new Rectangle(50, 50, 200, 200));
100
        }
101
        }
101
    }
102
    }
102
    
103
103
    /**
104
    /**
104
     * Is this an internal picture (image data included within
105
     * Is this an internal picture (image data included within
105
     *  the PowerPoint file), or an external linked picture
106
     *  the PowerPoint file), or an external linked picture
106
- 

Return to bug 58207