1*e357d40eSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*e357d40eSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*e357d40eSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*e357d40eSAndrew Rist  * distributed with this work for additional information
6*e357d40eSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*e357d40eSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*e357d40eSAndrew Rist  * "License"); you may not use this file except in compliance
9*e357d40eSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*e357d40eSAndrew Rist  *
11*e357d40eSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*e357d40eSAndrew Rist  *
13*e357d40eSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*e357d40eSAndrew Rist  * software distributed under the License is distributed on an
15*e357d40eSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*e357d40eSAndrew Rist  * KIND, either express or implied.  See the License for the
17*e357d40eSAndrew Rist  * specific language governing permissions and limitations
18*e357d40eSAndrew Rist  * under the License.
19*e357d40eSAndrew Rist  *
20*e357d40eSAndrew Rist  *************************************************************/
21*e357d40eSAndrew Rist 
22*e357d40eSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
25cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
26cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
27cdf0e10cSrcweir import com.sun.star.uno.IQueryInterface;
28cdf0e10cSrcweir import com.sun.star.lang.XInitialization;
29cdf0e10cSrcweir import com.sun.star.lang.XEventListener;
30cdf0e10cSrcweir import com.sun.star.awt.*;
31cdf0e10cSrcweir import com.sun.star.media.*;
32cdf0e10cSrcweir import com.sun.star.graphic.*;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir // -----------------
35cdf0e10cSrcweir // - Player Window -
36cdf0e10cSrcweir // -----------------
37cdf0e10cSrcweir 
38cdf0e10cSrcweir public class FrameGrabber implements com.sun.star.lang.XServiceInfo,
39cdf0e10cSrcweir                                      com.sun.star.media.XFrameGrabber
40cdf0e10cSrcweir {
41cdf0e10cSrcweir     private com.sun.star.lang.XMultiServiceFactory maFactory = null;
42cdf0e10cSrcweir     private javax.media.Player maPlayer = null;
43cdf0e10cSrcweir     private javax.media.control.FrameGrabbingControl maFrameGrabbingControl = null;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir     // -------------------------------------------------------------------------
46cdf0e10cSrcweir 
FrameGrabber( com.sun.star.lang.XMultiServiceFactory aFactory, String aURL )47cdf0e10cSrcweir     public FrameGrabber( com.sun.star.lang.XMultiServiceFactory aFactory, String aURL )
48cdf0e10cSrcweir     {
49cdf0e10cSrcweir         maFactory = aFactory;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir         try
52cdf0e10cSrcweir         {
53cdf0e10cSrcweir             maPlayer = javax.media.Manager.createRealizedPlayer( new java.net.URL( aURL ) );
54cdf0e10cSrcweir         }
55cdf0e10cSrcweir         catch( java.net.MalformedURLException e )
56cdf0e10cSrcweir         {
57cdf0e10cSrcweir         }
58cdf0e10cSrcweir         catch( java.io.IOException e )
59cdf0e10cSrcweir         {
60cdf0e10cSrcweir         }
61cdf0e10cSrcweir         catch( javax.media.NoPlayerException e )
62cdf0e10cSrcweir         {
63cdf0e10cSrcweir         }
64cdf0e10cSrcweir         catch( javax.media.CannotRealizeException e )
65cdf0e10cSrcweir         {
66cdf0e10cSrcweir         }
67cdf0e10cSrcweir         catch( java.lang.Exception e )
68cdf0e10cSrcweir         {
69cdf0e10cSrcweir         }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir         if( maPlayer != null )
72cdf0e10cSrcweir         {
73cdf0e10cSrcweir             maFrameGrabbingControl = (javax.media.control.FrameGrabbingControl) maPlayer.getControl(
74cdf0e10cSrcweir                                         "javax.media.control.FrameGrabbingControl" );
75cdf0e10cSrcweir         }
76cdf0e10cSrcweir     }
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     // -------------------------------------------------------------------------
79cdf0e10cSrcweir 
implImageToXGraphic( java.awt.Image aImage )80cdf0e10cSrcweir     public com.sun.star.graphic.XGraphic implImageToXGraphic( java.awt.Image aImage )
81cdf0e10cSrcweir     {
82cdf0e10cSrcweir         com.sun.star.graphic.XGraphic aRet = null;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir         if( maFactory != null && aImage != null )
85cdf0e10cSrcweir         {
86cdf0e10cSrcweir             if( aImage instanceof java.awt.image.BufferedImage )
87cdf0e10cSrcweir             {
88cdf0e10cSrcweir                 java.io.File aTempFile = null;
89cdf0e10cSrcweir 
90cdf0e10cSrcweir                 try
91cdf0e10cSrcweir                 {
92cdf0e10cSrcweir                     aTempFile = java.io.File.createTempFile( "sv0", ".png" );
93cdf0e10cSrcweir 
94cdf0e10cSrcweir                     if( aTempFile.canWrite() )
95cdf0e10cSrcweir                     {
96cdf0e10cSrcweir                         javax.imageio.ImageIO.write( (java.awt.image.BufferedImage) aImage, "png", aTempFile );
97cdf0e10cSrcweir 
98cdf0e10cSrcweir                         com.sun.star.graphic.XGraphicProvider aProvider =
99cdf0e10cSrcweir                             (com.sun.star.graphic.XGraphicProvider) UnoRuntime.queryInterface(
100cdf0e10cSrcweir                                 com.sun.star.graphic.XGraphicProvider.class,
101cdf0e10cSrcweir                                 maFactory.createInstance("com.sun.star.graphic.GraphicProvider") );
102cdf0e10cSrcweir 
103cdf0e10cSrcweir                         if( aProvider != null )
104cdf0e10cSrcweir                         {
105cdf0e10cSrcweir                             com.sun.star.beans.PropertyValue[] aArgs = new com.sun.star.beans.PropertyValue[ 1 ];
106cdf0e10cSrcweir 
107cdf0e10cSrcweir                             aArgs[ 0 ] = new com.sun.star.beans.PropertyValue();
108cdf0e10cSrcweir                             aArgs[ 0 ].Name = "URL";
109cdf0e10cSrcweir                             aArgs[ 0 ].Value = "file://" + aTempFile.toString();
110cdf0e10cSrcweir 
111cdf0e10cSrcweir                             aRet = aProvider.queryGraphic( aArgs );
112cdf0e10cSrcweir                         }
113cdf0e10cSrcweir                     }
114cdf0e10cSrcweir                 }
115cdf0e10cSrcweir                 catch( java.lang.IllegalArgumentException aExcp )
116cdf0e10cSrcweir                 {
117cdf0e10cSrcweir                 }
118cdf0e10cSrcweir                 catch( java.io.IOException aExcp )
119cdf0e10cSrcweir                 {
120cdf0e10cSrcweir                 }
121cdf0e10cSrcweir                 catch( com.sun.star.uno.Exception aExcp )
122cdf0e10cSrcweir                 {
123cdf0e10cSrcweir                 }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir                 if( aTempFile != null )
126cdf0e10cSrcweir                     aTempFile.delete();
127cdf0e10cSrcweir             }
128cdf0e10cSrcweir         }
129cdf0e10cSrcweir 
130cdf0e10cSrcweir         return aRet;
131cdf0e10cSrcweir     }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir     // -----------------
134cdf0e10cSrcweir     // - XFrameGrabber -
135cdf0e10cSrcweir     // -----------------
136cdf0e10cSrcweir 
grabFrame( double fMediaTime )137cdf0e10cSrcweir     public synchronized com.sun.star.graphic.XGraphic grabFrame( double fMediaTime )
138cdf0e10cSrcweir     {
139cdf0e10cSrcweir         com.sun.star.graphic.XGraphic aRet = null;
140cdf0e10cSrcweir 
141cdf0e10cSrcweir         if( maFrameGrabbingControl != null )
142cdf0e10cSrcweir         {
143cdf0e10cSrcweir             if( fMediaTime >= 0.0 && fMediaTime <= maPlayer.getDuration().getSeconds() )
144cdf0e10cSrcweir             {
145cdf0e10cSrcweir                 maPlayer.setMediaTime( new javax.media.Time( fMediaTime ) );
146cdf0e10cSrcweir 
147cdf0e10cSrcweir                 javax.media.Buffer aBuffer = maFrameGrabbingControl.grabFrame();
148cdf0e10cSrcweir 
149cdf0e10cSrcweir                 if( aBuffer != null && aBuffer.getFormat() instanceof javax.media.format.VideoFormat )
150cdf0e10cSrcweir                 {
151cdf0e10cSrcweir                     aRet = implImageToXGraphic( new javax.media.util.BufferToImage(
152cdf0e10cSrcweir                                                     (javax.media.format.VideoFormat) aBuffer.getFormat() ).
153cdf0e10cSrcweir                                                         createImage( aBuffer ) );
154cdf0e10cSrcweir                 }
155cdf0e10cSrcweir             }
156cdf0e10cSrcweir         }
157cdf0e10cSrcweir 
158cdf0e10cSrcweir         return aRet;
159cdf0e10cSrcweir     }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir     // ----------------
162cdf0e10cSrcweir     // - XServiceInfo -
163cdf0e10cSrcweir     // ----------------
164cdf0e10cSrcweir 
165cdf0e10cSrcweir     private static final String s_implName = "com.sun.star.comp.FrameGrabber_Java";
166cdf0e10cSrcweir     private static final String s_serviceName = "com.sun.star.media.FrameGrabber_Java";
167cdf0e10cSrcweir 
getImplementationName()168cdf0e10cSrcweir     public synchronized String getImplementationName()
169cdf0e10cSrcweir     {
170cdf0e10cSrcweir         return s_implName;
171cdf0e10cSrcweir     }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir     // -------------------------------------------------------------------------
174cdf0e10cSrcweir 
getSupportedServiceNames()175cdf0e10cSrcweir     public synchronized String [] getSupportedServiceNames()
176cdf0e10cSrcweir     {
177cdf0e10cSrcweir         return new String [] { s_serviceName };
178cdf0e10cSrcweir     }
179cdf0e10cSrcweir 
180cdf0e10cSrcweir     // -------------------------------------------------------------------------
181cdf0e10cSrcweir 
supportsService( String serviceName )182cdf0e10cSrcweir     public synchronized boolean supportsService( String serviceName )
183cdf0e10cSrcweir     {
184cdf0e10cSrcweir         return serviceName.equals( s_serviceName );
185cdf0e10cSrcweir     }
186cdf0e10cSrcweir }
187