Example #1
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            try
            {
                WinAPI.Console.Console.AttachConsole();
#if DEBUG
                string[] args = { "-d", @"..\..\test", "-k", "dummy" };
                var      opt  = CLIOption.Parse(args);
#else
                var opt = CLIOption.ParseEnvironmentCommandLine();
#endif

                if (opt.Version)
                {
                    Console.WriteLine(opt.VersionMessage);
                    Environment.Exit(0);
                }
                else if (opt.Help)
                {
                    Console.WriteLine(opt.HelpMessage);
                    Environment.Exit(0);
                }
                else if (!opt.ParseSuccess)
                {
                    Console.WriteLine(opt.HelpMessage);
                    if (opt.Directory == null)
                    {
                        MessageBox.Show("-d TARGET_DIRECTORY is required.",
                                        "Error",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                        Environment.Exit(1);
                    }
                    else if (opt.GoogleAPIKey == null)
                    {
                        MessageBox.Show("-k YOUR_GOOGLE_API_KEY is required.",
                                        "Error",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                    }
                    Environment.Exit(1);
                }

                Console.WriteLine("----------");
                Console.WriteLine($"CLIOption.Directory: {opt.Directory}");
                Console.WriteLine($"CLIOption.GoogleAPIKey: {String.Join("", opt.GoogleAPIKey.Select(x => 'X'))}");
                Console.WriteLine($"CLIOption.EntryPoint: {opt.EntryPoint}");
                Console.WriteLine($"CLIOption.LanguageHints: {String.Join(", ", opt.LanguageHints)}");
                Console.WriteLine($"CLIOption.Bulk: {opt.Bulk}");
                Console.WriteLine($"CLIOption.Clipboard: {opt.Clipboard}");
                Console.WriteLine($"CLIOption.ShowResult: {opt.ShowResult}");
                Console.WriteLine($"CLIOption.Notice: {opt.Notice}");
                Console.WriteLine("----------");

                if (opt.Bulk)
                {
                    Console.WriteLine("Mode: BulkConverter");
                    Processor.BulkProcess(opt);
                }
                else
                {
                    Application.Run(new Form1(opt));
                }
            }
            finally
            {
                WinAPI.Console.Console.FreeConsole();
            }
        }
Example #2
0
File: Form1.cs Project: rubyu/cOCR
        public Form1(CLIOption.Result opt)
        {
            this.cliOption = opt;

            InitializeComponent();

            if (opt.Bulk)
            {
                throw new InvalidOperationException();
            }

            Console.WriteLine("Mode: FileSystemWatcher");

            fsWatcher.Path                  = opt.Directory;
            fsWatcher.Filter                = "*";
            fsWatcher.NotifyFilter          = NotifyFilters.FileName;
            fsWatcher.IncludeSubdirectories = true;
            fsWatcher.Created              += (Object sender, FileSystemEventArgs args) =>
            {
                if (args.ChangeType == WatcherChangeTypes.Created)
                {
                    if (Processor.IsImageExtension(args.FullPath))
                    {
                        Console.WriteLine($"[Created] {args.FullPath}");
                        Processor.Process(opt, args.FullPath, (json) =>
                        {
                            string text = json.responses[0].fullTextAnnotation.text;
                            if (opt.Clipboard)
                            {
                                InvokeProperly(() =>
                                {
                                    try
                                    {
                                        ClipboardHelper.SetText(text);
                                    }
                                    catch (Exception ex)
                                    {
                                        Console.Error.WriteLine(ex.ToString());
                                    }
                                });
                            }
                            if (opt.ShowResult)
                            {
                                try
                                {
                                    System.Diagnostics.Process.Start(args.FullPath + ".html");
                                }
                                catch (Exception ex)
                                {
                                    Console.Error.WriteLine(ex.ToString());
                                }
                            }
                            if (opt.Notice)
                            {
                                InvokeProperly(() =>
                                {
                                    ShowBalloon(text, ToolTipIcon.Info, 5);
                                });
                            }
                        });
                    }
                }
            };
            fsWatcher.EnableRaisingEvents = true;
        }