Example #1
0
 public static Image CreateOutput(this QRMatrix matrix,
                                  Color?trueColor  = null,
                                  Color?falseColor = null,
                                  ImageScale?scale = ImageScale.Scale1x)
 {
     return(createOutput(matrix, trueColor, falseColor, scale));
 }
Example #2
0
        public static void ToJPG(this QRMatrix matrix,
                                 System.IO.Stream stream,
                                 Color?trueColor  = null,
                                 Color?falseColor = null,
                                 ImageScale?scale = ImageScale.Scale1x)
        {
            var output = createOutput(matrix, trueColor, falseColor, scale);

            output.Save(stream, JpegImageFormat.Standard);
        }
Example #3
0
        public static void ToGIF(this QRMatrix matrix,
                                 System.IO.Stream stream,
                                 Color?trueColor  = null,
                                 Color?falseColor = null,
                                 ImageScale?scale = ImageScale.Scale1x)
        {
            var output = createOutput(matrix, trueColor, falseColor, scale);

            output.Save(stream, GifImageFormat.Monochrome);
        }
Example #4
0
        public static void ToGIF(this QRMatrix matrix,
                                 System.IO.Stream stream,
                                 Color?trueColor  = null,
                                 Color?falseColor = null,
                                 ImageScale?scale = ImageScale.Scale1x)
        {
            var output = createDrawingOutput(matrix, trueColor, falseColor, scale);

            output.ToImage(stream, ImageFormat.Gif);
        }
Example #5
0
        private static Image createOutput(QRMatrix matrix,
                                          Color?trueColor  = null,
                                          Color?falseColor = null,
                                          ImageScale?scale = ImageScale.Scale1x)
        {
            var black = trueColor ?? Color.Black;
            var white = falseColor ?? Color.White;

            if (black == white)
            {
                throw new NFXException(StringConsts.ARGUMENT_ERROR + typeof(QRImageRenderer).Name + ".ToBitmap(trueColor!=falseColor)");
            }

            int scaleFactor = (int)scale;

            int canvasWidth  = matrix.Width * scaleFactor;
            int canvasHeight = matrix.Height * scaleFactor;

            var result = Image.Of(canvasWidth, canvasHeight);

            using (var canvas = result.CreateCanvas())
                using (var blackBrush = canvas.CreateSolidBrush(black))
                    using (var whiteBrush = canvas.CreateSolidBrush(white))
                    {
                        canvas.FillRectangle(blackBrush, 0, 0, canvasWidth, canvasHeight);

                        for (int yMatrix = 0; yMatrix < matrix.Height; yMatrix++)
                        {
                            for (int xMatrix = 0; xMatrix < matrix.Width; xMatrix++)
                            {
                                if (matrix[xMatrix, yMatrix] == 0)
                                {
                                    int scaledX = xMatrix * scaleFactor;
                                    int scaledY = yMatrix * scaleFactor;

                                    canvas.FillRectangle(whiteBrush, scaledX, scaledY, scaleFactor, scaleFactor);
                                }
                            }
                        }

                        return(result);
                    }
        }
Example #6
0
        public void BuildMatrix()
        {
            const int WIDTH = 21, HEIGHT = 21;

              // From http://www.swetake.com/qr/qr7.html
              int[] ints = {
              32, 65, 205, 69, 41, 220, 46, 128, 236,
              42, 159, 74, 221, 244, 169, 239, 150, 138,
              70, 237, 85, 224, 96, 74, 219 , 61};

              BitList bits = new BitList();
              foreach (int i in ints)
            bits.AppendBits(i, 8);

              QRMatrix matrix = new QRMatrix(WIDTH, HEIGHT);

              matrix.FormMatrix(bits, QRCorrectionLevel.H, QRVersion.GetVersionByNumber(1), 3);

              string expected =
            "1 1 1 1 1 1 1 0 0 1 1 0 0 0 1 1 1 1 1 1 1\r\n" +
            "1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\r\n" +
            "1 0 1 1 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 0 1\r\n" +
            "1 0 1 1 1 0 1 0 0 1 1 0 0 0 1 0 1 1 1 0 1\r\n" +
            "1 0 1 1 1 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 1\r\n" +
            "1 0 0 0 0 0 1 0 0 0 1 1 1 0 1 0 0 0 0 0 1\r\n" +
            "1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\r\n" +
            "0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0\r\n" +
            "0 0 1 1 0 0 1 1 1 0 0 1 1 1 1 0 1 0 0 0 0\r\n" +
            "1 0 1 0 1 0 0 0 0 0 1 1 1 0 0 1 0 1 1 1 0\r\n" +
            "1 1 1 1 0 1 1 0 1 0 1 1 1 0 0 1 1 1 0 1 0\r\n" +
            "1 0 1 0 1 1 0 1 1 1 0 0 1 1 1 0 0 1 0 1 0\r\n" +
            "0 0 1 0 0 1 1 1 0 0 0 0 0 0 1 0 1 1 1 1 1\r\n" +
            "0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 1 0 1 1\r\n" +
            "1 1 1 1 1 1 1 0 1 1 1 1 0 0 0 0 1 0 1 1 0\r\n" +
            "1 0 0 0 0 0 1 0 0 0 0 1 0 1 1 1 0 0 0 0 0\r\n" +
            "1 0 1 1 1 0 1 0 0 1 0 0 1 1 0 0 1 0 0 1 1\r\n" +
            "1 0 1 1 1 0 1 0 1 1 0 1 0 0 0 0 0 1 1 1 0\r\n" +
            "1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 0 1 1 1 0 0\r\n" +
            "1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0\r\n" +
            "1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 0 1 0 0 1 0";

              Assert.AreEqual( expected, matrix.ToString());
        }
