分享

.NET 一维、二维码生成DEMO

 行走在理想边缘 2014-07-27

条码技术越来越流行,已经不再局限于物流、超市等专业行业了,很多网站都已经加上了二维码,作为代码民工们,怎么能不懂得用这门技术呢。

 

在网上找了一些资料,一维码生成的源码相对较多,也可用,二维码的也不少,但我发现找来找去都是同一个DEMO,而且跑不动,晕死,后来找到了QrCodeNet的源码才搞掂。

 

关键代码如下:

一维码生成(调用BarcodeLib):

  1. //生成一维码图片  
  2. private byte[] GetBarcode(int height, int width, BarcodeLib.TYPE type,  
  3.                                    string code, out System.Drawing.Image image)  
  4. {  
  5.     image = null;  
  6.     BarcodeLib.Barcode b = new BarcodeLib.Barcode();  
  7.     b.BackColor = System.Drawing.Color.White;  
  8.     b.ForeColor = System.Drawing.Color.Black;  
  9.     b.IncludeLabel = true;  
  10.     b.Alignment = BarcodeLib.AlignmentPositions.CENTER;  
  11.     b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;  
  12.     b.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;  
  13.     System.Drawing.Font font = new System.Drawing.Font("verdana", 10f);  
  14.     b.LabelFont = font;  
  15.   
  16.     b.Height = height;  
  17.     b.Width = width;  
  18.   
  19.     try  
  20.     {  
  21.         image = b.Encode(type, code);  
  22.     }  
  23.     catch (Exception e)  
  24.     {  
  25.         MessageBox.Show(e.Message);  
  26.         image = null;  
  27.     }  
  28.     //SaveImage(image, Guid.NewGuid().ToString("N") + ".png");  
  29.     byte[] buffer = b.GetImageData(BarcodeLib.SaveTypes.GIF);  
  30.     return buffer;  
  31. }  
  32.   
  33. //调用  
  34. private void BuildBarcode()  
  35. {  
  36.     string number = string.IsNullOrEmpty(textBox1.Text.Trim()) ? "Avx-(13614)-vR" : textBox1.Text.Trim();  
  37.     BarcodeLib.TYPE codeType = comboBox1.SelectedIndex == -1 ? BarcodeLib.TYPE.CODE128 : (BarcodeLib.TYPE)Enum.Parse(typeof(BarcodeLib.TYPE), comboBox1.Text);  
  38.     System.Drawing.Image image;  
  39.     int width = 250, height = 100;  
  40.     byte[] buffer = GetBarcode(height, width,  
  41.         codeType, number, out image);  
  42.   
  43.     pictureBox1.Image = image;  
  44.     if (image != null)  
  45.         pictureBox1.SizeMode = image.Height > pictureBox1.Height ? PictureBoxSizeMode.Zoom : PictureBoxSizeMode.Normal;  
  46. }      

 


二维码生成类(调用QrCodeNet):

  1. /// <summary>  
  2. /// Class containing the description of the QR code and wrapping encoding and rendering.  
  3. /// </summary>  
  4. internal class CodeDescriptor  
  5. {  
  6.     public ErrorCorrectionLevel Ecl;  
  7.     public string Content;  
  8.     public QuietZoneModules QuietZones;  
  9.     public int ModuleSize;  
  10.     public BitMatrix Matrix;  
  11.     public string ContentType;  
  12.   
  13.     /// <summary>  
  14.     /// Parse QueryString that define the QR code properties  
  15.     /// </summary>  
  16.     /// <param name="request">HttpRequest containing HTTP GET data</param>  
  17.     /// <returns>A QR code descriptor object</returns>  
  18.     public static CodeDescriptor Init(ErrorCorrectionLevel level, string content, QuietZoneModules qzModules, int moduleSize)  
  19.     {  
  20.         var cp = new CodeDescriptor();  
  21.   
  22.         //// Error correction level  
  23.         cp.Ecl = level;  
  24.         //// Code content to encode  
  25.         cp.Content = content;  
  26.         //// Size of the quiet zone  
  27.         cp.QuietZones = qzModules;  
  28.         //// Module size  
  29.         cp.ModuleSize = moduleSize;  
  30.         return cp;  
  31.     }  
  32.   
  33.     /// <summary>  
  34.     /// Encode the content with desired parameters and save the generated Matrix  
  35.     /// </summary>  
  36.     /// <returns>True if the encoding succeeded, false if the content is empty or too large to fit in a QR code</returns>  
  37.     public bool TryEncode()  
  38.     {  
  39.         var encoder = new QrEncoder(Ecl);  
  40.         QrCode qr;  
  41.         if (!encoder.TryEncode(Content, out qr))  
  42.             return false;  
  43.   
  44.         Matrix = qr.Matrix;  
  45.         return true;  
  46.     }  
  47.   
  48.     /// <summary>  
  49.     /// Render the Matrix as a PNG image  
  50.     /// </summary>  
  51.     /// <param name="ms">MemoryStream to store the image bytes into</param>  
  52.     internal void Render(MemoryStream ms)  
  53.     {  
  54.         var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones));  
  55.         render.WriteToStream(Matrix, System.Drawing.Imaging.ImageFormat.Png, ms);  
  56.         ContentType = "image/png";  
  57.     }  
  58. }  


二维码生成调用:

  1. string content = string.IsNullOrEmpty(textBox1.Text.Trim()) ? "http://www." : textBox1.Text.Trim();  
  2.   
  3. var codeParams = CodeDescriptor.Init(ErrorCorrectionLevel.H, content, QuietZoneModules.Two, 5);  
  4.   
  5. codeParams.TryEncode();  
  6.   
  7. // Render the QR code as an image  
  8. using (var ms = new MemoryStream())  
  9. {  
  10.     codeParams.Render(ms);  
  11.   
  12.     Image image = Image.FromStream(ms);  
  13.     pictureBox1.Image = image;  
  14.     if (image != null)  
  15.         pictureBox1.SizeMode = image.Height > pictureBox1.Height ? PictureBoxSizeMode.Zoom : PictureBoxSizeMode.Normal;  
  16. }  


 

DEMO已经上传,有兴趣的可以下载看一下。

DEMO开发环境:VS2012

下载地址:一维、二维码生成DEMO

 

 

 

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多