首页 > 代码库 > C#生成二维码(含解码)

C#生成二维码(含解码)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Collections;
using com.google.zxing;//需要从网上下载
using System.Text.RegularExpressions;
using ByteMatrix = com.google.zxing.common.ByteMatrix;

namespace 二维码
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //生成条形码
        private void button1_Click(object sender, EventArgs e)
        {
            Regex rg = new Regex("^[0-9]{13}$");
            if (!rg.IsMatch(textBox1.Text))
            {
                MessageBox.Show("本例子采用EAN_13编码,需要输入13位数字");
                return;
            }

            try
            {
                MultiFormatWriter mutiWriter = new com.google.zxing.MultiFormatWriter();
                ByteMatrix bm = mutiWriter.encode(textBox1.Text, com.google.zxing.BarcodeFormat.EAN_13, 363, 150);
                Bitmap img = bm.ToBitmap();
                pictureBox1.Image = img;

                //自动保存图片到当前目录
                string filename = System.Environment.CurrentDirectory + "\\EAN_13" + DateTime.Now.Ticks.ToString() + ".jpg";
                img.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);
                label2.Text = "图片已保存到:" + filename;
            }
            catch (Exception ee)
            { 
                MessageBox.Show(ee.Message);
            }
        }

        //生成二维码
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                MultiFormatWriter mutiWriter = new com.google.zxing.MultiFormatWriter();
                ByteMatrix bm = mutiWriter.encode(textBox1.Text, com.google.zxing.BarcodeFormat.QR_CODE, 300, 300);
                Bitmap img = bm.ToBitmap();
                pictureBox1.Image = img;
                string filename = System.Environment.CurrentDirectory + "\\QR" + DateTime.Now.Ticks.ToString() + ".jpg";
                img.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);
                label2.Text = "图片已保存到:" + filename;

            }
            catch (Exception ee)
            { 
                MessageBox.Show(ee.Message); 
            }
        }

        //生成带图片的二维码
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                MultiFormatWriter mutiWriter = new com.google.zxing.MultiFormatWriter();
                Hashtable hint = new Hashtable();
                hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
                hint.Add(EncodeHintType.ERROR_CORRECTION, com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.H);
                ByteMatrix bm = mutiWriter.encode(textBox1.Text, com.google.zxing.BarcodeFormat.QR_CODE, 300, 300, hint);
                Bitmap img = bm.ToBitmap();
                Image middlImg = QRMiddleImg.Image;
                System.Drawing.Size realSize = mutiWriter.GetEncodeSize(textBox1.Text, com.google.zxing.BarcodeFormat.QR_CODE, 300, 300);
                //计算插入图片的大小和位置
                int middleImgW = Math.Min((int)(realSize.Width / 3.5), middlImg.Width);
                int middleImgH = Math.Min((int)(realSize.Height / 3.5), middlImg.Height);
                int middleImgL = (img.Width - middleImgW) / 2;
                int middleImgT = (img.Height - middleImgH) / 2;

                //将img转换成bmp格式,否则后面无法创建 Graphics对象
                Bitmap bmpimg = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.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(img, 0, 0);
                }

                //在二维码中插入图片
                System.Drawing.Graphics MyGraphic = System.Drawing.Graphics.FromImage(bmpimg);
                //白底
                MyGraphic.FillRectangle(Brushes.White, middleImgL, middleImgT, middleImgW, middleImgH);
                MyGraphic.DrawImage(middlImg, middleImgL, middleImgT, middleImgW, middleImgH);

                pictureBox1.Image = bmpimg;

                //自动保存图片到当前目录
                string filename = System.Environment.CurrentDirectory + "\\QR" + DateTime.Now.Ticks.ToString() + ".jpg";
                bmpimg.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);
                label2.Text = "图片已保存到:" + filename;

            }
            catch (Exception ee)
            { 
                MessageBox.Show(ee.Message); 
            }
        }

        string opFilePath = "";
        //选择要生成的中间图片
        private void pictureBox2_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "图片文件|*.bmp;*.jpg;*.png;*.ico";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                opFilePath = openFileDialog1.FileName;
                Image img = Image.FromFile(opFilePath);
                QRMiddleImg.Image = img;
            }
        }

        //选择要解码的图片
        private void button4_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "图片文件|*.bmp;*.jpg;*.png;*.ico";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                opFilePath = openFileDialog1.FileName;
                pictureBox2.ImageLocation = opFilePath;
            }
        }

        //解码二维码
        private void button5_Click(object sender, EventArgs e)
        {
            MultiFormatReader mutiReader = new com.google.zxing.MultiFormatReader();
            Bitmap img = (Bitmap)Bitmap.FromFile(opFilePath);
            if (img == null)
                return;
            LuminanceSource ls = new RGBLuminanceSource(img, img.Width, img.Height);
            BinaryBitmap bb = new BinaryBitmap(new com.google.zxing.common.HybridBinarizer(ls));
            //注意  必须是Utf-8编码
            Hashtable hints = new Hashtable();
            hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
            Result r = mutiReader.decode(bb, hints);
            richTextBox1.Text = r.Text;
        }
    }
}

C#生成二维码(含解码)