Пример #1
0
        //-------------------------------------------------------------------------------------------
        public static Bitmap GenerateCode(string data, int size)
        {
            QRCodeWriter writer = new QRCodeWriter();
               com.google.zxing.common.ByteMatrix matrix;

               matrix = writer.encode(data, BarcodeFormat.QR_CODE, size, size, null);

               Bitmap img = new Bitmap(size, size);
               Color Color = Color.FromArgb(0, 0, 0);

               for (int y = 0; y < matrix.Height; ++y)
               {
                    for (int x = 0; x < matrix.Width; ++x)
                    {
                         Color pixelColor = img.GetPixel(x, y);

                         //Find the colour of the dot
                         if (matrix.get_Renamed(x, y) == -1)
                         {
                              img.SetPixel(x, y, Color.White);
                         }
                         else
                         {
                              img.SetPixel(x, y, Color.Black);
                         }
                    }
               }
               return img;
        }
Пример #2
0
 /// <summary>
 /// Encodes the given message String as QR Barcode and returns the bitmap
 /// </summary>
 /// <param name="_message">Message that should be encoded</param>
 /// <returns>Bitmap that contains the QR Barcode</returns>
 internal static Bitmap Generate(string _message)
 {
     QRCodeWriter writer = new QRCodeWriter();
     ByteMatrix matrix = writer.encode(_message,
         BarcodeFormat.QR_CODE, 200, 200, null);
     sbyte[][] img = matrix.Array;
     Bitmap bmp = new Bitmap(matrix.Width, matrix.Height);
     Graphics g = Graphics.FromImage(bmp);
     g.Clear(Color.White);
     for (int i = 0; i <= img.Length - 1; i++)
     {
         for (int j = 0; j <= img[i].Length - 1; j++)
         {
             if (img[i][j] == 0)
             {
                 g.FillRectangle(Brushes.Black, j, i, 1, 1);
             }
             else
             {
                 g.FillRectangle(Brushes.White, j, i, 1, 1);
             }
         }
     }
     return bmp;
 }
