| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
10年前发布

使用zxing类生成一维码、二维码实例

  条形码在生活中使用的已经相当广泛了,不管是去书店买书,还是去超市买商品,都会用到条码,而且每一个条码中的信息都不尽相同,每一类的商品都有统一的条 码,当然条码的类型也有不同,比如有标准的UPC条码,也有Code39,Code128,EAN8等等好多好多。这些根据不同的需要而被广泛使用。

      最近根据项目的需求,用了一段时间对一维码、二维码进行了小小的研究,在一篇Blog中看到使用了google的zxing生成二维码,好奇之下自己做了一个小Demo。

生成一维码

生成二维码

#region 引用程序集    using System;    using System.Collections.Generic;    using System.ComponentModel;    using System.Data;    using System.Drawing;    using System.Linq;    using System.Text;    using System.Threading.Tasks;    using System.Windows.Forms;    using com.google.zxing.qrcode;    using com.google.zxing;    using com.google.zxing.common;    using ByteMatrix = com.google.zxing.common.ByteMatrix;    using EAN13Writer = com.google.zxing.oned.EAN13Writer;    using EAN8Writer = com.google.zxing.oned.EAN8Writer;    using MultiFormatWriter = com.google.zxing.MultiFormatWriter;    #endregion        namespace qr    {        public partial class Form1 : Form        {            public Form1()            {                // 初始化组件                InitializeComponent();            }                //  public static void writeToFile(ByteMatrix matrix, System.Drawing.Imaging.ImageFormat format, string file)            //{            //    Bitmap bmap = toBitmap(matrix);            //    bmap.Save(file, format);            //}            /// <summary>            /// 绘制一维码、二维码位图            /// </summary>            /// <param name="matrix"></param>            /// <returns></returns>            public static Bitmap toBitmap(ByteMatrix matrix)               {                // 定义位图的款和高                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("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF"));                       }                   }                   return bmap;                           }                private void button1_Click_1(object sender, EventArgs e)            {                string content = textBox1.Text;                int codeWidth = Convert.ToInt16(txtWidth.Text.Trim().ToString());                int codeHeight = Convert.ToInt16(txtHeight.Text.Trim().ToString());                // 生成二维码                ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, codeWidth, codeHeight);                Bitmap bitmap = toBitmap(byteMatrix);                pictureBox1.Image = bitmap;                //writeToFile(byteMatrix, System.Drawing.Imaging.ImageFormat.Png, sFD.FileName);                //SaveFileDialog sFD = new SaveFileDialog();                //sFD.DefaultExt = "*.png|*.png";                //sFD.AddExtension = true;                //try                //{                //    if (sFD.ShowDialog() == DialogResult.OK)                //    {                    //    }                //}                //catch (Exception ex)                //{                //    MessageBox.Show(ex.Message);                //}            }                private void button2_Click(object sender, EventArgs e)            {                string content = textBox1.Text;                // 生成一维码                ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.EAN_8, 200, 100);                Bitmap bitmap = toBitmap(byteMatrix);                pictureBox1.Image = bitmap;                                //Bitmap twobitmap = new Bitmap(bitmap.Width ,bitmap.Height);                //Graphics g = Graphics.FromImage(bitmap);                //g.DrawString(content, Font, Brushes.Black, pictureBox1.);                //pictureBox1.Image = bitmap;                            }         }    }  


来自:http://blog.csdn.net/happylee6688/article/details/8446203