程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#生成二維碼(含解碼)

C#生成二維碼(含解碼)

編輯: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;
        }
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved