示例#1
0
        /// <summary>
        /// Main method Entry point
        /// </summary>
        /// <param name="args">parameter to process the input image</param>
        private static void Main(string[] args)
        {
            // in(i) out(o) reverse(r)
            var param = new List<string> { "/i", "/o", "/r", "/h", "/w" };

            // i o r must be at odd position
            // -- /i is must
            if (args.Count(c => c == "/i" || c == "/I") == 0)
            {
                Console.WriteLine(
                    "\t>>Please check the command format.\n\tCommand it not properly formatted.\n\t'/i file.jpg' is required.");
                ShowHelp();
                return;
            }

            if (param.Any(c =>
            {
                var index = Array.FindIndex(args, el => el.Equals(c, StringComparison.InvariantCultureIgnoreCase));
                return index > -1 && index % 2 == 1;
            }))
            {
                Console.WriteLine("\t>>Please check the command format.\n\tCommand it not properly formatted.");
                ShowHelp();
                return;
            }

            Func<string, string> myFunc = ope =>
            {
                var index = Array.FindIndex(args, el => el.Equals(ope, StringComparison.InvariantCultureIgnoreCase));
                if (index > -1 && index + 1 < args.Length)
                {
                    return args[index + 1];
                }

                return string.Empty;
            };
            Func<string, int> getPercentageValue = ope =>
            {
                var result = -1;
                var value = myFunc(ope);
                if (!string.IsNullOrEmpty(value))
                {
                    if (value.Last() == '%')
                    {
                        value = value.Substring(0, value.Length - 1).Trim();
                    }

                    int.TryParse(value, out result);
                    if (result > 100)
                    {
                        result = 100;
                    }
                }

                return result;
            };

            var filePassedIn = myFunc(param.ElementAt(0));
            var outputFile = myFunc(param.ElementAt(1));
            var isReverse = myFunc(param.ElementAt(2)).Equals("1");
            var height = getPercentageValue(param.ElementAt(3));
            var width = getPercentageValue(param.ElementAt(4));

            filePassedIn = GetTheFilePath(filePassedIn);
            outputFile = string.IsNullOrEmpty(outputFile)
                ? Path.GetFileNameWithoutExtension(filePassedIn) + ".txt"
                : outputFile;

            if (string.IsNullOrEmpty(filePassedIn))
            {
                return;
            }

            var obj = new ImageToText(filePassedIn);
            obj.Reverse = isReverse;
            Console.WriteLine("Converting from [{0}] to [{1}]...", Path.GetFileName(filePassedIn), outputFile);
            Thread.Sleep(500);
            if (obj.SaveTheLoad(outputFile, width, height))
            {
                Console.WriteLine("File Processed successfully.");
            }

            Thread.Sleep(200);
        }