C#实现二维码的生成与解析

参考原帖:C# 二维码 生成、解析

项目源码:请别抢我闪刀姬/MakeBarCode

前言

一直觉得二维码颇神奇,前一段还经常在手机上用一个DIY二维码的软件。今天刚好看到了这位大佬的文章,就拿来学习一下。一来记录一下,二来练习一下上传源码。

页面设计

在这里插入图片描述
页面内容很少,左边是一个pictureBox,用来存放二维码图片,右边一个文本框,用来输入制作二维码的内容或者显示识别二维码的结果,下面三个button,用来打开新图片,保存生成的二维码图片,如果识别结果为链接可以点击“转到”,使用默认浏览器打开链接。(话说大佬的页面更简洁,打开图片都是用拖放的,保存和转到都是右键菜单,但我学半天也没有学会拖放打开图片,只好先制作一个缩水版。)

代码实现部分

定义变量

首先定义一个是否更新的变量,因为生成和识别所使用同一个文本框,所以要加以判断当前状态。

        bool update = true;

生成二维码

当文本框内容发生变化且当前状态为生成二维码时,获取当前文本框的内容调用方法进行制作。

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (update)
            {
                String url = textBox1.Text;
                if (!url.Equals(""))
                {
                    Bitmap pic = ToQR(url);
                    pictureBox1.Image = pic;
                }
            }
        }

保存二维码

单击“保存”且图片框的内容不为空时,弹出保存对话框,选择路径,通过保存方法将图片保存到指定位置。

        private void save_Click(object sender, System.EventArgs e)
        {
            if (pictureBox1.Image != null)
            {
                Bitmap pic = new Bitmap(pictureBox1.Image);
                SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.Filter = "png files(*.png)|*.png|All files(*.*)|*.*";    //设置文件类型 
                saveFileDialog1.FilterIndex = 2;                                     //设置默认文件类型显示顺序 
                saveFileDialog1.RestoreDirectory = true;                             //保存对话框是否记忆上次打开的目录 
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string localFilePath = saveFileDialog1.FileName.ToString();
                    Save(pic, localFilePath);
                }
            }
        }

打开新的图片

单击“打开”后弹出打开文件对话框,选择后将图片展示在图像框内,将解析结果显示在文本框内。

        private void Open_Click(object sender, System.EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            if (open.ShowDialog() == DialogResult.OK)
            {
                Image pic = Bitmap.FromFile(open.FileName);
                pictureBox1.Image = pic;
                update = false;
                String code = ToCode(new Bitmap(pic));
                textBox1.Text = code;
                update = true;
            }
        }

转到识别链接

格式正确则转到,格式错误则提示。

        private void go_Click(object sender, System.EventArgs e)
        {
            try
            {
                if (!textBox1.Text.Equals("")) System.Diagnostics.Process.Start(textBox1.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("请检查网址或路径是否正确");
            }
        }

功能性函数

生成链接地址对应的二维码图像

        public static Bitmap ToQR(String url)
        {
            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
            QrCode qrCode = qrEncoder.Encode(url);
            GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White);
            DrawingSize size = render.SizeCalculator.GetSize(qrCode.Matrix.Width);
            Bitmap pic = new Bitmap(size.CodeWidth, size.CodeWidth);
            Graphics g = Graphics.FromImage(pic);
            render.Draw(g, qrCode.Matrix);
            return pic;
        }

解析二维码图像转化为编码串

        public static string ToCode(Bitmap pic)
        {
            QRCodeDecoder decoder = new QRCodeDecoder();
            String decodedString = decoder.decode(new QRCodeBitmapImage(pic));
            return decodedString;
        }

按指定路径保存图像

        public static void Save(Bitmap pic, String filePath)
        {
            if (File.Exists(filePath)) File.Delete(filePath);
            pic.Save(filePath, ImageFormat.Png);
        }

运行效果

在这里插入图片描述
这里演示一下生成二维码,识别的截图与此差距不大。

结语

因为刚刚接触,所以表述上可能会有差错,主要就是感受一下完成时的喜悦。不足之处,望见谅。

发布了19 篇原创文章 · 获赞 47 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44122062/article/details/105054459