Exemplo n.º 1
0
        private static void TestMatrixEnDecoder()
        {
            string                  fileName = "encodedImg.png";
            DmtxImageEncoder        encoder  = new DmtxImageEncoder();
            DmtxImageEncoderOptions options  = new DmtxImageEncoderOptions();

            options.ModuleSize = 8;
            options.MarginSize = 4;
            options.BackColor  = Color.White;
            options.ForeColor  = Color.Green;
            Bitmap encodedBitmap = encoder.EncodeImage(testVal);

            encodedBitmap.Save(fileName, ImageFormat.Png);

            DmtxImageDecoder decoder = new DmtxImageDecoder();
            List <string>    codes   = decoder.DecodeImage((Bitmap)Bitmap.FromFile(fileName), 1, new TimeSpan(0, 0, 3));

            foreach (string code in codes)
            {
                Console.WriteLine("Decoded:\n" + code);
            }

            string     s  = encoder.EncodeSvgImage("DataMatrix.net rocks!!one!eleven!!111!eins!!!!", 7, 7, Color.FromArgb(100, 255, 0, 0), Color.Turquoise);
            TextWriter tw = new StreamWriter("encodedImg.svg");

            tw.Write(s);
            tw.Flush();
            tw.Close();

            TestRawEncoder("HELLO WORLD");
            new DmtxImageEncoder().EncodeImage("HELLO WORLD").Save("helloWorld.png");

            for (int i = 1; i < 10; i++)
            {
                var    encodedData = Guid.NewGuid().ToString();
                Bitmap source      = encoder.EncodeImage(encodedData);
                var    decodedData = decoder.DecodeImage(source);
                if (decodedData.Count != 1 || decodedData[0] != encodedData)
                {
                    throw new Exception("Encoding or decoding failed!");
                }
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            if (args.Length == 0 || (args.Length == 1 && IsHelp(args[0])))
            {
                ShowHelp();
                return;
            }
            SortedDictionary <string, string> argumentList = null;

            try
            {
                argumentList = ParseArguments(args);
            }
            catch (Exception)
            {
                Console.WriteLine("Error parsing arguments, aborting");
            }

            DmtxImageEncoderOptions encoderOptions = null;

            try
            {
                encoderOptions = GetEncoderOptions(argumentList);
            }
            catch (Exception)
            {
                Console.WriteLine("Invalid arguments, aborting");
            }
            string input = "";

            try
            {
                input = GetInputString(argumentList);
            }
            catch (Exception)
            {
                Console.WriteLine("Invalid input, aborting");
                return;
            }

            if (!argumentList.ContainsKey("-o"))
            {
                Console.WriteLine("No output file specified, aborting");
                return;
            }

            string outputFileName = argumentList["-o"];

            if (!outputFileName.ToLower().EndsWith(".jpg") &&
                !outputFileName.ToLower().EndsWith(".gif") &&
                !outputFileName.ToLower().EndsWith(".png") &&
                !outputFileName.ToLower().EndsWith(".bmp") &&
                !outputFileName.ToLower().EndsWith(".svg"))
            {
                Console.WriteLine("File type not supported!");
                return;
            }

            DmtxImageEncoder encoder = new DmtxImageEncoder();

            if (outputFileName.ToLower().EndsWith(".svg"))
            {
                string output = encoder.EncodeSvgImage(input, encoderOptions);
                try
                {
                    TextWriter tw = new StreamWriter(outputFileName);
                    tw.Write(output);
                    tw.Close();
                    Console.WriteLine("Output created successfully!");
                    return;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error writing to output file: " + ex.Message);
                    return;
                }
            }
            Image outputImage = null;

            if (argumentList.ContainsKey("-t") && argumentList["-t"] == "Mosaic")
            {
                encoderOptions.BackColor = Color.White;
                encoderOptions.ForeColor = Color.Black;
                outputImage = encoder.EncodeImageMosaic(input, encoderOptions);
            }
            else if (!argumentList.ContainsKey("-t") || (argumentList.ContainsKey("-t") && argumentList["-t"] == "Matrix"))
            {
                outputImage = encoder.EncodeImage(input, encoderOptions);
            }
            else
            {
                Console.WriteLine("Invalid output type, only 'Matrix' and 'Mosaic' are supported!");
                return;
            }
            try
            {
                outputImage.Save(outputFileName);
                Console.WriteLine("Output created successfully!");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error writing to output file: " + ex.Message);
                return;
            }
        }