Пример #3
0
        public static void GenerateQRCode(string URL, String TargetPath)
        {
            QRCodeWriter writer = new QRCodeWriter();
            Hashtable hints = new Hashtable();

            hints.Add(EncodeHintType.ERROR_CORRECTION, com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.M);
            hints.Add("Version", "7");
            ByteMatrix byteIMGNew = writer.encode(URL, BarcodeFormat.QR_CODE, 350, 350, hints);
            sbyte[][] imgNew = byteIMGNew.Array;
            Bitmap bmp1 = new Bitmap(byteIMGNew.Width, byteIMGNew.Height);
            Graphics g1 = Graphics.FromImage(bmp1);
            g1.Clear(System.Drawing.Color.White);
            for (int i = 0; i <= imgNew.Length - 1; i++)
            {
                for (int j = 0; j <= imgNew[i].Length - 1; j++)
                {
                    if (imgNew[j][i] == 0)
                    {
                        g1.FillRectangle(System.Drawing.Brushes.Black, i, j, 1, 1);
                    }
                    else
                    {
                        g1.FillRectangle(System.Drawing.Brushes.White, i, j, 1, 1);
                    }
                }
            }
            bmp1.Save(TargetPath, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
Пример #4
0
        private void button1_Click(object sender, EventArgs e)
        {
            String encoding = textBox_Name.Text + "|" + textBox_Kill.Text;

            com.google.zxing.qrcode.QRCodeWriter qrCode = new com.google.zxing.qrcode.QRCodeWriter();
            com.google.zxing.common.ByteMatrix byteIMG = qrCode.encode(encoding, com.google.zxing.BarcodeFormat.QR_CODE, 200, 200);

            sbyte[][] img = byteIMG.Array;

            Bitmap qrCodeBmp = new Bitmap(200, 200);
            Graphics g = Graphics.FromImage(qrCodeBmp);

            g.Clear(Color.White);

            for (int i = 0; i <= img.Length - 1; i++)
            {
                for (int j = 0; j <= img[i].Length - 1; j++)
                {
                    if (img[i][j] == 0)
                    {
                        g.FillRectangle(Brushes.Black, j, i, 1, 1);
                    }
                    else
                    {
                        g.FillRectangle(Brushes.White, j, i, 1, 1);
                    }
                }
            }

            Bitmap blankID = new Bitmap("hvzid02.jpg");
            Bitmap genID = new Bitmap(234, 126);

            Graphics build = Graphics.FromImage(genID);
            build.DrawImage(blankID, 0, 0);

            build.DrawString(textBox_Name.Text, new Font("Arial", 10), new SolidBrush(System.Drawing.Color.Black), 6, 80);
            build.DrawString(textBox_Kill.Text, new Font("Arial", 10), new SolidBrush(System.Drawing.Color.Black), 6, 95);

            build.DrawImage(scaleByPercent(qrCodeBmp, 75), 95, -12);

            toolStripStatusLabel1.Text = "Encoded";

            pictureBox_ID.Image = genID;

            saveFileDialog1.Filter = "jpeg files (*.jpeg)|*.jpeg";

            if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && saveFileDialog1.FileName.Length > 0)
            {

                genID.Save(saveFileDialog1.FileName, ImageFormat.Jpeg);

                toolStripStatusLabel1.Text = "ID saved";
            }
        }
Пример #5
0
        /// <summary>
        /// Generates a bitmap containing a QR-code
        /// The bitmap will be padded with some space in the margins.
        /// </summary>
        /// <param name="str">The string which we encode into the QR-code</param>
        /// <param name="width">Height of the resulting bitmap</param>
        /// <param name="height">Width of the resulting bitmap</param>
        /// <returns>A bitmap containing a QR-code containing the input string with padding.</returns>
        public static Bitmap GenerateQRC(string str, int width, int height)
        {
            QRCodeWriter qrCode = new QRCodeWriter();
            ByteMatrix byteIMG = qrCode.encode(str, BarcodeFormat.QR_CODE, width, height);

            sbyte[][] img = byteIMG.Array;
            Bitmap bmp = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bmp);

            fillImage(img, g);
            return bmp;
        }
Пример #6
0
 private void DoTextToQr(string text)
 {
     try
     {
         var writer = new QRCodeWriter();
         const string encoding = "UTF-8";// "ISO-8859-1";
         var hints = new Hashtable { { EncodeHintType.CHARACTER_SET, encoding }, { EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L } };
         var matrix = writer.encode(text, BarcodeFormat.QR_CODE, 100, 100, hints);
         var img = new Bitmap(200, 200);
         var g = Graphics.FromImage(img);
         g.Clear(Color.White);
         for (var y = 0; y < matrix.Height; ++y)
             for (var x = 0; x < matrix.Width; ++x)
                 if (matrix.get_Renamed(x, y) != -1)
                     g.FillRectangle(Brushes.Black, x * 2, y * 2, 2, 2);
         ImageBox.ShowDialog(img);
     }
     catch (Exception ex)
     {
         MessageBox.Show(@"Error Loading:" + Environment.NewLine + ex.Message);
     }
 }
Пример #7
0
 private void DoBinToQr(string filename)
 {
     try
     {
         var byteArray = File.ReadAllBytes(filename);
         var writer = new QRCodeWriter();
         const string encoding = "ISO-8859-1";
         var str = Encoding.GetEncoding(encoding).GetString(byteArray);
         var hints = new Hashtable { { EncodeHintType.CHARACTER_SET, encoding } };
         var matrix = writer.encode(str, BarcodeFormat.QR_CODE, 100, 100, hints);
         var img = new Bitmap(200, 200);
         var g = Graphics.FromImage(img);
         g.Clear(Color.White);
         for (var y = 0; y < matrix.Height; ++y)
             for (var x = 0; x < matrix.Width; ++x)
                 if (matrix.get_Renamed(x, y) != -1)
                     g.FillRectangle(Brushes.Black, x * 2, y * 2, 2, 2);
         ImageBox.ShowDialog(img);
     }
     catch (Exception ex)
     {
         MessageBox.Show(@"Error Loading:" + Environment.NewLine + ex.Message);
     }
 }
