1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
| package cam;
import java.io.*;
import javax.media.*;
import javax.media.control.*;
import javax.media.datasink.*;
import javax.media.format.*;
import javax.media.protocol.*;
public class Camera {
private static boolean debugDeviceList = false;
private static String defaultVideoDeviceName = "v4l:Logitech QuickCam Messenger :0";
private static String defaultAudioDeviceName = "JavaSound audio capture";
private static String defaultVideoFormatString = "size=320x240, encoding=rgb, maxdatalength=230400";
private static String defaultAudioFormatString = "linear, 44100.0 hz, 16-bit, stereo, littleendian, signed";
private static CaptureDeviceInfo captureVideoDevice = null;
private static CaptureDeviceInfo captureAudioDevice = null;
private static VideoFormat captureVideoFormat = null;
private static AudioFormat captureAudioFormat = null;
public static void main(String args[])
{
// get command line arguments
for (int x = 0; x < args.length; x++)
{
// -dd = debug devices list -> display list of all media devices - and exit
if (args[x].toLowerCase().compareTo("-dd") == 0)
debugDeviceList = true;
Stdout.log("Debug Mode");
}
// get a list of all media devices, search default devices and formats, and print it out if args[x] = "-dd"
// --------------------------------------------------------------------------------------------------------
Stdout.log("Affichage des Medias Disponibles sur le PC...");
java.util.Vector deviceListVector = CaptureDeviceManager.getDeviceList(null);
if (deviceListVector == null)
{
Stdout.log("... error: media device list vector is null, program aborted");
System.exit(0);
}
if (deviceListVector.size() == 0)
{
Stdout.log("... error: media device list vector size is 0, program aborted");
System.exit(0);
}
for (int x = 0; x < deviceListVector.size(); x++)
{
// display device name
CaptureDeviceInfo deviceInfo = (CaptureDeviceInfo) deviceListVector.elementAt(x);
String deviceInfoText = deviceInfo.getName();
if (debugDeviceList)
Stdout.log("device " + x + ": " + deviceInfoText);
// display device formats
Format deviceFormat[] = deviceInfo.getFormats();
for (int y = 0; y < deviceFormat.length; y++){
// serach for default video device
if (captureVideoDevice == null)
if (deviceFormat[y] instanceof VideoFormat)
if (deviceInfo.getName().indexOf(defaultVideoDeviceName) >= 0){
captureVideoDevice = deviceInfo;
Stdout.log(">>> capture video device = " + deviceInfo.getName());
}
// search for default video format
if (captureVideoDevice == deviceInfo)
if (captureVideoFormat == null)
if (DeviceInfo.formatToString(deviceFormat[y]).indexOf(defaultVideoFormatString) >= 0){
captureVideoFormat = (VideoFormat) deviceFormat[y];
Stdout.log(">>> capture video format = " + DeviceInfo.formatToString(deviceFormat[y]));
}
// serach for default audio device
if (captureAudioDevice == null)
if (deviceFormat[y] instanceof AudioFormat)
if (deviceInfo.getName().indexOf(defaultAudioDeviceName) >= 0){
captureAudioDevice = deviceInfo;
Stdout.log(">>> capture audio device = " + deviceInfo.getName());
}
// search for default audio format
if (captureAudioDevice == deviceInfo)
if (captureAudioFormat == null)
if (DeviceInfo.formatToString(deviceFormat[y]).indexOf(defaultAudioFormatString) >= 0){
captureAudioFormat = (AudioFormat) deviceFormat[y];
Stdout.log(">>> capture audio format = " + DeviceInfo.formatToString(deviceFormat[y]));
}
if (debugDeviceList)
Stdout.log(" - format: " + DeviceInfo.formatToString(deviceFormat[y]));
}
}
Stdout.log("... list completed.");
// if args[x] = "-dd" terminate now
// --------------------------------
if (debugDeviceList)
System.exit(0);
// setup video data source
// -----------------------
Stdout.log("Create Data Source Video");
MediaLocator videoMediaLocator = captureVideoDevice.getLocator();
DataSource videoDataSource = null;
try
{
videoDataSource = javax.media.Manager.createDataSource(videoMediaLocator);
}
catch (IOException ie) {
Stdout.log("Erreur d'IO pour Video");
ie.printStackTrace();
System.exit(0);
}
catch (NoDataSourceException nse) {
Stdout.log("Erreur de DataSource non Dispo Video");
nse.printStackTrace();
System.exit(0);
}
if (! DeviceInfo.setFormat(videoDataSource, captureVideoFormat))
{
Stdout.log("Error: unable to set video format - program aborted");
System.exit(0);
}
Stdout.log("Create Data source audio");
// setup audio data source
// -----------------------
MediaLocator audioMediaLocator = captureAudioDevice.getLocator();
DataSource audioDataSource = null;
try
{
audioDataSource = javax.media.Manager.createDataSource(audioMediaLocator);
}
catch (IOException ie) {
Stdout.log("Erreur d'IO pour Audio");
//Stdout.logAndAbortException(ie);
ie.toString();
System.exit(0);
}
catch (NoDataSourceException nse) {
Stdout.log("Erreur de DataSource non Dispo pour Audio");
nse.toString();
System.exit(0);
//Stdout.logAndAbortException(nse);
}
if (! DeviceInfo.setFormat(audioDataSource, captureAudioFormat))
{
Stdout.log("Error: unable to set audio format - program aborted");
System.exit(0);
}
Stdout.log("Merge Data source");
// merge the two data sources
// --------------------------
DataSource mixedDataSource = null;
try
{
DataSource dArray[] = new DataSource[2];
dArray[0] = videoDataSource;
dArray[1] = audioDataSource;
mixedDataSource = javax.media.Manager.createMergingDataSource(dArray);
}
catch (IncompatibleSourceException ise) { Stdout.logAndAbortException(ise); }
// create a new processor
// ----------------------
// setup output file format ->> msvideo
FileTypeDescriptor outputType = new FileTypeDescriptor(FileTypeDescriptor.MSVIDEO);
// setup output video and audio data format
Format outputFormat[] = new Format[2];
outputFormat[0] = new VideoFormat(VideoFormat.INDEO50);
outputFormat[1] = new AudioFormat(AudioFormat.GSM_MS /* LINEAR */);
// create processor
Stdout.log("Create Realized Processor");
//on tente de faire juste videoDataSource
ProcessorModel processorModel = new ProcessorModel(videoDataSource, outputFormat, outputType);
//ProcessorModel processorModel = new ProcessorModel(mixedDataSource, outputFormat, outputType);
Processor processor = null;
try
{
processor = Manager.createRealizedProcessor(processorModel);
}
catch (IOException e) { Stdout.logAndAbortException(e); }
catch (NoProcessorException e) { Stdout.logAndAbortException(e); }
catch (CannotRealizeException e) {
e.printStackTrace();
Stdout.logAndAbortException(e);
}
Stdout.log("Creation fichier AVI");
// get the output of the processor
DataSource source = processor.getDataOutput();
// create a File protocol MediaLocator with the location
// of the file to which bits are to be written
MediaLocator dest = new MediaLocator("file:testcam.avi");
// create a datasink to do the file
DataSink dataSink = null;
MyDataSinkListener dataSinkListener = null;
try
{
dataSink = Manager.createDataSink(source, dest);
dataSinkListener = new MyDataSinkListener();
dataSink.addDataSinkListener(dataSinkListener);
dataSink.open();
}
catch (IOException e) { Stdout.logAndAbortException(e); }
catch (NoDataSinkException e) { Stdout.logAndAbortException(e); }
catch (SecurityException e) { Stdout.logAndAbortException(e); }
// now start the datasink and processor
try
{
dataSink.start();
}
catch (IOException e) { Stdout.logAndAbortException(e); }
processor.start();
Stdout.log("starting capturing ...");
try { Thread.currentThread().sleep(10000); } catch (InterruptedException ie) {} // capture for 10 seconds
Stdout.log("... capturing done");
// stop and close the processor when done capturing...
// close the datasink when EndOfStream event is received...
processor.stop();
processor.close();
dataSinkListener.waitEndOfStream(10);
dataSink.close();
Stdout.log("[all done]");
}
} |
Partager