示例#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));
 }
示例#2
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);
        }
示例#3
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);
        }
示例#4
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 AzosException(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);
                    }
        }
示例#5
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);
        }