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 com.google.zxing;
using COMMON = com.google.zxing.common;

namespace Qcode
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.textBox1.Text.Trim()))
            {
                MessageBox.Show("请输入需要转换的信息!");
            }
            else
            {
                string content = this.textBox1.Text;//待编码数据
                try
                {
                    int QSize = Int32.Parse(textBox2.Text);//二维码大小
                    string s = hScrollBar1.Value.ToString("X");//二维码透明度
                    string q = hScrollBar2.Value.ToString("X");//背景透明度
                    string Scolor = "0x" + s + textBox3.Text;//二维码颜色
                    string Qcolor = "0x" + q + textBox4.Text;//背景颜色
                    COMMON.ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QSize, QSize);
                    Bitmap bt = toBitmap(byteMatrix, Scolor, Qcolor);
                    pictureBox1.Image = bt;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        public static Bitmap toBitmap(COMMON.ByteMatrix matrix,string scolor,string qcolor)
        {
            int width = matrix.Width;
            int height = matrix.Height;
            Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml(scolor) : ColorTranslator.FromHtml(qcolor));
                }
            }

            return bmap;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Image img = pictureBox1.Image;
            if (img != null)
            {
                SaveFileDialog sFD = new SaveFileDialog();
                sFD.Filter = "*.png|*.png";
                if (sFD.ShowDialog() == DialogResult.OK)
                {
                    Bitmap bmap = new Bitmap(img, img.Width, img.Height);
                    bmap.Save(sFD.FileName);
                    MessageBox.Show("保存成功!");
                }
            }
            else
            {
                MessageBox.Show("您还没有生成二维码!");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            Bitmap bmap;
            try
            {
                Image img = Image.FromFile(this.openFileDialog1.FileName);
                bmap = new Bitmap(img);
                if (bmap == null)
                {
                    MessageBox.Show("解码错误,请确保二维码图片已打开!");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("解码错误,请确保图片格式正确!");
                return;
            }

            LuminanceSource source = new RGBLuminanceSource(bmap, bmap.Width, bmap.Height);
            com.google.zxing.BinaryBitmap bitmap = new com.google.zxing.BinaryBitmap(new COMMON.HybridBinarizer(source));
            Result result;
            try
            {
                result = new MultiFormatReader().decode(bitmap);
            }
            catch
            {
                string str = "解码失败,失败原因可能是:"+"\n";
                str += "1.您打开的图片非二维码图片!" + "\n";
                str += "2.您打开的二维码图片背景色太深!" + "\n";
                str += "3.您打开的二维码图片二维码和背景色太接近!" + "\n";
                MessageBox.Show(str);
                return;
            }
            textBox1.Text = result.Text;
        }

    }
}

【引用dll文件】
http://pan.baidu.com/s/1ntNr79v

【关注我的博客】

转载于:https://www.cnblogs.com/xuhang/p/3831776.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值