]> source.dussan.org Git - poi.git/commitdiff
HSSFPicture.resize() - a handy method to reset a picture to its original width and...
authorYegor Kozlov <yegor@apache.org>
Mon, 1 Oct 2007 18:07:32 +0000 (18:07 +0000)
committerYegor Kozlov <yegor@apache.org>
Mon, 1 Oct 2007 18:07:32 +0000 (18:07 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@581029 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/hssf/quick-guide.xml
src/examples/src/org/apache/poi/hssf/usermodel/examples/OfficeDrawing.java
src/java/org/apache/poi/hssf/model/Workbook.java
src/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java
src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java
src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java [new file with mode: 0644]

index f5e396036bdbbebec33103e4ef231890dbae8b16..6c86ed2df688d1ff863c6f31e5cb63c636447031 100644 (file)
@@ -24,6 +24,7 @@
         <title>Busy Developers' Guide to HSSF Features</title>
         <authors>
             <person email="user@poi.apache.org" name="Glen Stampoultzis" id="CO"/>
+            <person email="user@poi.apache.org" name="Yegor Kozlov" id="YK"/>
         </authors>
     </header>
     <body>
     anchor.setAnchorType( 2 );
     patriarch.createPicture(anchor, loadPicture( "src/resources/logos/logoKarmokar4.png", wb ));
   </source>
-  <p>Reading images from a workbook:</p>
+  <p>Creating an image and setting its anchor to the actual width and height:</p>
+      <source>
+    HSSFPatriarch patriarch = sheet5.createDrawingPatriarch();
+
+    HSSFPicture picture = patriarch.createPicture(new HSSFClientAnchor(), loadPicture( "src/resources/logos/logoKarmokar4.png", wb ));
+    picture.resize();
+      </source>
+      <p>or</p> 
+      <source>
+    HSSFPatriarch patriarch = sheet5.createDrawingPatriarch();
+
+    HSSFPicture picture = patriarch.createPicture(new HSSFClientAnchor(), loadPicture( "src/resources/logos/logoKarmokar4.png", wb ));
+    HSSFClientAnchor prefferedSize = picture.getPrefferedSize();
+    picture.setAnchor(prefferedSize);
+      </source>
+        <warning>
+          HSSFPicture.resize() works only for JPEG and PNG. Other formats are not yet supported.
+        </warning>
+
+   <p>Reading images from a workbook:</p>
       <source>
     HSSFWorkbook wb;
 
index ebee3850a431c21a10d552dcb85bea40f914dd9e..21319f873057056444998372b869a82a9f47e34d 100644 (file)
@@ -166,6 +166,8 @@ public class OfficeDrawing
         anchor = new HSSFClientAnchor(0,0,1023,255,(short)6,2,(short)8,7);
         anchor.setAnchorType( 2 );
         HSSFPicture picture = patriarch.createPicture(anchor, loadPicture( "src/resources/logos/logoKarmokar4s.png", wb ));
+        //Reset the image to the original size.
+        picture.resize();
         picture.setLineStyle( picture.LINESTYLE_DASHDOTGEL );
 
     }
index 28970820d9bdd4e0b4441523ad7ab802209c4ce1..972aa09015ceee1beeadb8bfad5d896ff3c0939d 100644 (file)
@@ -2218,7 +2218,12 @@ public class Workbook implements Model
     public WindowOneRecord getWindowOne() {
         return windowOne;
     }
-    
+
+    public EscherBSERecord getBSERecord(int pictureIndex)
+    {
+        return (EscherBSERecord)escherBSERecords.get(pictureIndex-1);
+    }
+
     public int addBSERecord(EscherBSERecord e)
     {
         createDrawingGroup();
index 4dfa7007543f68e69c0969e7a6952404e1ac59c6..e383ed558686a1c55a2b6814c7a927b0e933ed0d 100644 (file)
@@ -90,6 +90,7 @@ public class HSSFPatriarch
         HSSFPicture shape = new HSSFPicture(null, anchor);
         shape.setPictureIndex( pictureIndex );
         shape.anchor = anchor;
+        shape.patriarch = this;
         shapes.add(shape);
         return shape;
     }
index 9425d54d76de73b6409c5e70b319238e3a8efab7..79649b06a8aff295656810c12dd9d88ae3483b30 100644 (file)
 */
 package org.apache.poi.hssf.usermodel;
 
