天天看點

關于生成\讀取 一維碼二維碼的方法

一、首先下載下傳 ZXing.Net

位址是:http://zxingnet.codeplex.com/releases/view/117068

然後将對應版本 .dll 拖入項目中,再引用之。

主要是用 BarcodeWriter、BarcodeReader。

二、生成二維碼

.NET 平台的代碼始終要簡單些。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

QrCodeEncodingOptions options =

new

QrCodeEncodingOptions();

options.CharacterSet =

"UTF-8"

;

options.DisableECI =

true

;

// Extended Channel Interpretation (ECI) 主要用于特殊的字元集。并不是所有的掃描器都支援這種編碼。

options.ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.H;

// 糾錯級别

options.Width = 300;

options.Height = 300;

options.Margin = 1;

// options.Hints,更多屬性,也可以在這裡添加。

BarcodeWriter writer =

new

BarcodeWriter();

writer.Format = BarcodeFormat.QR_CODE;

writer.Options = options;

Response.Clear();

using

(Bitmap bmp = writer.Write(

"http://www.cftea.com"

)) // Write 具備生成、寫入兩個功能

{

MemoryStream ms =

new

MemoryStream();

{

bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

Response.ContentType =

"image/png"

;

Response.BinaryWrite(ms.ToArray());

}

}

Response.End();

糾錯級别:

  1.     L - 約 7% 糾錯能力。
  2.     M - 約 15% 糾錯能力。
  3.     Q - 約 25% 糾錯能力。
  4.     H - 約 30% 糾錯能力。

三、生成條形碼

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

QrCodeEncodingOptions options =

new

QrCodeEncodingOptions();

options.CharacterSet =

"UTF-8"

;

options.Width = 300;

options.Height = 50;

options.Margin = 1;

options.PureBarcode =

false

;

// 是否是純碼,如果為 false,則會在圖檔下方顯示數字

BarcodeWriter writer =

new

BarcodeWriter();

writer.Format = BarcodeFormat.CODE_128;

writer.Options = options;

Response.Clear();

using

(Bitmap bmp = writer.Write(

"12345678"

))

{

MemoryStream ms =

new

MemoryStream();

{

bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

Response.ContentType =

"image/png"

;

Response.BinaryWrite(ms.ToArray());

}

}

Response.End();

四、識别二維碼、條形碼

?

1 2 3 4 5 6 7

BarcodeReader reader =

new

BarcodeReader();

reader.Options.CharacterSet =

"UTF-8"

;

using

(Bitmap bmp =

new

Bitmap(

"D:\\qr.png"

))

{

Result result = reader.Decode(bmp);

Response.Write(result.Text);

}

總結

好了,以上就是這篇文章的全部内容了,如果要改變背景顔色、畫頭像,可以直接在 Bitmap 中畫,希望本文的内容對大家的學習或者工作能帶來一定的幫助