コード例 #1
0
ファイル: QRCodeForm.cs プロジェクト: zsae/shadowsocks-csharp
        private void GenQR(string ssconfig)
        {
            string qrText = ssconfig;

            QRCode4CS.Options options = new QRCode4CS.Options();
            options.Text = qrText;
            QRCode4CS.QRCode qrCoded = null;
            bool             success = false;

            foreach (var level in new QRErrorCorrectLevel[] { QRErrorCorrectLevel.H, QRErrorCorrectLevel.Q, QRErrorCorrectLevel.M, QRErrorCorrectLevel.L })
            {
                for (int i = 3; i < 10; i++)
                {
                    try
                    {
                        options.TypeNumber   = i;
                        options.CorrectLevel = level;
                        qrCoded = new QRCode4CS.QRCode(options);
                        qrCoded.Make();
                        success = true;
                        break;
                    }
                    catch
                    {
                        qrCoded = null;
                        continue;
                    }
                }
                if (success)
                {
                    break;
                }
            }
            if (qrCoded == null)
            {
                return;
            }
            int    blockSize = Math.Max(200 / qrCoded.GetModuleCount(), 1);
            Bitmap drawArea  = new Bitmap((qrCoded.GetModuleCount() * blockSize), (qrCoded.GetModuleCount() * blockSize));

            using (Graphics g = Graphics.FromImage(drawArea))
            {
                g.Clear(Color.White);
                using (Brush b = new SolidBrush(Color.Black))
                {
                    for (int row = 0; row < qrCoded.GetModuleCount(); row++)
                    {
                        for (int col = 0; col < qrCoded.GetModuleCount(); col++)
                        {
                            if (qrCoded.IsDark(row, col))
                            {
                                g.FillRectangle(b, blockSize * row, blockSize * col, blockSize, blockSize);
                            }
                        }
                    }
                }
            }
            pictureBox1.Image = drawArea;
        }
コード例 #2
0
 private void GenQR(string ssconfig)
 {
     string qrText = ssconfig;
     QRCode4CS.Options options = new QRCode4CS.Options();
     options.Text = qrText;
     QRCode4CS.QRCode qrCoded = null;
     bool success = false;
     foreach (var level in new QRErrorCorrectLevel[]{QRErrorCorrectLevel.H, QRErrorCorrectLevel.Q, QRErrorCorrectLevel.M, QRErrorCorrectLevel.L})
     {
         for (int i = 3; i < 10; i++)
         {
             try
             {
                 options.TypeNumber = i;
                 options.CorrectLevel = level;
                 qrCoded = new QRCode4CS.QRCode(options);
                 qrCoded.Make();
                 success = true;
                 break;
             }
             catch
             {
                 qrCoded = null;
                 continue;
             }
         }
         if (success)
             break;
     }
     if (qrCoded == null)
     {
         return;
     }
     int blockSize = Math.Max(200 / qrCoded.GetModuleCount(), 1);
     Bitmap drawArea = new Bitmap((qrCoded.GetModuleCount() * blockSize), (qrCoded.GetModuleCount() * blockSize));
     using (Graphics g = Graphics.FromImage(drawArea))
     {
         g.Clear(Color.White);
         using (Brush b = new SolidBrush(Color.Black))
         {
             for (int row = 0; row < qrCoded.GetModuleCount(); row++)
             {
                 for (int col = 0; col < qrCoded.GetModuleCount(); col++)
                 {
                     if (qrCoded.IsDark(row, col))
                     {
                         g.FillRectangle(b, blockSize * row, blockSize * col, blockSize, blockSize);
                     }
                 }
             }
         }
     }
     pictureBox1.Image = drawArea;
 }
