Exemple #1
0
        /// <summary>
        /// 创建二维码
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string Create(string str, int _size, string destImg = "", bool _combin = false, QRCodeLevel _levet = QRCodeLevel.M, QRCodeVersion _ver = QRCodeVersion.A, QRCodeType _type = QRCodeType.Byte)
        {
            var _result = string.Empty;
            var fullpath = string.Empty;
            var filepath = System.Web.HttpContext.Current.Server.MapPath(@"~\QRCode\upload") + "\\";
            if (string.IsNullOrEmpty(destImg))
            {
                destImg = filepath + "favicon.png";
            }
            if (string.IsNullOrEmpty(str))
            {
                _result = "[{\"status\":0,\"count\":1,\"content\":\"" + "亲内容去哪里了".ChinessConvertToUnicodeString() + "\"}]";
                return _result;
            }

            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            String encoding = _type.ToString();
            if (encoding == "Byte")
            {
                qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            }
            else if (encoding == "AlphaNumeric")
            {
                qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC;
            }
            else if (encoding == "Numeric")
            {
                qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.NUMERIC;
            }

            try
            {
                int scale = _size;
                qrCodeEncoder.QRCodeScale = scale;
            }
            catch (Exception ex)
            {
                //throw new Exception(ex.ToString());
                _result = "[{\"status\":0,\"count\":1,\"content\":\"" + "大小设置出错".ChinessConvertToUnicodeString() + "\"}]";
                return _result;
            }
            try
            {
                int version = Convert.ToInt16(_ver);
                qrCodeEncoder.QRCodeVersion = version + 1;
            }
            catch (Exception ex)
            {
                //throw new Exception(ex.ToString());
                _result = "[{\"status\":0,\"count\":1,\"content\":\"" + "版本设置出错".ChinessConvertToUnicodeString() + "\"}]";
                return _result;
            }
            string errorCorrect = _levet.ToString();
            if (errorCorrect == "L")
                qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
            else if (errorCorrect == "M")
                qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
            else if (errorCorrect == "Q")
                qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q;
            else if (errorCorrect == "H")
                qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;

            Image image;

            image = qrCodeEncoder.Encode(ConverToGB(str, 16));

            var filename = Guid.NewGuid().ToString() + ".png";

            fullpath = filepath + filename;

            if (!Directory.Exists(filepath))
            {
                Directory.CreateDirectory(filepath);
            }
            fullpath = filepath + filename;
            if (_combin)
            {
                CombinImage(image, destImg, true);
            }
            image.Dispose();
            _result = "[{\"status\":1,\"count\":1,\"content\":\"" + str + "\",\"list\":[{\"imgurl\":\"" + fullpath + "\"}]}]";

            return _result;
        }
Exemple #2
0
 /// <summary>
 /// Generates a QR code with specified error correction level, version, and pixel scaling from the input data and returns the QR code image as an array of bytes.
 /// </summary>
 /// <param name="data">The user data for encoding.</param>
 /// <param name="encoding">The QR code encoding.</param>
 /// <param name="levelCorrection">The level of error correction.</param>
 /// <param name="version">The QR code version.</param>
 /// <param name="scaleMultiplier">The pixel scaling of the resulting QR code image.</param>
 /// <returns>byte[]</returns>
 public static byte[] GetQRCode(string data, QRCodeEncodingMethod encoding, QRCodeErrorCorrection levelCorrection, QRCodeVersion version, int scaleMultiplier = 1)
 {
     return(GetQRCode(data, encoding, levelCorrection, version, scaleMultiplier, Color.Black, Color.White));
 }
Exemple #3
0
 public void LoadQRCode(string encode_data, QRCodeVersion version)
 {
     QRCode = Images.GenerateQRCodeByte(RemoveAccentsWithNormalization(encode_data), new QRCodeParams(version));
 }
Exemple #4
0
 public QRCodeParams(QRCodeVersion version)
 {
     Init(version);
 }
Exemple #5
0
        /// <summary>
        /// Renders a QR code on bitmap from the encoded data and returns an array of bytes.
        /// </summary>
        /// <param name="data">The user data for encoding.</param>
        /// <param name="encoding">The QR code encoding.</param>
        /// <param name="levelCorrection">The level of error correction.</param>
        /// <param name="version">The QR code version.</param>
        /// <param name="scaleMultiplier">The pixel scaling of the resulting QR code image.</param>
        /// <param name="foregroundColor">The QR code color.</param>
        /// <param name="backgroundColor">The background color.</param>
        /// <param name="quietZone">The size of the quiet zone.</param>
        /// <returns>byte[]</returns>
        internal static byte[] GetQRCodeImage(string data, QRCodeEncodingMethod encoding, QRCodeErrorCorrection levelCorrection, QRCodeVersion version, int scaleMultiplier, Color foregroundColor, Color backgroundColor, int quietZone = 4)
        {
            if (!Enum.IsDefined(typeof(QRCodeVersion), version))
            {
                throw new ArgumentOutOfRangeException();
            }

            // Only the binary method is implemented
            if (encoding != QRCodeEncodingMethod.Binary)
            {
                throw new NotImplementedException();
            }

            int numVersion;

            if (version == QRCodeVersion.Automatic)
            {
                numVersion = (int)QRCodesUtils.GetMinVersionForBinaryMode(data, levelCorrection);
            }
            else
            {
                numVersion = (int)version;
            }

            var numberMask = 4;


            // Get the image data array
            byte[] dataBinary = QRCodesEncoder.EncodeQRCodeData(data, numVersion, levelCorrection);

            // Create a bitmap for the QR code
            var bitmapSource = CreateBitmap(dataBinary, numVersion, numberMask, levelCorrection,
                                            new SolidBrush(foregroundColor),
                                            new SolidBrush(backgroundColor),
                                            quietZone);
            // Pixel scaling

            var bitmap = QRCodesUtils.Scale(bitmapSource, scaleMultiplier);

            // Save the resulting image in the PNG format
            var memStream = new MemoryStream();

            bitmap.Save(memStream, System.Drawing.Imaging.ImageFormat.Png);
            memStream.Flush();
            var qrData = memStream.ToArray();

            memStream.Close();
            return(qrData);
        }