1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
27 package com
.sun
.star
.report
;
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
;
44 import java
.awt
.Dimension
;
46 import java
.io
.InputStream
;
52 public class SOImageService
implements ImageService
55 private final XGraphicProvider m_xGraphicProvider
;
58 * Creates a new instance of SOImageService
60 * @throws ReportExecutionException
62 public SOImageService(final XComponentContext xCompContext
)
63 throws ReportExecutionException
, com
.sun
.star
.uno
.Exception
65 if (xCompContext
== null)
67 throw new ReportExecutionException();
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
));
75 if (m_xGraphicProvider
== null)
77 throw new ReportExecutionException("There is no graphic-provider available.");
81 public Dimension
getImageSize(final InputStream image
) throws ReportExecutionException
83 return getImageSize(new InputStreamToXInputStreamAdapter(image
));
86 private Dimension
getImageSize(final XInputStream image
) throws ReportExecutionException
88 final Dimension dim
= new Dimension();
91 final PropertyValue
[] value
= new PropertyValue
[]
95 // value[0] = new PropertyValue();
96 value
[0].Name
= "InputStream";
97 value
[0].Value
= image
;
99 final XPropertySet xImage
= (XPropertySet
) UnoRuntime
.queryInterface(XPropertySet
.class,
100 m_xGraphicProvider
.queryGraphic(value
));
104 final XPropertySetInfo xInfo
= xImage
.getPropertySetInfo();
105 if (xInfo
.hasPropertyByName("Size100thMM"))
107 Size imageSize
= (Size
) xImage
.getPropertyValue("Size100thMM");
108 dim
.setSize(imageSize
.Width
, imageSize
.Height
);
109 if (dim
.height
== 0 && dim
.width
== 0)
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
);
117 else if (xInfo
.hasPropertyByName("SizePixel"))
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
);
128 throw new ReportExecutionException("Failed to query Image-Size", ex
);
133 public Dimension
getImageSize(final byte[] image
) throws ReportExecutionException
135 return getImageSize(new ByteArrayToXInputStreamAdapter(image
));
138 private String
getMimeType(final XInputStream image
) throws ReportExecutionException
142 final PropertyValue
[] value
= new PropertyValue
[]
146 value
[0].Name
= "InputStream";
147 value
[0].Value
= image
;
149 final XPropertySet xImage
= (XPropertySet
) UnoRuntime
.queryInterface(XPropertySet
.class,
150 m_xGraphicProvider
.queryGraphic(value
));
154 final XPropertySetInfo xInfo
= xImage
.getPropertySetInfo();
155 if (xInfo
.hasPropertyByName("MimeType"))
157 return (String
) xImage
.getPropertyValue("MimeType");
161 catch (UnknownPropertyException ex
)
163 throw new ReportExecutionException();
165 catch (WrappedTargetException ex
)
167 throw new ReportExecutionException();
169 catch (com
.sun
.star
.lang
.IllegalArgumentException ex
)
171 throw new ReportExecutionException();
173 catch (IOException ex
)
175 throw new ReportExecutionException();
180 public String
getMimeType(final InputStream image
) throws ReportExecutionException
182 return getMimeType(new InputStreamToXInputStreamAdapter(image
));
185 public String
getMimeType(final byte[] image
) throws ReportExecutionException
187 return getMimeType(new ByteArrayToXInputStreamAdapter(image
));