1. 程式人生 > >C# ZXing.Net生成二維碼、識別二維碼、生成帶Logo的二維碼(一)

C# ZXing.Net生成二維碼、識別二維碼、生成帶Logo的二維碼(一)

tree bit 字符串 單位 images j2se lba 支付 .net

一.ZXing.Net 源代碼地址:http://zxingnet.codeplex.com/

也可以使用Nuget包管理,添加如圖:

技術分享

說明:ZXing是一個開源Java類庫用於解析多種格式的1D/2D條形碼。目標是能夠對QR編碼、Data Matrix、UPC的1D條形碼進行解碼。 其提供了多種平臺下的客戶端包括:J2ME、J2SE和Android。現在也有了對應的.Net版本

二、生成二維碼

將字符編碼時可以指定字符格式;默認為ISO-8859-1英文字符集,但一般移動設備常用UTF-8字符集編碼,

可以通過QrCodeEncodingOptions設置編碼方式。

如果要生成其他zxing支持的條形碼,只要修改BarcodeWriter.Format就可以了。

技術分享
/// <summary>
/// 生成二維碼,保存成圖片
/// </summary>
static void Generate1(string text)
{
    BarcodeWriter writer = new BarcodeWriter();
    writer.Format = BarcodeFormat.QR_CODE;
    QrCodeEncodingOptions options = new QrCodeEncodingOptions();
    options.DisableECI = true;
    //設置內容編碼
    options.CharacterSet = "UTF-8";
    //設置二維碼的寬度和高度
    options.Width = 500;
    options.Height = 500;
    //設置二維碼的邊距,單位不是固定像素
    options.Margin = 1;
    writer.Options = options;

    Bitmap map = writer.Write(text);
    string filename = @"H:\桌面\截圖\generate1.png";
    map.Save(filename, ImageFormat.Png);
    map.Dispose();
}
技術分享
//生成二維碼 
Generate1("https://www.baidu.com/");
Generate1("ionic是一個強大的混合式/hybrid HTML5移動開發框架,特點是使用標準的HTML、CSS和JavaScript,開發跨平臺的應用 ,只需要幾步就可以快速創建您的Ionic應用,創建應用從這裏開始");

技術分享

三、生成條形碼

技術分享
static void Generate2(string text)
{
    BarcodeWriter writer = new BarcodeWriter();
    //使用ITF 格式,不能被現在常用的支付寶、微信掃出來
    //如果想生成可識別的可以使用 CODE_128 格式
    //writer.Format = BarcodeFormat.ITF;
    writer.Format = BarcodeFormat.CODE_128;
    EncodingOptions options = new EncodingOptions()
    {
        Width = 150,
        Height =50,
        Margin=2
    };
    writer.Options = options;
    Bitmap map = writer.Write(text);
    string filename = @"H:\桌面\截圖\generate2.png";
    map.Save(filename, ImageFormat.Png);
}
技術分享 技術分享
//生成條形碼
Generate2("1234567890");
//錯誤說明
//只支持數字
Requested contents should only contain digits, but got ‘i‘
//只支持偶數個
The lenght of the input should be even
//最大長度80
Requested contents should be less than 80 digits long, but got 102
技術分享

技術分享

三、識別二維碼/條形碼

技術分享
/// <summary>
/// 讀取二維碼
/// 讀取失敗,返回空字符串
/// </summary>
/// <param name="filename">指定二維碼圖片位置</param>
static string Read1(string filename)
{
    BarcodeReader reader = new BarcodeReader();
    reader.Options.CharacterSet = "UTF-8";
    Bitmap map = new Bitmap(filename);
    Result result = reader.Decode(map);
    return result == null ? "" : result.Text;
}
技術分享 技術分享
//1.讀取二維碼內容
//讀取字母成功
//string filename = @"H:\桌面\截圖\url.png";
//string filename = @"H:\桌面\截圖\weibo.png";
//string filename = @"H:\桌面\截圖\qrcode1.png";
//string filename = @"H:\桌面\截圖\qrcode2.png";
//string filename = @"H:\桌面\截圖\qrcode3.png";
//對於有logo的二維碼只返回字符串內容
//string filename = @"H:\桌面\截圖\qrcode4.png";
//string filename = @"H:\桌面\截圖\qrcode5.png";
//string filename = @"H:\桌面\截圖\qrcode6.png";
//識別條形碼
string filename = @"H:\桌面\截圖\generate2.png";
string result = Read1(filename);
Console.WriteLine(result);
技術分享

Demo源代碼:http://git.oschina.net/tiama3798/QrCodeTool/tree/StudyDemo1/

更多:

C# ZXing.Net生成二維碼、識別二維碼、生成帶Logo的二維碼(二)

C# ZXing.Net生成二維碼、識別二維碼、生成帶Logo的二維碼(一)