コード例 #3
0
    public System.Drawing.Image CreateQRCode(string inputString)
    {
        QRCode4CS.QRCode qrcode = new QRCode4CS.QRCode(new QRCode4CS.Options(inputString));
        qrcode.Make();
        System.Drawing.Image canvas = new Bitmap(86, 86);
        Graphics             artist = Graphics.FromImage(canvas);

        artist.Clear(Color.White);
        for (int row = 0; row < qrcode.GetModuleCount(); row++)
        {
            for (int col = 0; col < qrcode.GetModuleCount(); col++)
            {
                bool isDark = qrcode.IsDark(row, col);

                if (isDark == true)
                {
                    artist.FillRectangle(Brushes.Black, 2 * row + 10, 2 * col + 10, 2 * row + 15, 2 * col + 15);
                }
                else
                {
                    artist.FillRectangle(Brushes.White, 2 * row + 10, 2 * col + 10, 2 * row + 15, 2 * col + 15);
                }
            }
        }
        artist.FillRectangle(Brushes.White, 0, 76, 86, 86);
        artist.FillRectangle(Brushes.White, 76, 0, 86, 86);

        using (var ms = new System.IO.MemoryStream()) {
            canvas.Save(ms, ImageFormat.Png);
            Response.ClearContent();
            Response.Clear();
            Response.Buffer = true;
            ms.WriteTo(Response.OutputStream);
            Response.ContentType = "image/png";
            Response.Flush();
        }

        artist.Dispose();
        return(canvas);
    }
コード例 #4
0
        internal static double GetLostPoint(QRCode qrCode)
        {
            int    moduleCount = qrCode.GetModuleCount();
            double lostPoint   = 0;

            for (int row = 0; row < moduleCount; row++)
            {
                for (int col = 0; col < moduleCount; col++)
                {
                    var sameCount = 0;
                    var dark      = qrCode.IsDark(row, col);

                    for (var r = -1; r <= 1; r++)
                    {
                        if (row + r < 0 || moduleCount <= row + r)
                        {
                            continue;
                        }

                        for (var c = -1; c <= 1; c++)
                        {
                            if (col + c < 0 || moduleCount <= col + c)
                            {
                                continue;
                            }

                            if (r == 0 && c == 0)
                            {
                                continue;
                            }

                            if (dark == qrCode.IsDark((int)((int)row + r), (int)((int)col + c)))
                            {
                                sameCount++;
                            }
                        }
                    }

                    if (sameCount > 5)
                    {
                        lostPoint += (int)(3 + sameCount - 5);
                    }
                }
            }

            // LEVEL2

            for (int row = 0; row < moduleCount - 1; row++)
            {
                for (int col = 0; col < moduleCount - 1; col++)
                {
                    var count = 0;
                    if (qrCode.IsDark(row, col))
                    {
                        count++;
                    }
                    if (qrCode.IsDark(row + 1, col))
                    {
                        count++;
                    }
                    if (qrCode.IsDark(row, col + 1))
                    {
                        count++;
                    }
                    if (qrCode.IsDark(row + 1, col + 1))
                    {
                        count++;
                    }
                    if (count == 0 || count == 4)
                    {
                        lostPoint += 3;
                    }
                }
            }

            // LEVEL3

            for (int row = 0; row < moduleCount; row++)
            {
                for (int col = 0; col < moduleCount - 6; col++)
                {
                    if (qrCode.IsDark(row, col) &&
                        !qrCode.IsDark(row, col + 1) &&
                        qrCode.IsDark(row, col + 2) &&
                        qrCode.IsDark(row, col + 3) &&
                        qrCode.IsDark(row, col + 4) &&
                        !qrCode.IsDark(row, col + 5) &&
                        qrCode.IsDark(row, col + 6))
                    {
                        lostPoint += 40;
                    }
                }
            }

            for (int col = 0; col < moduleCount; col++)
            {
                for (int row = 0; row < moduleCount - 6; row++)
                {
                    if (qrCode.IsDark(row, col) &&
                        !qrCode.IsDark(row + 1, col) &&
                        qrCode.IsDark(row + 2, col) &&
                        qrCode.IsDark(row + 3, col) &&
                        qrCode.IsDark(row + 4, col) &&
                        !qrCode.IsDark(row + 5, col) &&
                        qrCode.IsDark(row + 6, col))
                    {
                        lostPoint += 40;
                    }
                }
            }

            // LEVEL4

            int darkCount = 0;

            for (int col = 0; col < moduleCount; col++)
            {
                for (int row = 0; row < moduleCount; row++)
                {
                    if (qrCode.IsDark(row, col))
                    {
                        darkCount++;
                    }
                }
            }

            double ratio = Math.Abs(100.0 * darkCount / moduleCount / moduleCount - 50) / 5;

            lostPoint += ratio * 10;

            return(lostPoint);
        }