码迷,mamicode.com
首页 > Windows程序 > 详细

C# ZXing.Net生成二维码、识别二维码、生成带Logo的二维码(一)

时间:2016-04-24 14:13:49      阅读:720      评论:0      收藏:0      [点我收藏+]

标签:

一.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);

 

C# ZXing.Net生成二维码、识别二维码、生成带Logo的二维码(一)

标签:

原文地址:http://www.cnblogs.com/tianma3798/p/5426869.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!