public void testForBits()
 {
     Assert.AreEqual(ErrorCorrectionLevel.M, ErrorCorrectionLevel.forBits(0));
     Assert.AreEqual(ErrorCorrectionLevel.L, ErrorCorrectionLevel.forBits(1));
     Assert.AreEqual(ErrorCorrectionLevel.H, ErrorCorrectionLevel.forBits(2));
     Assert.AreEqual(ErrorCorrectionLevel.Q, ErrorCorrectionLevel.forBits(3));
 }
Example #2
0
 private FormatInformation(int formatInfo)
 {
     // Bits 3,4
     errorCorrectionLevel = ErrorCorrectionLevel.forBits((formatInfo >> 3) & 0x03);
     // Bottom 3 bits
     dataMask = (sbyte)(formatInfo & 0x07);
 }
Example #3
0
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="content">内容文本</param>
        /// <param name="size">图片尺寸(像素),0表示不设置</param>
        /// <param name="margin">二维码的边距,图片尺寸大于0生效</param>
        /// <param name="errorCorrection">纠错码等级</param>
        /// <param name="characterSet">内容编码</param>
        /// <returns></returns>
        public static System.Drawing.Image EncodeQrCode(string content, int size = 10, int margin = 2, string errorCorrection = "L", string characterSet = "utf-8")
        {
            BarcodeWriter barCodeWriter = new BarcodeWriter();

            barCodeWriter.Format = BarcodeFormat.QR_CODE;
            QrCodeEncodingOptions options = new QrCodeEncodingOptions()
            {
                // DisableECI = true,
                CharacterSet = characterSet, // 设置内容编码
                // QrVersion = 8,
            };

            errorCorrection = errorCorrection.ToUpper();
            IDictionary <string, int> dic = new Dictionary <string, int>()
            {
                { "L", 1 },
                { "M", 0 },
                { "Q", 3 },
                { "H", 2 }
            };

            if (!dic.ContainsKey(errorCorrection))
            {
                errorCorrection = "L";
            }


            if (size != 0)
            {
                int s;
                if (errorCorrection == "L" || errorCorrection == "M")
                {
                    s = size * 27;
                }
                else
                {
                    s = size * 31;
                }

                s += (margin - 1) * (size * 2);

                options.Width  = s;
                options.Height = s;
                options.Margin = margin; // 设置二维码的边距,单位不是固定像素
            }


            options.ErrorCorrection = ErrorCorrectionLevel.forBits(dic[errorCorrection]);


            barCodeWriter.Options = options;
            ZXing.Common.BitMatrix bm = barCodeWriter.Encode(content);
            Bitmap result             = barCodeWriter.Write(bm);

            return(result);
        }
 public void testForBits()
 {
     Assert.AreEqual(ErrorCorrectionLevel.M, ErrorCorrectionLevel.forBits(0));
     Assert.AreEqual(ErrorCorrectionLevel.L, ErrorCorrectionLevel.forBits(1));
     Assert.AreEqual(ErrorCorrectionLevel.H, ErrorCorrectionLevel.forBits(2));
     Assert.AreEqual(ErrorCorrectionLevel.Q, ErrorCorrectionLevel.forBits(3));
     try
     {
         ErrorCorrectionLevel.forBits(4);
         throw new AssertionException("Should have thrown an exception");
     }
     catch (ArgumentException)
     {
         // good
     }
 }
 public void testBadECLevel()
 {
     ErrorCorrectionLevel.forBits(4);
     throw new AssertionException("Should have thrown an exception");
 }