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