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)));
}

最终实现效果如下所示:

相关推荐
上位机付工17 天前
西门子S7通信协议抓包分析应用
c#·wireshark·上位机·plc·抓包·s7协议·西门子
上位机付工18 天前
C#上位机实现报警语音播报
开发语言·c#·上位机·plc·运动控制卡·语音播报·报警播报
上位机付工19 天前
不会PLC,怎么学上位机?
c#·上位机·modbus·三菱·西门子·欧姆龙plc
上位机付工19 天前
C#上位机通过WebApi访问WinCC
开发语言·c#·上位机·webapi·wincc
上位机付工19 天前
C#上位机通过WebApi对接DeepSeek
c#·上位机·plc·webapi
百锦再20 天前
Modbus上位机访问形式详解及代码示例
串口·上位机·ip·tcp·modbus·ascii·网口
o0向阳而生0o25 天前
69、JS中如何调用上位机接口
javascript·上位机
未来之窗软件服务1 个月前
android 上位机调试软件-安卓串口 com ttl 调试——仙盟创梦IDE
android·ide·上位机·仙盟创梦ide
炯哈哈2 个月前
【上位机——WPF】命名空间
开发语言·windows·c#·wpf·上位机
炯哈哈2 个月前
【上位机——WPF】App.xml和Application类简介
xml·开发语言·c#·wpf·上位机