close

首先要在c# 新增套件

依照以上的順序就可以使用qrcode 套件

之前安裝很多次都失敗..後來又無緣無故安裝成功..這個套件比MIT好用太多了!!!

MIT還沒有qrcode reader的功能

廢話不多說...

產生二維條碼code

        private void GenerateCode(ref Bitmap bmp)
        {
            //要轉成QRCode 的內容
            string content = "hello,I am qrcode";
            //QRCode的設定
            ZXing.BarcodeWriter writer = new ZXing.BarcodeWriter
            {
                Format = ZXing.BarcodeFormat.QR_CODE,
                Options = new ZXing.QrCode.QrCodeEncodingOptions
                {
                    //產生出圖片的高度
                    Height = 180,
                    //產生出圖片的寬度
                    Width = 180,
                    //文字是使用哪種編碼方式
                    CharacterSet = "UTF-8",

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

                }
            };
            //將要編碼的文字產生出QRCode的圖檔
            bmp = writer.Write(content);
        }

結果如下:

讀取二維條碼

        private void decode(Bitmap img,ref ZXing.Result result)
        {
            ZXing.IBarcodeReader reader = new ZXing.BarcodeReader();
            //將圖片顯示於 PictureBox 中
            byte[] Imagebyte = BitamtpToByte(QrCodeBmp);
            //避免QrCodeBmp讀取擋案,進行解碼造成error
            MemoryStream ms = new MemoryStream(Imagebyte);
            img = (Bitmap)Image.FromStream(ms);
            //進行解碼的動作
             result= reader.Decode(img);

        }

結果如下:

 

arrow
arrow

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