Example #7
0
        private static DrawingOutput createDrawingOutput(QRMatrix matrix,
                                                         Color?trueColor  = null,
                                                         Color?falseColor = null,
                                                         ImageScale?scale = ImageScale.Scale1x)
        {
            var black = trueColor ?? Color.Black;
            var white = falseColor ?? Color.White;

            if (black == white)
            {
                throw new NFXException(StringConsts.ARGUMENT_ERROR + typeof(QRImageRenderer).Name + ".ToBitmap(trueColor!=falseColor)");
            }

            int scaleFactor = (int)scale;

            int canvasWidth  = matrix.Width * scaleFactor;
            int canvasHeight = matrix.Height * scaleFactor;

            //TODO: Can we not cache brush instances on color in static dictionary? i.e. var blackbrush = BRUSHES[black]; Is brush thread safe for reading only?
            using (var blackBrush = new SolidBrush(black))
                using (var whiteBrush = new SolidBrush(white))
                {
                    var drawingOutput = new DrawingOutput(canvasWidth, canvasHeight, blackBrush);
                    for (int yMatrix = 0, yCanvasStart = 0, yCanvasStop = scaleFactor;
                         yMatrix < matrix.Height;
                         yMatrix++, yCanvasStart += scaleFactor, yCanvasStop += scaleFactor)
                    {
                        for (int xMatrix = 0, xCanvasStart = 0, xCanvasStop = scaleFactor;
                             xMatrix < matrix.Width;
                             xMatrix++, xCanvasStart += scaleFactor, xCanvasStop += scaleFactor)
                        {
                            if (matrix[xMatrix, yMatrix] == 0)
                            {
                                drawingOutput.SetPixelScaled(xMatrix, yMatrix, whiteBrush, scaleFactor);
                            }
                        }
                    }

                    return(drawingOutput);
                }
        }
Example #8
0
 public static void ToGIF(this QRMatrix matrix, System.IO.Stream stream,
                          Color?trueColor = null, Color?falseColor = null, ImageScale?scale = ImageScale.Scale1x)
 {
     matrix.ToImage(stream, System.Drawing.Imaging.ImageFormat.Gif, trueColor, falseColor, scale);
 }
Example #9
0
        private static int chooseMaskPattern(BitList bits, QRCorrectionLevel correctionLevel, QRVersion version, QRMatrix matrix)
        {
            int minPenalty      = Int32.MaxValue; // Assume the lowest possible penalty
            int bestMaskPattern = -1;

            // Calculate all mask paterns to find the pattern with minimum possible penalty
            for (int maskPattern = 0; maskPattern < MASK_PATTERNS_QTY; maskPattern++)
            {
                matrix.FormMatrix(bits, correctionLevel, version, maskPattern);
                int penalty = matrix.GetMaskPenalty();
                if (penalty < minPenalty)
                {
                    minPenalty      = penalty;
                    bestMaskPattern = maskPattern;
                }
            }
            return(bestMaskPattern);
        }
Example #10
0
        public void VersionInfo()
        {
            const int WIDTH = 21, HEIGHT = 21;
              // Version info bits = 000111 110010 010100
              // Actually, version 7 QR Code has 45x45 matrix but we use 21x21 here
              // since 45x45 matrix is too big to depict.
              QRMatrix matrix = new QRMatrix(WIDTH, HEIGHT);

              matrix.AddVersionInfoIfRequired(QRVersion.GetVersionByNumber(7));

              string expected =
            "                    0 0 1                \r\n" +
            "                    0 1 0                \r\n" +
            "                    0 1 0                \r\n" +
            "                    0 1 1                \r\n" +
            "                    1 1 1                \r\n" +
            "                    0 0 0                \r\n" +
            "                                         \r\n" +
            "                                         \r\n" +
            "                                         \r\n" +
            "                                         \r\n" +
            "0 0 0 0 1 0                              \r\n" +
            "0 1 1 1 1 0                              \r\n" +
            "1 0 0 1 1 0                              \r\n" +
            "                                         \r\n" +
            "                                         \r\n" +
            "                                         \r\n" +
            "                                         \r\n" +
            "                                         \r\n" +
            "                                         \r\n" +
            "                                         \r\n" +
            "                                         ";

              Assert.AreEqual( expected, matrix.ToString());
        }