+import org.apache.poi.ddf.EscherBSERecord;
+import org.apache.poi.util.POILogFactory;
+import org.apache.poi.util.POILogger;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+import javax.imageio.ImageIO;
+import javax.imageio.ImageReader;
+import javax.imageio.stream.ImageInputStream;
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.Iterator;
+
 /**
  * Represents a escher picture.  Eg. A GIF, JPEG etc...
  *
  * @author Glen Stampoultzis
- * @version $Id$
+ * @author Yegor Kozlov (yegor at apache.org)
  */
 public class HSSFPicture
         extends HSSFSimpleShape
 {
-    public static final int PICTURE_TYPE_EMF = 0;                // Windows Enhanced Metafile
-    public static final int PICTURE_TYPE_WMF = 1;                // Windows Metafile
-    public static final int PICTURE_TYPE_PICT = 2;               // Macintosh PICT
-    public static final int PICTURE_TYPE_JPEG = 3;               // JFIF
-    public static final int PICTURE_TYPE_PNG = 4;                // PNG
-    public static final int PICTURE_TYPE_DIB = 5;                // Windows DIB
+    public static final int PICTURE_TYPE_EMF = HSSFWorkbook.PICTURE_TYPE_EMF;                // Windows Enhanced Metafile
+    public static final int PICTURE_TYPE_WMF = HSSFWorkbook.PICTURE_TYPE_WMF;                // Windows Metafile
+    public static final int PICTURE_TYPE_PICT = HSSFWorkbook.PICTURE_TYPE_PICT;              // Macintosh PICT
+    public static final int PICTURE_TYPE_JPEG = HSSFWorkbook.PICTURE_TYPE_JPEG;              // JFIF
+    public static final int PICTURE_TYPE_PNG = HSSFWorkbook.PICTURE_TYPE_PNG;                // PNG
+    public static final int PICTURE_TYPE_DIB = HSSFWorkbook.PICTURE_TYPE_DIB;                // Windows DIB
 
     int pictureIndex;
+    HSSFPatriarch patriarch;
+
+    private static final POILogger log = POILogFactory.getLogger(HSSFPicture.class);
 
     /**
      * Constructs a picture object.
@@ -52,4 +69,111 @@ public class HSSFPicture
     {
         this.pictureIndex = pictureIndex;
     }
+
+    /**
+     * Reset the image to the original size.
+     *
+     * @since POI 3.0.2
+     */
+    public void resize(){
+        HSSFClientAnchor anchor = (HSSFClientAnchor)getAnchor();
+        anchor.setAnchorType(2);
+
+        HSSFClientAnchor pref = getPrefferedSize();
+
+        int row2 = anchor.getRow1() + (pref.getRow2() - pref.getRow1());
+        int col2 = anchor.getCol1() + (pref.getCol2() - pref.getCol1());
+
+        anchor.setCol2((short)col2);
+        anchor.setDx1(0);
+        anchor.setDx2(pref.getDx2());
+
+        anchor.setRow2(row2);
+        anchor.setDy1(0);
+        anchor.setDy2(pref.getDy2());
+    }
+
+    /**
+     * Calculate the preffered size for this picture.
+     *
+     * @return HSSFClientAnchor with the preffered size for this image
+     * @since POI 3.0.2
+     */
+    public HSSFClientAnchor getPrefferedSize(){
+        HSSFClientAnchor anchor = new HSSFClientAnchor();
+
+        EscherBSERecord bse = (EscherBSERecord)patriarch.sheet.book.getBSERecord(pictureIndex);
+        byte[] data = bse.getBlipRecord().getPicturedata();
+        int type = bse.getBlipTypeWin32();
+        switch (type){
+            //we can calculate the preffered size only for JPEG and PNG
+            //other formats like WMF, EMF and PICT are not supported in Java
+            case HSSFWorkbook.PICTURE_TYPE_JPEG:
+            case HSSFWorkbook.PICTURE_TYPE_PNG:
+                BufferedImage img = null;
+                ImageReader r = null;
+                try {
+                    //read the image using javax.imageio.*
+                    ImageInputStream iis = ImageIO.createImageInputStream( new ByteArrayInputStream(data) );
+                    Iterator i = ImageIO.getImageReaders( iis );
+                    r = (ImageReader) i.next();
+                    r.setInput( iis );
+                    img = r.read(0);
+
+                    int[] dpi = getResolution(r);
+                    int imgWidth = img.getWidth()*96/dpi[0];
+                    int imgHeight = img.getHeight()*96/dpi[1];
+
+                    //Excel measures cells in units of 1/256th of a character width.
+                    //The cell width calculated based on this info is always "off".
+                    //A better approach seems to be to use empirically obtained cell width and row height
+                    int cellwidth = 64;
+                    int rowheight = 17;
+
+                    int col2 = imgWidth/cellwidth;
+                    int row2 = imgHeight/rowheight;
+
+                    int dx2 = (int)((float)(imgWidth % cellwidth)/cellwidth * 1024);
+                    int dy2 = (int)((float)(imgHeight % rowheight)/rowheight * 256);
+
+                    anchor.setCol2((short)col2);
+                    anchor.setDx2(dx2);
+
+                    anchor.setRow2(row2);
+                    anchor.setDy2(dy2);
+
+                } catch (IOException e){
+                    //silently return if ImageIO failed to read the image
+                    log.log(POILogger.WARN, e);
+                    img = null;
+                }
+
+                break;
+        }
+        return anchor;
+    }
+
+    /**
+     * The metadata of PNG and JPEG can contain the width of a pixel in millimeters.
+     * Return the the "effective" dpi calculated as <code>25.4/HorizontalPixelSize</code>
+     * and <code>25.4/VerticalPixelSize</code>.  Where 25.4 is the number of mm in inch.
+     *
+     * @return array of two elements: <code>{horisontalPdi, verticalDpi}</code>.
+     * {96, 96} is the default.
+     */
+    protected int[] getResolution(ImageReader r) throws IOException {
+        int hdpi=96, vdpi=96;
+        double mm2inch = 25.4;
+
+        NodeList lst;
+        Element node = (Element)r.getImageMetadata(0).getAsTree("javax_imageio_1.0");
+        lst = node.getElementsByTagName("HorizontalPixelSize");
+        if(lst != null && lst.getLength() == 1) hdpi = (int)(mm2inch/Float.parseFloat(((Element)lst.item(0)).getAttribute("value")));
+
+        lst = node.getElementsByTagName("VerticalPixelSize");
+        if(lst != null && lst.getLength() == 1) vdpi = (int)(mm2inch/Float.parseFloat(((Element)lst.item(0)).getAttribute("value")));
+
+        return new int[]{hdpi, vdpi};
+    }
+
 }
