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