Пример #8
0
        private void CreateQr()
        {
            QRCodeWriter writer = new QRCodeWriter();
            Hashtable ht = new Hashtable();
            ht.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
            ht.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            ht.Add(EncodeHintType.VERSION_START, 5);
            Bitmap image = writer.encode(this.txtContent.Text, BarcodeFormat.QR_CODE, 460, 460, ht).ToBitmap();
            Bitmap bitmap = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb);
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            graphics.DrawImage(image, 0, 0);
            image.Dispose();
            SizeF ef = new SizeF();

            #region 设置左上角特效颜色
            Bitmap bitmapLeftTop = SetBitmap(bitmap.Width, bitmap.Height);
            Color color = Color.FromArgb(200, 0xe0, 0x72, 1);
            int num = 122 - (Encoding.UTF8.GetBytes(this.txtContent.Text).Length - 20) / 2;

            for (int i = 0; i < bitmap.Width; i++)
            {
                for (int j = 0; j < bitmap.Height; j++)
                {
                    Color leftTopColor;
                    Color pixel = bitmap.GetPixel(i, j);
                    if ((i < num) && (j < num))
                    {
                        leftTopColor = ((pixel.A == 0xff) && (pixel.B == 0)) ? color : pixel;
                    }
                    else
                    {
                        leftTopColor = ((pixel.A == 0xff) && (pixel.B == 0)) ? bitmapLeftTop.GetPixel(i, j) : pixel;
                    }
                    bitmap.SetPixel(i, j, leftTopColor);
                }
            }
            bitmapLeftTop.Dispose();
            #endregion

            #region 设置标题特效
            string str2 = this.txtTitle.Text;
            float emSize = 18f;
            emSize -= (str2.Length - 4) * 1.8f;
            Font font = new Font("微软雅黑", emSize, FontStyle.Bold);
            ef = graphics.MeasureString(str2, font);
            float num7 = (bitmap.Width - ef.Width) / 2f;
            Brush brush = new SolidBrush(Color.FromArgb(0xff, 0x3a, 0xb2, 0xc2));
            Brush brush2 = new SolidBrush(Color.White);
            int y = 40;
            graphics.FillRectangle(brush2, new Rectangle((int)num7, y, (int)ef.Width - 3, (int)ef.Height - 3));
            graphics.DrawString(str2, font, brush, (float)((int)num7), (float)y);

            #endregion

            #region 设置中心特效
            Brush brush3 = new SolidBrush(Color.FromArgb(0xff, 0x3a, 0xb2, 0xc2));
            const int width = 140;
            graphics.FillEllipse(brush2, (bitmap.Width - width) / 2, (bitmap.Height - width) / 2, width, width);
            const int num10 = 0x80;
            graphics.FillEllipse(brush3, (bitmap.Width - num10) / 2, (bitmap.Height - num10) / 2, num10, num10);
            const int num11 = 110;
            graphics.FillEllipse(brush2, (bitmap.Width - num11) / 2, (bitmap.Height - num11) / 2, num11, num11);
            str2 = this.txtMark.Text;
            float num12 = 32f;
            num12 -= (str2.Length - 3) * 3.5f;
            Font font2 = new Font("Meiryo", num12, FontStyle.Bold);
            ef = graphics.MeasureString(str2, font2);
            float x = ((bitmap.Width - ef.Width) / 2f) + 2f;
            float num14 = ((bitmap.Height - ef.Height) / 2f) + 8f;
            graphics.DrawString(str2, font2, brush3, x, num14);
            graphics.Dispose();

            #endregion

            if (this.pictureBox1.Image != null)
            {
                this.pictureBox1.Image.Dispose();
            }
            this.pictureBox1.Height = bitmap.Height;
            this.pictureBox1.Width = bitmap.Width;
            this.pictureBox1.Image = bitmap;
        }
        private ByteMatrix GetQRMatrix(int size)
        {
            var writer = new QRCodeWriter();
            const string encoding = "ISO-8859-1";
            if (hexBox1.ByteProvider == null)
                return null;
            if (hexBox1.ByteProvider.Length == 0)
                return null;
            QRByteArray = new Byte[0];
            var data = new Byte[0];
            for (int i = 0; i < hexBox1.ByteProvider.Length; i++)
            {
                data = new Byte[QRByteArray.Length + 1];
                QRByteArray.CopyTo(data, 0);
                data[i] = hexBox1.ByteProvider.ReadByte(i);
                QRByteArray = data;
            }

            if (!advancedInterfaceToolStripMenuItem.Checked || !dontDecryptToolStripMenuItem.Checked)
                data = Crypto.Encrypt(QRByteArray);
            else
                data = QRByteArray;

            if (data == null)
                return null;
            var str = Encoding.GetEncoding(encoding).GetString(data);
            var hints = new Hashtable { { EncodeHintType.CHARACTER_SET, encoding }, {EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q } };
            return writer.encode(str, BarcodeFormat.QR_CODE, size, size, hints);
        }
