zoukankan      html  css  js  c++  java
  • C# 生成读取二维码

    先在Nuget中安装这个动态库

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Linq;
    using System.Text;
    using ThoughtWorks.QRCode.Codec;
    using ThoughtWorks.QRCode.Codec.Data;
    
    namespace QRCode
    {
        public class QRCodeHelper
        {
    
            public static string SaveFilePath = "";
    
            /// <summary>
            /// 生成二维码(中间不带LOGO)
            /// </summary>
            /// <param name="qrdata">要附加的内容</param>
            /// <returns></returns>
            public static void CreateQRCode(string qrdata)
            {
                try
                {
                    QRCodeEncoder encoder = new QRCodeEncoder();
                    encoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//编码方式(注意:BYTE能支持中文,ALPHA_NUMERIC扫描出来的都是数字)
                    encoder.QRCodeScale = 4;//大小(值越大生成的二维码图片像素越高)
                    encoder.QRCodeVersion = 0;//版本(注意:设置为0主要是防止编码的字符串太长时发生错误)
                    encoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;//错误效验、错误更正(有4个等级)
                    encoder.QRCodeBackgroundColor = Color.White;
                    encoder.QRCodeForegroundColor = Color.Black;
                    Image image = encoder.Encode(qrdata, Encoding.UTF8);
                    image.Save(SaveFilePath, ImageFormat.Png);
                    image.Dispose();
    
                }
                catch (Exception e)
                {
                    //一些操作
                }
            }
    
            /// <summary>
            /// 生成二维码(中间带LOGO)
            /// </summary>
            /// <param name="qrdata">要附加的内容</param>
            ///<param name="AbsoluteLogo">保存的绝对路径</param>
            /// <returns></returns>
            public static void CreateQRCode(string qrdata, string AbsoluteLogo)
            {
                try
                {
                    QRCodeEncoder encoder = new QRCodeEncoder();
                    encoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//编码方式(注意:BYTE能支持中文,ALPHA_NUMERIC扫描出来的都是数字)
                    encoder.QRCodeScale = 4;//大小(值越大生成的二维码图片像素越高)
                    encoder.QRCodeVersion = 0;//版本(注意:设置为0主要是防止编码的字符串太长时发生错误)
                    encoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;//错误效验、错误更正(有4个等级)
                    encoder.QRCodeBackgroundColor = Color.White;
                    encoder.QRCodeForegroundColor = Color.Black;
                    Image image = encoder.Encode(qrdata, Encoding.UTF8);
                    Bitmap btm = new Bitmap(AbsoluteLogo);
                    Bitmap copyImage = new Bitmap(btm, image.Width / 5, image.Height / 5);
                    Graphics g = Graphics.FromImage(image);
                    int x = image.Width / 2 - copyImage.Width / 2;
                    int y = image.Height / 2 - copyImage.Height / 2;
                    g.DrawImage(copyImage, x, y);
                    image.Save(SaveFilePath, ImageFormat.Png);
                    image.Dispose();
                    btm.Dispose();
                    copyImage.Dispose();
                    g.Dispose();
                }
                catch (Exception e)
                {
                    //一些操作
                }
            }
    
            /// <summary>
            /// 解析二维码
            /// </summary>
            /// <param name="absoluteSave">二维码的绝对路径</param>
            /// <returns></returns>
            public static string DeCode(string AbsoluteQRCodePath)
            {
                string decodedString = new QRCodeDecoder().decode(new QRCodeBitmapImage(new Bitmap(AbsoluteQRCodePath)), Encoding.UTF8);
                return decodedString;
            }
        }
    }
    
    
    
            /// <summary>
            /// 生成二维码
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void simpleButton1_Click(object sender, EventArgs e)
            {
                #region 调用QRCode创建二维码
                string fileN = Environment.CurrentDirectory + "\Img\123_New.png";
                if (File.Exists(fileN))
                {
                    File.Delete(fileN);
                }
                QRCodeHelper.SaveFilePath = fileN;
                QRCodeHelper.CreateQRCode(this.textEdit1.Text.Trim());
                this.pictureEdit1.Image = Image.FromFile(fileN);
                #endregion
            }
            /// <summary>
            /// 读取二维码
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void simpleButton2_Click(object sender, EventArgs e)
            {
                string str = QRCodeHelper.DeCode(Environment.CurrentDirectory + "\Img\璇狼之风QRCODE.png");
    
                this.Text = "当前二维码内容:" + str;
            }
    
            /// <summary>
            /// 带Logo的QR
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void simpleButton3_Click(object sender, EventArgs e)
            {
                #region 调用QRCode创建二维码 带Logo
    
                string fileN = Environment.CurrentDirectory + "\Img\璇狼之风QRCODE.png";
    
                if (File.Exists(fileN))
                {
                    File.Delete(fileN);
                }
    
                string filePic = Environment.CurrentDirectory + "\Img\璇狼之风.jpg";
    
                QRCodeHelper.SaveFilePath = fileN;
    
                QRCodeHelper.CreateQRCode(this.textEdit1.Text.Trim(), filePic);
    
                System.Drawing.Image img = System.Drawing.Image.FromFile(fileN);
    
                System.Drawing.Image bmp = new System.Drawing.Bitmap(img);
    
                this.pictureEdit1.Image = bmp;
    
                img.Dispose();
    
                #endregion
            }
    
    

    直如弦,死道边;曲如钩,反封侯
  • 相关阅读:
    js中 var let const 区别
    img标签src引用网络图片,响应403的解决方法
    统计开发push数据
    gp日志查看
    node之path模块
    算法相关问题
    常用的 curl 发送 http 请求 命令
    Python与Go列表切片越界的对比
    golang时间与时区相关操作总结
    go语言结构体转map的方法
  • 原文地址:https://www.cnblogs.com/houlin/p/14858003.html
Copyright © 2011-2022 走看看