• ZXing.net 生成和解析二维码


    nuget引用zxing.net包

    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
    this.pictureBox1.Image = GetQRCodeByZXingNet("https://mp.weixin.qq.com/",238,238);
    }
    /// <summary>
    /// 生成二维码图片
    /// </summary>
    /// <param name="strMessage">要生成二维码的字符串</param>
    /// <param name="width">二维码图片宽度</param>
    /// <param name="height">二维码图片高度</param>
    /// <returns></returns>
    private Bitmap GetQRCodeByZXingNet(String strMessage, Int32 width, Int32 height)
    {
    Bitmap result = null;
    try
    {
    BarcodeWriter barCodeWriter = new BarcodeWriter();
    barCodeWriter.Format = BarcodeFormat.QR_CODE;
    barCodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
    barCodeWriter.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
    barCodeWriter.Options.Height = height;
    barCodeWriter.Options.Width = width;
    barCodeWriter.Options.Margin = 0;
    ZXing.Common.BitMatrix bm = barCodeWriter.Encode(strMessage);
    result = barCodeWriter.Write(bm);
    //string filename = @"C:Users	est.png";
    //result .Save(filename, ImageFormat.Png);
    //result .Dispose();
    }
    catch (Exception ex)
    {
    //异常输出
    }
    return result;
    }
    /// <summary>
    /// 解码二维码
    /// </summary>
    /// <param name="barcodeBitmap">待解码的二维码图片</param>
    /// <returns>扫码结果</returns>
    private string DecodeQrCode(Bitmap barcodeBitmap)
    {
    BarcodeReader reader = new BarcodeReader();
    reader.Options.CharacterSet = "UTF-8";
    var result = reader.Decode(barcodeBitmap);
    return (result == null) ? null : result.Text;
    }
    
    private void button2_Click(object sender, EventArgs e)
    {
    this.label1.Text = DecodeQrCode((Bitmap)this.pictureBox1.Image);
    }
    }
    
    
  • 相关阅读:
    [WPF]根据内容自动设置大小的RichTextBox
    SICP In Other Languages
    VS 代码段编辑器
    利用SQL Server Migration Assistant将access数据库导入到SQL
    关于gcc on windows的“拒绝访问”问题
    文本比较(C#版本)
    [WPF]自定义鼠标指针
    哎~~~又是RichTextBox
    [iphone]想或正在做iphone开发的朋友,这里有点ppt和demo
    Windows Phone 7 的 “界面设计与交互指南”
  • 原文地址:https://www.cnblogs.com/stubborn-donkey/p/9634348.html
Copyright © 2020-2023  润新知