当前位置:   article > 正文

C# Winform编程 ZXing 二维码,条形码生成与识别_c# zxing

c# zxing

安装ZXing

NuGet搜索ZXing安装ZXing.Net包
在这里插入图片描述

使用ZXing

using ZXing;
using ZXing.Common;
using ZXing.QrCode;
using ZXing.QrCode.Internal;

生成条形码

/// <summary>
/// 生成条形码
/// </summary>
/// <param name="text"></param>
static void GenerateBarCode(string text)
{ 
    BarcodeWriter writer = new BarcodeWriter();
    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 = @"D:\2.png";
    map.Save(filename, ImageFormat.Png);
    map.Dispose();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

生成二维码

 /// <summary>
 /// 生成二维码
 /// </summary>
 /// <param name="text"></param>
 static void GenerateQRCode(string text)
 {
     BarcodeWriter writer = new BarcodeWriter();
     writer.Format = BarcodeFormat.QR_CODE;
     QrCodeEncodingOptions options = new QrCodeEncodingOptions();
     options.DisableECI = true;
     // 设置内容字符编码
     options.CharacterSet = "UTF-8";
     // 设置二维码的宽度高度
     options.Width = 200;
     options.Height = 200;
     // 设置二维码的边距,单位不是固定像素
     options.Margin = 1;
     writer.Options = options;

     Bitmap map = writer.Write(text);
     string filename = @"D:\1.png";
     map.Save(filename, ImageFormat.Png);
     map.Dispose();

 }
  • 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

生成带Logo的二维码

/// <summary>
/// 生成带Logo图片的二维码
/// </summary>
/// <param name="text"></param>
static void GenerateQRCodeWithLoge(string text) 
{
    // Logo 图片
    Bitmap logo = new Bitmap(@"D:\logo.png");
    // 构造二维码写码器
    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, 300, 300, hint);
    BarcodeWriter barcodeWriter = new BarcodeWriter();
    Bitmap map = barcodeWriter.Write(bm);

    // 获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸)
    int[] rectangle = bm.getEnclosingRectangle();

    // 计算插入logo图片的大小位置
    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;

    // 将img转换成bmp格式,否则后面无法创建Graphics对象
    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);
    }

    // 将二维码插入图片
    Graphics myGraphics = Graphics.FromImage(bmpimg);
    // 白底
    myGraphics.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH);
    myGraphics.DrawImage(logo, middleL, middleT, middleW, middleH);

    // 保存生成图片
    bmpimg.Save(@"D:\logo_qrcode.png", ImageFormat.Png);

}
  • 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
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

识别二维码、条形码

/// <summary>
/// 识别二维码、条形码
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
static string ReadBarCode(string filename)
{ 
    BarcodeReader reader = new BarcodeReader();
    reader.Options.CharacterSet = "UTF-8";
    Bitmap map = new Bitmap(filename);
    Result result = reader.Decode(map);
    map.Dispose();

    return result == null ? "" : result.Text;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/649420
推荐阅读
相关标签
  

闽ICP备14008679号