一樣超簡單的方法 , 來吧 !

 

Step1. 開啟一個C# 的Project 

Stpe2.將Control都拉一下 ( 如圖 )

 

Step3.去下載Lib (https://archive.codeplex.com/?p=zxingnet ) "ZXing"

將這個Lib放到你開發的專案中

 

Step4.將這個Lib add to reference

 

 

 

 

Step5.接下來我們來寫點程式吧~顆顆 ( 群輝大魔王的迷樣的賤笑 )

----> 點擊你的按鈕,直接進去 ( 我們先寫Encode 的部分 )

 

 

 private void m_btnEncode_Click(object sender, EventArgs e)
        {
            //Use bitmap to storage qr-code
            System.Drawing.Bitmap bitmap = null;
            //let string to qr-code
            string strQrCodeContent = m_editInput.Text;

            //Setting Qr - code ( use (zxing.dll)lib )
            /*
                Step1. Download zxing.dll
                Step2. put this file to project
                Step3. add reference
            */
            ZXing.BarcodeWriter writer = new ZXing.BarcodeWriter
            {
                Format  = ZXing.BarcodeFormat.QR_CODE,
                Options = new ZXing.QrCode.QrCodeEncodingOptions
                {
                    //Create Photo 
                    Height          = 200,
                    Width           = 200,
                    CharacterSet    = "UTF-8",

                    //錯誤修正容量
                    //L水平    7%的字碼可被修正
                    //M水平    15%的字碼可被修正
                    //Q水平    25%的字碼可被修正
                    //H水平    30%的字碼可被修正
                    ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.H
                }

            };
            //Create Qr-code , use input string
            bitmap = writer.Write(strQrCodeContent);
            //Storage bitmpa

            string strDir;
            strDir = Directory.GetCurrentDirectory();
            strDir += "\\temp.png";
            bitmap.Save(strDir, System.Drawing.Imaging.ImageFormat.Png);
            //Display to picturebox
            pictureBox1.Image = bitmap;
        }

 

-----------------------------------------------------------------------

我們直接將PNG保存到Directory去,加上名稱,等等我們就Load這張就好

-----------------------------------------------------------------------

 

 

Step6.接下來我們來寫Decode的部分

 

 private void m_btnDecode_Click(object sender, EventArgs e)
        {
            //like before , create a bitmap 
            System.Drawing.Bitmap bitmap = null;
            //Create QRCode Reader Object
            ZXing.IBarcodeReader reader = new ZXing.BarcodeReader();

            //Load QrCode picture
            string strDir;
            strDir = Directory.GetCurrentDirectory();
            strDir += "\\temp.png";
            FileStream file = new FileStream(strDir, FileMode.Open);
            Byte[] data = new Byte[file.Length];

            file.Read(data, 0, data.Length);
            file.Close();

            MemoryStream ms = new MemoryStream(data);
            bitmap = (Bitmap)Image.FromStream(ms);

            //Show picture to picture box
            pictureBox2.Image = bitmap;
            //decode
            ZXing.Result decodeResult = reader.Decode(bitmap);

            if (decodeResult != null)
            {
                m_editOutput.Text = decodeResult.Text;
            }
            else 
            {
                MessageBox.Show("Error");
            }

        }

 

 

 

 

 

 

 

 

※驗證 : 可以用手機下載一款QR Code的掃描器,然後對準你的QR CODE ! 看看會不會出現一樣的文字,這邊就不去拍驗證的圖了 ( PS : 在input那邊,你也可以使用網址,這樣你的手機掃下去,會有意想不到的事情發生哦 ~! )。

 

未完待續。

 

 

arrow
arrow
    全站熱搜

    Eric 發表在 痞客邦 留言(0) 人氣()