Example #11
0
        public void TypeInfo()
        {
            const int WIDTH = 21, HEIGHT = 21;

              // Type info bits = 100000011001110.
              QRMatrix matrix = new QRMatrix(WIDTH, HEIGHT);

              matrix.AddTypeInfo(QRCorrectionLevel.M, 5);
              string expected =
            "                0                        \r\n" +
            "                1                        \r\n" +
            "                1                        \r\n" +
            "                1                        \r\n" +
            "                0                        \r\n" +
            "                0                        \r\n" +
            "                                         \r\n" +
            "                1                        \r\n" +
            "1 0 0 0 0 0   0 1         1 1 0 0 1 1 1 0\r\n" +
            "                                         \r\n" +
            "                                         \r\n" +
            "                                         \r\n" +
            "                                         \r\n" +
            "                                         \r\n" +
            "                0                        \r\n" +
            "                0                        \r\n" +
            "                0                        \r\n" +
            "                0                        \r\n" +
            "                0                        \r\n" +
            "                0                        \r\n" +
            "                1                        ";

              Assert.AreEqual( expected, matrix.ToString());
        }
Example #12
0
        public void Pattern_02()
        {
            const int WIDTH = 25, HEIGHT = 25;

              QRMatrix matrix = new QRMatrix(WIDTH, HEIGHT);
              matrix.Fill(2);
              matrix.AddBasicPatterns(QRVersion.GetVersionByNumber(2));

              string expected =
            "1 1 1 1 1 1 1 0                   0 1 1 1 1 1 1 1\r\n" +
            "1 0 0 0 0 0 1 0                   0 1 0 0 0 0 0 1\r\n" +
            "1 0 1 1 1 0 1 0                   0 1 0 1 1 1 0 1\r\n" +
            "1 0 1 1 1 0 1 0                   0 1 0 1 1 1 0 1\r\n" +
            "1 0 1 1 1 0 1 0                   0 1 0 1 1 1 0 1\r\n" +
            "1 0 0 0 0 0 1 0                   0 1 0 0 0 0 0 1\r\n" +
            "1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\r\n" +
            "0 0 0 0 0 0 0 0                   0 0 0 0 0 0 0 0\r\n" +
            "            1                                    \r\n" +
            "            0                                    \r\n" +
            "            1                                    \r\n" +
            "            0                                    \r\n" +
            "            1                                    \r\n" +
            "            0                                    \r\n" +
            "            1                                    \r\n" +
            "            0                                    \r\n" +
            "            1                   1 1 1 1 1        \r\n" +
            "0 0 0 0 0 0 0 0 1               1 0 0 0 1        \r\n" +
            "1 1 1 1 1 1 1 0                 1 0 1 0 1        \r\n" +
            "1 0 0 0 0 0 1 0                 1 0 0 0 1        \r\n" +
            "1 0 1 1 1 0 1 0                 1 1 1 1 1        \r\n" +
            "1 0 1 1 1 0 1 0                                  \r\n" +
            "1 0 1 1 1 0 1 0                                  \r\n" +
            "1 0 0 0 0 0 1 0                                  \r\n" +
            "1 1 1 1 1 1 1 0                                  ";

              Assert.AreEqual( expected, matrix.ToString());
        }
Example #13
0
        public void DataBits()
        {
            const int WIDTH = 21, HEIGHT = 21;

              //System.Diagnostics.Debugger.Launch();

              // Cells other than basic patterns should be filled with zero.
              QRMatrix matrix = new QRMatrix(WIDTH, HEIGHT);

              matrix.AddBasicPatterns(QRVersion.GetVersionByNumber(1));

              BitList bits = new BitList();

              matrix.InsertDataBits(bits, -1);

              string expected =
            "1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1\r\n" +
            "1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\r\n" +
            "1 0 1 1 1 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1\r\n" +
            "1 0 1 1 1 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1\r\n" +
            "1 0 1 1 1 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1\r\n" +
            "1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\r\n" +
            "1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\r\n" +
            "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n" +
            "0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n" +
            "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n" +
            "0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n" +
            "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n" +
            "0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n" +
            "0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0\r\n" +
            "1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n" +
            "1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n" +
            "1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n" +
            "1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n" +
            "1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n" +
            "1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n" +
            "1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0";

              Assert.AreEqual( expected, matrix.ToString());
        }
Example #14
0
 private static int chooseMaskPattern(BitList bits, QRCorrectionLevel correctionLevel, QRVersion version, QRMatrix matrix)
 {
   int minPenalty = Int32.MaxValue;  // Assume the lowest possible penalty
   int bestMaskPattern = -1;
   // Calculate all mask paterns to find the pattern with minimum possible penalty
   for (int maskPattern = 0; maskPattern < MASK_PATTERNS_QTY; maskPattern++)
   {
     matrix.FormMatrix(bits, correctionLevel, version, maskPattern);
     int penalty = matrix.GetMaskPenalty();
     if (penalty < minPenalty)
     {
       minPenalty = penalty;
       bestMaskPattern = maskPattern;
     }
   }
   return bestMaskPattern;
 }