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 
28 import com.sun.star.uno.UnoRuntime;
29 import com.sun.star.uno.XComponentContext;
30 import com.sun.star.uno.AnyConverter;
31 import com.sun.star.uno.IQueryInterface;
32 import com.sun.star.lang.XInitialization;
33 import com.sun.star.lang.XEventListener;
34 import com.sun.star.awt.*;
35 import com.sun.star.media.*;
36 import com.sun.star.graphic.*;
37 
38 // -----------------
39 // - Player Window -
40 // -----------------
41 
42 public class FrameGrabber implements com.sun.star.lang.XServiceInfo,
43                                      com.sun.star.media.XFrameGrabber
44 {
45     private com.sun.star.lang.XMultiServiceFactory maFactory = null;
46     private javax.media.Player maPlayer = null;
47     private javax.media.control.FrameGrabbingControl maFrameGrabbingControl = null;
48 
49     // -------------------------------------------------------------------------
50 
51     public FrameGrabber( com.sun.star.lang.XMultiServiceFactory aFactory, String aURL )
52     {
53         maFactory = aFactory;
54 
55         try
56         {
57             maPlayer = javax.media.Manager.createRealizedPlayer( new java.net.URL( aURL ) );
58         }
59         catch( java.net.MalformedURLException e )
60         {
61         }
62         catch( java.io.IOException e )
63         {
64         }
65         catch( javax.media.NoPlayerException e )
66         {
67         }
68         catch( javax.media.CannotRealizeException e )
69         {
70         }
71         catch( java.lang.Exception e )
72         {
73         }
74 
75         if( maPlayer != null )
76         {
77             maFrameGrabbingControl = (javax.media.control.FrameGrabbingControl) maPlayer.getControl(
78                                         "javax.media.control.FrameGrabbingControl" );
79         }
80     }
81 
82     // -------------------------------------------------------------------------
83 
84     public com.sun.star.graphic.XGraphic implImageToXGraphic( java.awt.Image aImage )
85     {
86         com.sun.star.graphic.XGraphic aRet = null;
87 
88         if( maFactory != null && aImage != null )
89         {
90             if( aImage instanceof java.awt.image.BufferedImage )
91             {
92                 java.io.File aTempFile = null;
93 
94                 try
95                 {
96                     aTempFile = java.io.File.createTempFile( "sv0", ".png" );
97 
98                     if( aTempFile.canWrite() )
99                     {
100                         javax.imageio.ImageIO.write( (java.awt.image.BufferedImage) aImage, "png", aTempFile );
101 
102                         com.sun.star.graphic.XGraphicProvider aProvider =
103                             (com.sun.star.graphic.XGraphicProvider) UnoRuntime.queryInterface(
104                                 com.sun.star.graphic.XGraphicProvider.class,
105                                 maFactory.createInstance("com.sun.star.graphic.GraphicProvider") );
106 
107                         if( aProvider != null )
108                         {
109                             com.sun.star.beans.PropertyValue[] aArgs = new com.sun.star.beans.PropertyValue[ 1 ];
110 
111                             aArgs[ 0 ] = new com.sun.star.beans.PropertyValue();
112                             aArgs[ 0 ].Name = "URL";
113                             aArgs[ 0 ].Value = "file://" + aTempFile.toString();
114 
115                             aRet = aProvider.queryGraphic( aArgs );
116                         }
117                     }
118                 }
119                 catch( java.lang.IllegalArgumentException aExcp )
120                 {
121                 }
122                 catch( java.io.IOException aExcp )
123                 {
124                 }
125                 catch( com.sun.star.uno.Exception aExcp )
126                 {
127                 }
128 
129                 if( aTempFile != null )
130                     aTempFile.delete();
131             }
132         }
133 
134         return aRet;
135     }
136 
137     // -----------------
138     // - XFrameGrabber -
139     // -----------------
140 
141     public synchronized com.sun.star.graphic.XGraphic grabFrame( double fMediaTime )
142     {
143         com.sun.star.graphic.XGraphic aRet = null;
144 
145         if( maFrameGrabbingControl != null )
146         {
147             if( fMediaTime >= 0.0 && fMediaTime <= maPlayer.getDuration().getSeconds() )
148             {
149                 maPlayer.setMediaTime( new javax.media.Time( fMediaTime ) );
150 
151                 javax.media.Buffer aBuffer = maFrameGrabbingControl.grabFrame();
152 
153                 if( aBuffer != null && aBuffer.getFormat() instanceof javax.media.format.VideoFormat )
154                 {
155                     aRet = implImageToXGraphic( new javax.media.util.BufferToImage(
156                                                     (javax.media.format.VideoFormat) aBuffer.getFormat() ).
157                                                         createImage( aBuffer ) );
158                 }
159             }
160         }
161 
162         return aRet;
163     }
164 
165     // ----------------
166     // - XServiceInfo -
167     // ----------------
168 
169     private static final String s_implName = "com.sun.star.comp.FrameGrabber_Java";
170     private static final String s_serviceName = "com.sun.star.media.FrameGrabber_Java";
171 
172     public synchronized String getImplementationName()
173     {
174         return s_implName;
175     }
176 
177     // -------------------------------------------------------------------------
178 
179     public synchronized String [] getSupportedServiceNames()
180     {
181         return new String [] { s_serviceName };
182     }
183 
184     // -------------------------------------------------------------------------
185 
186     public synchronized boolean supportsService( String serviceName )
187     {
188         return serviceName.equals( s_serviceName );
189     }
190 }
191