Пример #10
0
 private ByteMatrix GetQRMatrix(int size)
 {
     var writer = new QRCodeWriter();
     const string encoding = "ISO-8859-1";
     var ms = new MemoryStream();
     SaveChanges();
     gridControl.LevelData.Write(ms);
     var data = ms.ToArray();
     ms.Close();
     var str = Encoding.GetEncoding(encoding).GetString(data);
     var hints = new Hashtable { { EncodeHintType.CHARACTER_SET, encoding } };
     return writer.encode(str, BarcodeFormat.QR_CODE, size, size, hints);
 }
Пример #11
0
 public KwQRCodeWriter()
 {
     writer = new QRCodeWriter();
 }
Пример #12
0
        public static Bitmap textToQRImage(string text, QR_CORRECT_LEV correctLev)
        {
            try
            {
                if (writer == null) writer = new QRCodeWriter();
                Hashtable hints = new Hashtable();
                switch (correctLev)
                {
                    case QR_CORRECT_LEV.L:
                        hints[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.L;
                        break;
                    case QR_CORRECT_LEV.M:
                        hints[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.M;
                        break;
                    case QR_CORRECT_LEV.Q:
                        hints[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.Q;
                        break;
                    case QR_CORRECT_LEV.H:
                        hints[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.H;
                        break;
                    default:
                        hints[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.L;
                        break;
                }
                ByteMatrix byteMatrix = writer.encode(
                        text,
                        BarcodeFormat.QR_CODE,
                        size,
                        size,
                        hints);

                return byteMatrix.ToBitmap();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
        }
Пример #13
0
        private ByteMatrix QRCodeGen(string text, int width, int height)
        {
            try
            {
                QRCodeWriter writer = new QRCodeWriter(0);
                Hashtable hints = new Hashtable();

                hints.Add(EncodeHintType.ERROR_CORRECTION, com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.M);
                hints.Add("Version", "7");

                ByteMatrix _matrix = writer.encode(text, BarcodeFormat.QR_CODE, width, height, hints);

                #region Supress border white line
                int row = -1;
                int col = -1;
                int endRow = 0;
                int endCol = 0;
                bool isWhiteLine = true;

                while (isWhiteLine)
                {
                    row++;
                    for (int i = 0; i < _matrix.Width; i++)
                    {
                        if (_matrix.Array[row][i] == 0)
                        {
                            isWhiteLine = false;
                            break;
                        }
                    }
                }

                isWhiteLine = true;
                endRow = _matrix.Height;
                while (isWhiteLine)
                {
                    endRow--;
                    for (int i = 0; i < _matrix.Width; i++)
                    {
                        if (_matrix.Array[endRow][i] == 0)
                        {
                            isWhiteLine = false;
                            break;
                        }
                    }
                }

                isWhiteLine = true;
                while (isWhiteLine)
                {
                    col++;
                    for (int i = 0; i < _matrix.Height; i++)
                    {
                        if (_matrix.Array[i][col] == 0)
                        {
                            isWhiteLine = false;
                            break;
                        }
                    }
                }

                isWhiteLine = true;
                endCol = _matrix.Width;
                while (isWhiteLine)
                {
                    endCol--;
                    for (int i = 0; i < _matrix.Height; i++)
                    {
                        if (_matrix.Array[i][endCol] == 0)
                        {
                            isWhiteLine = false;
                            break;
                        }
                    }
                }

                #endregion

                ByteMatrix final = new ByteMatrix(endCol - col, endRow - row);
                for (int y = row; y < endRow; y++)
                {
                    for (int x = col; x < endCol; x++)
                    {
                        final.Array[x - col][y - row] = _matrix.Array[x][y];
                    }
                }

                return final;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
Пример #14
0
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = dataGridView1.SelectedCells[0].OwningRow;
            string key = row.Cells["ColKey"].Value.ToString();

            QRCodeWriter qcCode = new QRCodeWriter();
            ByteMatrix btMatrix = qcCode.encode(key, BarcodeFormat.QR_CODE, 570, 570);
            Form keyQR = new KeyQR(btMatrix.ToBitmap());
            keyQR.Show();
        }
Пример #15
0
    Texture2D CreateQR(string iQRString, int iSize)
    {
        Texture2D tmpTex = new Texture2D(iSize, iSize);

        QRCodeWriter tmpWriter = new QRCodeWriter();
        ByteMatrix tmpMatrix = tmpWriter.encode(iQRString, com.google.zxing.BarcodeFormat.QR_CODE, iSize, iSize);

        Color32[] tmpColor = new Color32[iSize * iSize];

        for (int i = 0; i < iSize; i++)
        {
            for (int j = 0; j < iSize; j++)
            {
                int tmpPos = j * iSize + i;
                if (tmpMatrix.Array[i][j] == 0)
                {
                    tmpColor[tmpPos] = QRCodeBackcolor;
                }
                else
                {
                    tmpColor[tmpPos] = QRCodeForecolor;
                }
                //byte tmpCol = tmpMatrix.Array[i][j] == 0 ? (byte)0 : (byte)255;
                //tmpColor[tmpPos].r = tmpColor[tmpPos].g = tmpColor[tmpPos].b = tmpCol;
                //tmpColor[tmpPos].a = 255;
            }
        }

        tmpTex.SetPixels32(tmpColor);
        tmpTex.Apply();

        return tmpTex;
    }
Пример #16
0
        private bool CreateCode()
        {
            bool returndata = true;
            try{
            //Write the file if it doesnt exist.
            if(!File.Exists(ConfigurationManager.AppSettings["imgDir"] + Id +".png")){
                QRCodeWriter writer = new QRCodeWriter();

                int size = 180;
                ByteMatrix data = writer.encode(ConfigurationManager.AppSettings["linkUrlRoot"] + Id, BarcodeFormat.QR_CODE, size, size);

                Bitmap img = new Bitmap(size, size);

                for (int y = 0; y < data.Height; ++y)
                {
                    for (int x = 0; x < data.Width; ++x)
                    {

                        //Find the colour of the dot
                        if (data.get_Renamed(x, y) == -1)
                        {
                            img.SetPixel(x, y, Color.White);
                        }
                        else
                        {
                            img.SetPixel(x, y, Color.Black);
                        }
                    }
                }

                img.Save(ConfigurationManager.AppSettings["imgDir"] + Id +".png", ImageFormat.Png);
                }
            }
            catch{
                returndata =false;
            }
            return returndata;
        }
        private void _SaleCupon_Initialize()
        {
            QRCodeWriter QRWriter = new QRCodeWriter();
            ByteMatrix QRObject = QRWriter.encode("http://www.curfit.com/mobile/" + cuponhash.Data.hash , BarcodeFormat.QR_CODE, 600, 600);
            SaleCuponControl.imgQRCode.Source = getImageSource(QRObject.ToBitmap());

            SaleCuponControl.btnClose.Click += new RoutedEventHandler(_SaleCupon_P_RightHandUp);
            SaleCuponControl.btnReturn.Click += new RoutedEventHandler(_SaleCupon_P_LeftHandUp);

        }