·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> .NET条形码
建议不要用CODE-39码,改用CODE-128码;
CODE-39码密度比较低,条码数字内容太多,导致条码太长,缩短长度就只能减小X尺寸,造成识读困难;
CODE-128码密度高,相同的数字生成条码更短。
你可以对比一下图中的两个条码,上面是CODE-39,下面是CODE-128,相同的内容:
解决方案:
Default.aspx
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="BarcodeTest.Default" %> 2 3 <!DOCTYPE html> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 8 <title>条形码</title> 9 <script type="text/javascript" src="jquery-latest.min.js"></script>10 <script type="text/Javascript">11 $(function () {12 $("#btnBar").click(function () {13 var rawData = $.trim($("#txtRawData").val());14 $("#imgBarcode").attr("src", "BarcodeHandler.ashx?RawData=" + rawData + "&BarHeight=50&BarcodeType=" + $("#ddlBarcodeType").val()15 + "&IsDisplayFontData=" + ($("#cbIsDisplayFontData").attr("checked") == "checked") + "&FontSize=" + $("#txtFontSize").val() + "&FontAlignment=" + $("#ddlFontAlignment").get(0).selectedIndex);16 });17 });18 </script>19 </head>20 <body>21 <form id="form1" runat="server">22 <div>23 原始码:24 <input type="text" id="txtRawData" maxlength="48" value="BJ-BJSJF-GCSG-0004-HTFY00002" />25 <br />26 编码选择:<select id="ddlBarcodeType"><option>Auto</option>27 <option>A</option>28 <option>B</option>29 <option>C</option>30 </select>31 </div>32 <br />33 字体大小:<input type="text" id="txtFontSize" value="16" />字体布局:<select id="ddlFontAlignment"><option>Near</option>34 <option selected="selected">Center</option>35 <option>Far</option>36 </select><input checked="checked" type="checkbox" id="cbIsDisplayFontData" />字体显示37 <div>38 <input type="button" id="btnBar" value="生成条形码" />39 </div>40 <div style="text-align: center;">41 <img id="imgBarcode" src="" alt="条形码" />42 </div>43 </form>44 </body>45 </html>
BarcodeHandler.ashx
1 using Barcode; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Web; 6 7 namespace BarcodeTest 8 { 9 /// <summary>10 /// author: Kenmu11 /// created by: 2014-11-0612 /// function: 条形码生成13 /// </summary>14 public class BarcodeHandler : IHttpHandler15 {16 17 public void PRocessRequest(HttpContext context)18 {19 HttpRequest request = context.Request;20 HttpResponse response = context.Response;21 response.ClearContent();22 response.ContentType = "image/jpeg";23 24 string barcodeType = request["BarcodeType"] ?? "Auto";25 string rawData = request["RawData"] ?? ((char)20).ToString() + @"123a" + ((char)18).ToString() + "ab";26 byte barHeight = byte.Parse(string.IsNullOrEmpty(request["BarHeight"]) ? "32" : request["BarHeight"]);27 bool isDisplayFontData = string.IsNullOrEmpty(request["IsDisplayFontData"]) ? true : bool.Parse(request["IsDisplayFontData"]);28 int fontSize = string.IsNullOrEmpty(request["FontSize"]) ? 16 : int.Parse(request["FontSize"]);29 int fontAlignment = string.IsNullOrEmpty(request["FontAlignment"]) ? 1 : int.Parse(request["FontAlignment"]);//0(Near左)、1(Center中)、2(Far右)30 BaseCode128 code128;31 switch (barcodeType)32 {33 case "A":34 code128 = new Code128A(rawData);35 break;36 case "B":37 code128 = new Code128B(rawData);38 break;39 case "C":40 code128 = new Code128C(rawData);41 break;42 case "Auto":43 default:44 code128 = new Code128Auto(rawData);45 break;46 }47 code128.BarHeight = barHeight;48 code128.IsDisplayFontData = isDisplayFontData;49 code128.FontSize = fontSize;50 code128.FontAlignment = (System.Drawing.StringAlignment)fontAlignment;51 52 System.Drawing.Image img = code128.GetBarCodeImage();53 img.Save(response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);54 img.Dispose();55 }56 57 public bool IsReusable58 {59 get60 {61 return false;62 }63 }64 }65 }
CharacterSet.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 //author: Kenmu 7 //created by: 2014-11-05 8 //function: 条形码 9 namespace Barcode10 {11 /// <summary> 12 /// Code128字符集 13 /// </summary> 14 internal enum CharacterSet15 {16 A,17 B,18 C19 } 20 }
IBarCode.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Drawing; 4 using System.Linq; 5 using System.Text; 6 7 //author: Kenmu 8 //created by: 2014-11-05 9 //function: 条形码10 namespace Barcode11 {12 /// <summary> 13 /// 条形码接口 14 /// </summary> 15 public interface IBarCode16 {17 string RawData { get; }18 /// <summary> 19 /// 条形码对应的数据 20 /// </summary> 21 string EncodedData { get; }22 /// <summary> 23 /// 当前条形码标准 24 /// </summary> 25 string BarCodeType { get; }26 27 /// <summary> 28 /// 得到条形码对应的图片 29 /// </summary> 30 /// <returns></returns> 31 Image GetBarCodeImage();32 }33 }
BaseCode128.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Drawing; 6 7 //author: Kenmu 8 //created by: 2014-11-06 9 //function: 条形码 10 namespace Barcode 11 { 12 /// <summary> 13 /// BaseCode128抽象类 14 /// </summary> 15 public abstract class BaseCode128 : IBarCode 16 { 17 protected Color backColor = Color.White;//条码背景色 18 protected Color barColor = Color.Black;//条码和原始数据字体颜色 19 20 /// <summary> 21 /// 当前条形码种类 22 /// </summary> 23 public string BarCodeType 24 { 25 get { return System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name; } 26 } 27 28 /// <summary> 29 /// 条形码对应的编码数据 30 /// </summary> 31 protected string _EncodedData; 32 public string EncodedData 33 { 34 get { return this._EncodedData; } 35 } 36 37 /// <summary> 38 /// 【原始数据】 39 /// </summary> 40 protected string _RawData; 41 public string RawData 42 { 43 get { return this._RawData; } 44 } 45 46 /// <summary> 47 /// 在条形码下面显示数据;如果为空,则取【原始数据】 48 /// </summary> 49 protected string _PresentationData = null; 50 public string PresentationData 51 { 52 get { return string.IsNullOrEmpty(this._PresentationData) ? this._RawData : this._Present