1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 package com.sun.star.report; 28 29 import com.sun.star.awt.Size; 30 import com.sun.star.beans.PropertyValue; 31 import com.sun.star.beans.UnknownPropertyException; 32 import com.sun.star.beans.XPropertySet; 33 import com.sun.star.beans.XPropertySetInfo; 34 import com.sun.star.graphic.XGraphicProvider; 35 import com.sun.star.io.IOException; 36 import com.sun.star.io.XInputStream; 37 import com.sun.star.lang.WrappedTargetException; 38 import com.sun.star.lang.XMultiComponentFactory; 39 import com.sun.star.lib.uno.adapter.ByteArrayToXInputStreamAdapter; 40 import com.sun.star.lib.uno.adapter.InputStreamToXInputStreamAdapter; 41 import com.sun.star.uno.UnoRuntime; 42 import com.sun.star.uno.XComponentContext; 43 44 import java.awt.Dimension; 45 46 import java.io.InputStream; 47 48 49 /** 50 * @author oj93728 51 */ 52 public class SOImageService implements ImageService 53 { 54 55 private final XGraphicProvider m_xGraphicProvider; 56 57 /** 58 * Creates a new instance of SOImageService 59 * @param xCompContext 60 * @throws ReportExecutionException 61 */ 62 public SOImageService(final XComponentContext xCompContext) 63 throws ReportExecutionException, com.sun.star.uno.Exception 64 { 65 if (xCompContext == null) 66 { 67 throw new ReportExecutionException(); 68 } 69 70 71 final XMultiComponentFactory m_xMCF = xCompContext.getServiceManager(); 72 m_xGraphicProvider = (XGraphicProvider) UnoRuntime.queryInterface(XGraphicProvider.class, 73 m_xMCF.createInstanceWithContext("com.sun.star.graphic.GraphicProvider", xCompContext)); 74 75 if (m_xGraphicProvider == null) 76 { 77 throw new ReportExecutionException("There is no graphic-provider available."); 78 } 79 } 80 81 public Dimension getImageSize(final InputStream image) throws ReportExecutionException 82 { 83 return getImageSize(new InputStreamToXInputStreamAdapter(image)); 84 } 85 86 private Dimension getImageSize(final XInputStream image) throws ReportExecutionException 87 { 88 final Dimension dim = new Dimension(); 89 try 90 { 91 final PropertyValue[] value = new PropertyValue[] 92 { 93 new PropertyValue() 94 }; 95 // value[0] = new PropertyValue(); 96 value[0].Name = "InputStream"; 97 value[0].Value = image; 98 99 final XPropertySet xImage = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, 100 m_xGraphicProvider.queryGraphic(value)); 101 102 if (xImage != null) 103 { 104 final XPropertySetInfo xInfo = xImage.getPropertySetInfo(); 105 if (xInfo.hasPropertyByName("Size100thMM")) 106 { 107 Size imageSize = (Size) xImage.getPropertyValue("Size100thMM"); 108 dim.setSize(imageSize.Width, imageSize.Height); 109 if (dim.height == 0 && dim.width == 0) 110 { 111 imageSize = (Size) xImage.getPropertyValue("SizePixel"); 112 final int dpi = java.awt.Toolkit.getDefaultToolkit().getScreenResolution(); 113 final double fac = 2540 / (double) dpi; 114 dim.setSize(imageSize.Width * fac, imageSize.Height * fac); 115 } 116 } 117 else if (xInfo.hasPropertyByName("SizePixel")) 118 { 119 final Size imageSize = (Size) xImage.getPropertyValue("SizePixel"); 120 final int dpi = java.awt.Toolkit.getDefaultToolkit().getScreenResolution(); 121 final double fac = 2540 / dpi; 122 dim.setSize(imageSize.Width * fac, imageSize.Height * fac); 123 } 124 } 125 } 126 catch (Exception ex) 127 { 128 throw new ReportExecutionException("Failed to query Image-Size", ex); 129 } 130 return dim; 131 } 132 133 public Dimension getImageSize(final byte[] image) throws ReportExecutionException 134 { 135 return getImageSize(new ByteArrayToXInputStreamAdapter(image)); 136 } 137 138 private String getMimeType(final XInputStream image) throws ReportExecutionException 139 { 140 try 141 { 142 final PropertyValue[] value = new PropertyValue[] 143 { 144 new PropertyValue() 145 }; 146 value[0].Name = "InputStream"; 147 value[0].Value = image; 148 149 final XPropertySet xImage = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, 150 m_xGraphicProvider.queryGraphic(value)); 151 152 if (xImage != null) 153 { 154 final XPropertySetInfo xInfo = xImage.getPropertySetInfo(); 155 if (xInfo.hasPropertyByName("MimeType")) 156 { 157 return (String) xImage.getPropertyValue("MimeType"); 158 } 159 } 160 } 161 catch (UnknownPropertyException ex) 162 { 163 throw new ReportExecutionException(); 164 } 165 catch (WrappedTargetException ex) 166 { 167 throw new ReportExecutionException(); 168 } 169 catch (com.sun.star.lang.IllegalArgumentException ex) 170 { 171 throw new ReportExecutionException(); 172 } 173 catch (IOException ex) 174 { 175 throw new ReportExecutionException(); 176 } 177 return null; 178 } 179 180 public String getMimeType(final InputStream image) throws ReportExecutionException 181 { 182 return getMimeType(new InputStreamToXInputStreamAdapter(image)); 183 } 184 185 public String getMimeType(final byte[] image) throws ReportExecutionException 186 { 187 return getMimeType(new ByteArrayToXInputStreamAdapter(image)); 188 } 189 } 190