index 9d7c287cb822ae3a9f00bc99d79773e41c96066d..069e4efd82040b0a6bcfeae4aa77a4bc85e19594 100644 (file)
@@ -83,8 +83,8 @@ public class HSSFSheet
 
     private Sheet sheet;
     private TreeMap rows;
-    private Workbook book;
-    private HSSFWorkbook workbook;
+    protected Workbook book;
+    protected HSSFWorkbook workbook;
     private int firstrow;
     private int lastrow;
     private static POILogger log = POILogFactory.getLogger(HSSFSheet.class);
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java
new file mode 100644 (file)
index 0000000..45f9264
--- /dev/null
@@ -0,0 +1,79 @@
+/*\r
+* Licensed to the Apache Software Foundation (ASF) under one or more\r
+* contributor license agreements.  See the NOTICE file distributed with\r
+* this work for additional information regarding copyright ownership.\r
+* The ASF licenses this file to You under the Apache License, Version 2.0\r
+* (the "License"); you may not use this file except in compliance with\r
+* the License.  You may obtain a copy of the License at\r
+*\r
+*     http://www.apache.org/licenses/LICENSE-2.0\r
+*\r
+* Unless required by applicable law or agreed to in writing, software\r
+* distributed under the License is distributed on an "AS IS" BASIS,\r
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+* See the License for the specific language governing permissions and\r
+* limitations under the License.\r
+*/\r
+package org.apache.poi.hssf.usermodel;\r
+\r
+import junit.framework.TestCase;\r
+\r
+import java.io.IOException;\r
+import java.io.FileInputStream;\r
+import java.io.ByteArrayOutputStream;\r
+\r
+/**\r
+ * Test <code>HSSFPicture</code>.\r
+ *\r
+ * @author Yegor Kozlov (yegor at apache.org)\r
+ */\r
+public class TestHSSFPicture extends TestCase{\r
+\r
+    public void testResize() throws Exception {\r
+        HSSFWorkbook wb = new HSSFWorkbook();\r
+        HSSFSheet sh1 = wb.createSheet();\r
+        HSSFPatriarch p1 = sh1.createDrawingPatriarch();\r
+\r
+        int idx1 = loadPicture( "src/resources/logos/logoKarmokar4.png", wb);\r
+        HSSFPicture picture1 = p1.createPicture(new HSSFClientAnchor(), idx1);\r
+        HSSFClientAnchor anchor1 = picture1.getPrefferedSize();\r
+\r
+        //assert against what would BiffViewer print if we insert the image in xls and dump the file\r
+        assertEquals(0, anchor1.getCol1());\r
+        assertEquals(0, anchor1.getRow1());\r
+        assertEquals(1, anchor1.getCol2());\r
+        assertEquals(9, anchor1.getRow2());\r
+        assertEquals(0, anchor1.getDx1());\r
+        assertEquals(0, anchor1.getDy1());\r
+        assertEquals(848, anchor1.getDx2());\r
+        assertEquals(240, anchor1.getDy2());\r
+    }\r
+\r
+    /**\r
+     * Copied from org.apache.poi.hssf.usermodel.examples.OfficeDrawing\r
+     */\r
+    private static int loadPicture( String path, HSSFWorkbook wb ) throws IOException\r
+    {\r
+        int pictureIndex;\r
+        FileInputStream fis = null;\r
+        ByteArrayOutputStream bos = null;\r
+        try\r
+        {\r
+            fis = new FileInputStream( path);\r
+            bos = new ByteArrayOutputStream( );\r
+            int c;\r
+            while ( (c = fis.read()) != -1)\r
+                bos.write( c );\r
+            pictureIndex = wb.addPicture( bos.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG );\r
+        }\r
+        finally\r
+        {\r
+            if (fis != null)\r
+                fis.close();\r
+            if (bos != null)\r
+                bos.close();\r
+        }\r
+        return pictureIndex;\r
+    }\r
+\r
+}\r