xref: /AOO41X/main/reportbuilder/java/com/sun/star/report/SOImageService.java (revision 1a37d04709416107a5353584b2c0d44023f0fea0)
1*1a37d047SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*1a37d047SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*1a37d047SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*1a37d047SAndrew Rist  * distributed with this work for additional information
6*1a37d047SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*1a37d047SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*1a37d047SAndrew Rist  * "License"); you may not use this file except in compliance
9*1a37d047SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*1a37d047SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*1a37d047SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*1a37d047SAndrew Rist  * software distributed under the License is distributed on an
15*1a37d047SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*1a37d047SAndrew Rist  * KIND, either express or implied.  See the License for the
17*1a37d047SAndrew Rist  * specific language governing permissions and limitations
18*1a37d047SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*1a37d047SAndrew Rist  *************************************************************/
21*1a37d047SAndrew Rist 
22*1a37d047SAndrew Rist 
23cdf0e10cSrcweir package com.sun.star.report;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.awt.Size;
26cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
27cdf0e10cSrcweir import com.sun.star.beans.UnknownPropertyException;
28cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
29cdf0e10cSrcweir import com.sun.star.beans.XPropertySetInfo;
30cdf0e10cSrcweir import com.sun.star.graphic.XGraphicProvider;
31cdf0e10cSrcweir import com.sun.star.io.IOException;
32cdf0e10cSrcweir import com.sun.star.io.XInputStream;
33cdf0e10cSrcweir import com.sun.star.lang.WrappedTargetException;
34cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
35cdf0e10cSrcweir import com.sun.star.lib.uno.adapter.ByteArrayToXInputStreamAdapter;
36cdf0e10cSrcweir import com.sun.star.lib.uno.adapter.InputStreamToXInputStreamAdapter;
37cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
38cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir import java.awt.Dimension;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir import java.io.InputStream;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir 
45cdf0e10cSrcweir /**
46cdf0e10cSrcweir  * @author oj93728
47cdf0e10cSrcweir  */
48cdf0e10cSrcweir public class SOImageService implements ImageService
49cdf0e10cSrcweir {
50cdf0e10cSrcweir 
51cdf0e10cSrcweir     private final XGraphicProvider m_xGraphicProvider;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir     /**
54cdf0e10cSrcweir      * Creates a new instance of SOImageService
55cdf0e10cSrcweir      * @param xCompContext
56cdf0e10cSrcweir      * @throws ReportExecutionException
57cdf0e10cSrcweir      */
SOImageService(final XComponentContext xCompContext)58cdf0e10cSrcweir     public SOImageService(final XComponentContext xCompContext)
59cdf0e10cSrcweir             throws ReportExecutionException, com.sun.star.uno.Exception
60cdf0e10cSrcweir     {
61cdf0e10cSrcweir         if (xCompContext == null)
62cdf0e10cSrcweir         {
63cdf0e10cSrcweir             throw new ReportExecutionException();
64cdf0e10cSrcweir         }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 
67cdf0e10cSrcweir         final XMultiComponentFactory m_xMCF = xCompContext.getServiceManager();
68cdf0e10cSrcweir         m_xGraphicProvider = (XGraphicProvider) UnoRuntime.queryInterface(XGraphicProvider.class,
69cdf0e10cSrcweir                 m_xMCF.createInstanceWithContext("com.sun.star.graphic.GraphicProvider", xCompContext));
70cdf0e10cSrcweir 
71cdf0e10cSrcweir         if (m_xGraphicProvider == null)
72cdf0e10cSrcweir         {
73cdf0e10cSrcweir             throw new ReportExecutionException("There is no graphic-provider available.");
74cdf0e10cSrcweir         }
75cdf0e10cSrcweir     }
76cdf0e10cSrcweir 
getImageSize(final InputStream image)77cdf0e10cSrcweir     public Dimension getImageSize(final InputStream image) throws ReportExecutionException
78cdf0e10cSrcweir     {
79cdf0e10cSrcweir         return getImageSize(new InputStreamToXInputStreamAdapter(image));
80cdf0e10cSrcweir     }
81cdf0e10cSrcweir 
getImageSize(final XInputStream image)82cdf0e10cSrcweir     private Dimension getImageSize(final XInputStream image) throws ReportExecutionException
83cdf0e10cSrcweir     {
84cdf0e10cSrcweir         final Dimension dim = new Dimension();
85cdf0e10cSrcweir         try
86cdf0e10cSrcweir         {
87cdf0e10cSrcweir             final PropertyValue[] value = new PropertyValue[]
88cdf0e10cSrcweir             {
89cdf0e10cSrcweir                 new PropertyValue()
90cdf0e10cSrcweir             };
91cdf0e10cSrcweir             // value[0] = new PropertyValue();
92cdf0e10cSrcweir             value[0].Name = "InputStream";
93cdf0e10cSrcweir             value[0].Value = image;
94cdf0e10cSrcweir 
95cdf0e10cSrcweir             final XPropertySet xImage = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
96cdf0e10cSrcweir                     m_xGraphicProvider.queryGraphic(value));
97cdf0e10cSrcweir 
98cdf0e10cSrcweir             if (xImage != null)
99cdf0e10cSrcweir             {
100cdf0e10cSrcweir                 final XPropertySetInfo xInfo = xImage.getPropertySetInfo();
101cdf0e10cSrcweir                 if (xInfo.hasPropertyByName("Size100thMM"))
102cdf0e10cSrcweir                 {
103cdf0e10cSrcweir                     Size imageSize = (Size) xImage.getPropertyValue("Size100thMM");
104cdf0e10cSrcweir                     dim.setSize(imageSize.Width, imageSize.Height);
105cdf0e10cSrcweir                     if (dim.height == 0 && dim.width == 0)
106cdf0e10cSrcweir                     {
107cdf0e10cSrcweir                         imageSize = (Size) xImage.getPropertyValue("SizePixel");
108cdf0e10cSrcweir                         final int dpi = java.awt.Toolkit.getDefaultToolkit().getScreenResolution();
109cdf0e10cSrcweir                         final double fac = 2540 / (double) dpi;
110cdf0e10cSrcweir                         dim.setSize(imageSize.Width * fac, imageSize.Height * fac);
111cdf0e10cSrcweir                     }
112cdf0e10cSrcweir                 }
113cdf0e10cSrcweir                 else if (xInfo.hasPropertyByName("SizePixel"))
114cdf0e10cSrcweir                 {
115cdf0e10cSrcweir                     final Size imageSize = (Size) xImage.getPropertyValue("SizePixel");
116cdf0e10cSrcweir                     final int dpi = java.awt.Toolkit.getDefaultToolkit().getScreenResolution();
117cdf0e10cSrcweir                     final double fac = 2540 / dpi;
118cdf0e10cSrcweir                     dim.setSize(imageSize.Width * fac, imageSize.Height * fac);
119cdf0e10cSrcweir                 }
120cdf0e10cSrcweir             }
121cdf0e10cSrcweir         }
122cdf0e10cSrcweir         catch (Exception ex)
123cdf0e10cSrcweir         {
124cdf0e10cSrcweir             throw new ReportExecutionException("Failed to query Image-Size", ex);
125cdf0e10cSrcweir         }
126cdf0e10cSrcweir         return dim;
127cdf0e10cSrcweir     }
128cdf0e10cSrcweir 
getImageSize(final byte[] image)129cdf0e10cSrcweir     public Dimension getImageSize(final byte[] image) throws ReportExecutionException
130cdf0e10cSrcweir     {
131cdf0e10cSrcweir         return getImageSize(new ByteArrayToXInputStreamAdapter(image));
132cdf0e10cSrcweir     }
133cdf0e10cSrcweir 
getMimeType(final XInputStream image)134cdf0e10cSrcweir     private String getMimeType(final XInputStream image) throws ReportExecutionException
135cdf0e10cSrcweir     {
136cdf0e10cSrcweir         try
137cdf0e10cSrcweir         {
138cdf0e10cSrcweir             final PropertyValue[] value = new PropertyValue[]
139cdf0e10cSrcweir             {
140cdf0e10cSrcweir                 new PropertyValue()
141cdf0e10cSrcweir             };
142cdf0e10cSrcweir             value[0].Name = "InputStream";
143cdf0e10cSrcweir             value[0].Value = image;
144cdf0e10cSrcweir 
145cdf0e10cSrcweir             final XPropertySet xImage = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
146cdf0e10cSrcweir                     m_xGraphicProvider.queryGraphic(value));
147cdf0e10cSrcweir 
148cdf0e10cSrcweir             if (xImage != null)
149cdf0e10cSrcweir             {
150cdf0e10cSrcweir                 final XPropertySetInfo xInfo = xImage.getPropertySetInfo();
151cdf0e10cSrcweir                 if (xInfo.hasPropertyByName("MimeType"))
152cdf0e10cSrcweir                 {
153cdf0e10cSrcweir                     return (String) xImage.getPropertyValue("MimeType");
154cdf0e10cSrcweir                 }
155cdf0e10cSrcweir             }
156cdf0e10cSrcweir         }
157cdf0e10cSrcweir         catch (UnknownPropertyException ex)
158cdf0e10cSrcweir         {
159cdf0e10cSrcweir             throw new ReportExecutionException();
160cdf0e10cSrcweir         }
161cdf0e10cSrcweir         catch (WrappedTargetException ex)
162cdf0e10cSrcweir         {
163cdf0e10cSrcweir             throw new ReportExecutionException();
164cdf0e10cSrcweir         }
165cdf0e10cSrcweir         catch (com.sun.star.lang.IllegalArgumentException ex)
166cdf0e10cSrcweir         {
167cdf0e10cSrcweir             throw new ReportExecutionException();
168cdf0e10cSrcweir         }
169cdf0e10cSrcweir         catch (IOException ex)
170cdf0e10cSrcweir         {
171cdf0e10cSrcweir             throw new ReportExecutionException();
172cdf0e10cSrcweir         }
173cdf0e10cSrcweir         return null;
174cdf0e10cSrcweir     }
175cdf0e10cSrcweir 
getMimeType(final InputStream image)176cdf0e10cSrcweir     public String getMimeType(final InputStream image) throws ReportExecutionException
177cdf0e10cSrcweir     {
178cdf0e10cSrcweir         return getMimeType(new InputStreamToXInputStreamAdapter(image));
179cdf0e10cSrcweir     }
180cdf0e10cSrcweir 
getMimeType(final byte[] image)181cdf0e10cSrcweir     public String getMimeType(final byte[] image) throws ReportExecutionException
182cdf0e10cSrcweir     {
183cdf0e10cSrcweir         return getMimeType(new ByteArrayToXInputStreamAdapter(image));
184cdf0e10cSrcweir     }
185cdf0e10cSrcweir }
186