C#轻松实现条形码二维码生成及识别

一、前言

大家好!我是付工。

今天给大家分享一下,如何基于C#来生成并识别条形码或者二维码。

二、http://ZXing.Net

实现二维码生成的库有很多,我们这里采用的是http://ZXing.Net

ZXing是一个开放源码的,用Java实现的多种格式的一维二维条码图像处理库,而http://ZXing.Net是ZXing在.Net平台下的实现。

我们通过Nuget搜索http://zxing.net即可搜索安装。

三、BarCodeHelper

安装好http://ZXing.Net后,我们创建一个BarCodeHelper,对于进行封装,封装了以下几个方法:

方法一:生成条形码

复制代码
public static Bitmap GenerateBarCode(string text, int width, int height)
{
    BarcodeWriter writer = new BarcodeWriter();
    writer.Format = BarcodeFormat.CODE_39;
    EncodingOptions options = new EncodingOptions()
    {
        Width = width,
        Height = height,
        Margin = 2,
        PureBarcode = true
    };
    writer.Options = options;
    Bitmap map = writer.Write(text);
    return map;
}

方法二:生成二维码

复制代码
public static Bitmap GenerateQRCode(string text, int width, int height)
{
    BarcodeWriter writer = new BarcodeWriter();
    writer.Format = BarcodeFormat.QR_CODE;
    QrCodeEncodingOptions options = new QrCodeEncodingOptions()
    {
        DisableECI = true,//设置内容编码
        CharacterSet = "UTF-8",  //设置二维码的宽度和高度
        Width = width,
        Height = height,
        Margin = 1//设置二维码的边距,单位不是固定像素
    };
    writer.Options = options;

    Bitmap map = writer.Write(text);
    return map;
}

方法三:生成带Logo二维码:

复制代码
public static Bitmap GenerateQRCodeWithLogo(string text, int width, int height, Bitmap logo)
{
    //构造二维码写码器
    MultiFormatWriter writer = new MultiFormatWriter();
    Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
    hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
    hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    //生成二维码 
    BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, width, height, hint);
    BarcodeWriter barcodeWriter = new BarcodeWriter();
    Bitmap map = barcodeWriter.Write(bm);
    //获取二维码实际尺寸
    int[] rectangle = bm.getEnclosingRectangle();
    //计算插入图片的大小和位置
    int middleW = Math.Min((int)(rectangle[2] / 3.5), logo.Width);
    int middleH = Math.Min((int)(rectangle[3] / 3.5), logo.Height);
    int middleL = (map.Width - middleW) / 2;
    int middleT = (map.Height - middleH) / 2;
    Bitmap bmpimg = new Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb);
    using (Graphics g = Graphics.FromImage(bmpimg))
    {
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        g.DrawImage(map, 0, 0, width, height);
    }
    //将二维码插入图片
    Graphics myGraphic = Graphics.FromImage(bmpimg);
    //白底
    myGraphic.FillRectangle(Brushes.Transparent, middleL, middleT, middleW, middleH);
    myGraphic.DrawImage(logo, middleL, middleT, middleW, middleH);
    return bmpimg;
}

方法四:识别条码二维码:

复制代码
public static string ReadCode(Bitmap bitmap)
{
    BarcodeReader barcodeReader = new BarcodeReader();
    barcodeReader.Options.CharacterSet = "UTF-8";
    Result result = barcodeReader.Decode(bitmap);
    return result.Text;
}

四、案例应用

有了以上方法后,编写一个案例来进行功能测试。

按钮事件代码很简单,就是调用BarCodeHelper中的对应方法即可:

复制代码
private void btn_BarCode_Click(object sender, EventArgs e)
{
    this.pic_BarCode.Image = BarCodeHelper.GenerateBarCode(this.txt_BarCode.Text, this.pic_BarCode.Width, this.pic_BarCode.Height);
}

private void btn_QRCode_Click(object sender, EventArgs e)
{
    if (chk_Logo.Checked)
    {
        this.pic_QRCode.Image = BarCodeHelper.GenerateQRCodeWithLogo(this.txt_QRCode.Text,
this.pic_QRCode.Width, this.pic_QRCode.Height, new Bitmap(this.pic_Logo.Image));
    }
    else
    {
        this.pic_QRCode.Image = BarCodeHelper.GenerateQRCode(this.txt_QRCode.Text, this.pic_QRCode.Width, this.pic_QRCode.Height);
    }
}

private void btn_ReadQRCode_Click(object sender, EventArgs e)
{
    MessageBox.Show(BarCodeHelper.ReadCode(new Bitmap(this.pic_QRCode.Image)));
}

private void btn_ReadBarCode_Click(object sender, EventArgs e)
{
    MessageBox.Show(BarCodeHelper.ReadCode(new Bitmap(this.pic_BarCode.Image)));
}

最终实现效果如下所示:

相关推荐
捷米特网关模块通讯3 天前
以太网模块搭桥:S7-1500 PLC 对接 S7-200 SMART PLC 实现汽车焊装车间上位机集中管理
上位机·数据采集·西门子plc·工业自动化·总线协议·plc以太网模块
捷米特网关模块通讯7 天前
上位机与西门子S7-200 SMART PLC以太网通讯模块在智能仓储物流中的配置详解
上位机·数据采集·西门子plc·工业自动化·总线协议·plc以太网模块
捷米特网关模块通讯9 天前
以太网模块破解西门子200PLC串口瓶颈,支持8台上位机并发访问与远程调试
上位机·数据采集·西门子plc·以太网模块·工业自动化·工业智能网关
专注VB编程开发20年1 个月前
简易虚拟 PLC 服务器-流水线自动化,上位机程序维护升级,西门子PLC仿真
服务器·单片机·自动化·上位机·plc·流水线·工控
有技巧搬砖1 个月前
让 FCT/ICT/ATE/BMS 测试更简单高效
功能测试·测试工具·上位机
九仞山1 个月前
组态王6.6项目拷贝后日期选择组件无法显示的解决办法
上位机·scada·组态王·自控系统集成
百锦再2 个月前
《C#上位机开发从门外到门内》2-7:网络通信(TCP/IP、UDP)
tcp/ip·udp·c#·嵌入式·上位机·通信·下位机
kylezhao20193 个月前
第二节、C# 上位机核心数据类型详解(工控场景实战版)
开发语言·c#·上位机
youcans_3 个月前
【STM32-MBD】(9)Simulink 模型开发之上位机显示波形
stm32·单片机·嵌入式硬件·上位机·simulink
“抚琴”的人3 个月前
C#上位机观察者模式
开发语言·观察者模式·c#·上位机