static int Main(string[] args) { if (args.Length % 2 != 0 || (args.Length == 1 && args[0] == "/?")) { PrintUsage(); return(0); } string logfilename = null; try { string fileName = String.Format("{0:yyyyMMddHmmss}", System.DateTime.Now); int delay = 4000; int width = 640; int height = 480; for (int i = 0; i < args.Length; i = i + 2) { string sw = args[i].Replace("-", "").ToLower(); string op = args[i + 1]; switch (sw) { case ("f"): fileName = op; break; case ("s"): string[] ops = op.Split("x".ToCharArray()); if (ops == null || ops.Length != 2) { Console.WriteLine("size format WIDTHxHEIGHT."); PrintUsage(); return(0); } width = Convert.ToInt32(ops[0]); height = Convert.ToInt32(ops[1]); break; case ("d"): delay = Convert.ToInt32(op); break; case ("l"): logfilename = op; break; default: Console.Out.WriteLine("Unknown option {0}", sw); PrintUsage(); return(0); } } Webcam camera = new Webcam(new Size(width, height), 30); Image captured_image = null; int counter = 0; //remove any extensions from target file name int lastdot = -1; lastdot = fileName.LastIndexOf("."); if (lastdot != -1 && !fileName.Substring(lastdot).Contains("\\")) { fileName = fileName.Substring(0, lastdot); } if (fileName.EndsWith("\\")) { fileName = fileName + String.Format("{0:yyyyMMddHmmss}", System.DateTime.Now); } //Assign proper extension if (!fileName.EndsWith(".jpg", true, null)) { fileName = fileName + ".jpg"; } if (logfilename != null && !logfilename.EndsWith(".log", true, null)) { logfilename = logfilename + ".log"; } string curr_dir = Environment.CurrentDirectory; //modify filenames to show absolute file paths if (!fileName.Contains("\\")) { fileName = curr_dir + "\\" + fileName; } if (logfilename != null && !logfilename.Contains("\\")) { logfilename = curr_dir + "\\" + logfilename; } //start the camera camera.Start(); //start listening for windows messages Application.DoEvents(); /* Try capturing the image from the webcam 100 times * with sleeping 10 milliseconds before each try. */ do { Thread.Sleep(delay); captured_image = camera.currentImage; counter++; } while (captured_image == null && counter <= 100); camera.Stop(); if (captured_image == null) { throw new Exception("Device time-out"); } else { using (FileStream fs = new FileStream(fileName, FileMode.Create)) { captured_image.Save(fs, ImageFormat.Jpeg); Console.WriteLine("Image saved to " + fileName); WriteLog("Image stored at " + fileName, logfilename); } } } catch (FileNotFoundException fnfex) { WriteLog(fnfex.ToString(), logfilename); return(1); } catch (ArgumentException aex) { Console.WriteLine("Argument Exception"); WriteLog(aex.ToString(), logfilename); return(2); } catch (Exception ex) { WriteLog(ex.ToString(), logfilename); return(7); } finally { Application.Exit(); } return(0); }
static int Main(string[] args) { if (args.Length % 2 != 0 || (args.Length == 1 && args[0] == "/?")) { PrintUsage(); return 0; } string logfilename = null; try { string fileName = String.Format("{0:yyyyMMddHmmss}", System.DateTime.Now); int delay = 4000; int width = 640; int height = 480; for (int i = 0; i < args.Length; i = i + 2) { string sw = args[i].Replace("-", "").ToLower(); string op = args[i + 1]; switch (sw) { case ("f"): fileName = op; break; case ("s"): string[] ops = op.Split("x".ToCharArray()); if (ops == null || ops.Length != 2) { Console.WriteLine("size format WIDTHxHEIGHT."); PrintUsage(); return 0; } width = Convert.ToInt32(ops[0]); height = Convert.ToInt32(ops[1]); break; case ("d"): delay = Convert.ToInt32(op); break; case ("l"): logfilename = op; break; default: Console.Out.WriteLine("Unknown option {0}", sw); PrintUsage(); return 0; } } Webcam camera = new Webcam(new Size(width, height), 30); Image captured_image = null; int counter = 0; //remove any extensions from target file name int lastdot = -1; lastdot = fileName.LastIndexOf("."); if (lastdot != -1 && !fileName.Substring(lastdot).Contains("\\")) fileName = fileName.Substring(0, lastdot); if (fileName.EndsWith("\\")) { fileName = fileName + String.Format("{0:yyyyMMddHmmss}", System.DateTime.Now); } //Assign proper extension if (!fileName.EndsWith(".jpg", true, null)) fileName = fileName + ".jpg"; if (logfilename != null && !logfilename.EndsWith(".log", true, null)) logfilename = logfilename + ".log"; string curr_dir = Environment.CurrentDirectory; //modify filenames to show absolute file paths if (!fileName.Contains("\\")) fileName = curr_dir + "\\" + fileName; if (logfilename != null && !logfilename.Contains("\\")) logfilename = curr_dir + "\\" + logfilename; //start the camera camera.Start(); //start listening for windows messages Application.DoEvents(); /* Try capturing the image from the webcam 100 times * with sleeping 10 milliseconds before each try. */ do { Thread.Sleep(delay); captured_image = camera.currentImage; counter++; } while (captured_image == null && counter <= 100); camera.Stop(); if (captured_image == null) { throw new Exception("Device time-out"); } else { using (FileStream fs = new FileStream(fileName, FileMode.Create)) { captured_image.Save(fs, ImageFormat.Jpeg); Console.WriteLine("Image saved to " + fileName); WriteLog("Image stored at " + fileName, logfilename); } } } catch (FileNotFoundException fnfex) { WriteLog(fnfex.ToString(), logfilename); return 1; } catch (ArgumentException aex) { Console.WriteLine("Argument Exception"); WriteLog(aex.ToString(), logfilename); return 2; } catch (Exception ex) { WriteLog(ex.ToString(), logfilename); return 7; } finally { Application.Exit(); } return 0; }