Html部分
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type="text/javascript" language="javascript" src="JS/1.5/jquery-1.5.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("#barButton").click(function() {
var raw = $.trim($("#rawData").val());
$("#imgCode").attr("src", "Code128Handler.ashx?raw=" + raw + "&type=" + $("#se").val() + "&cw=" + $("#sw").val()
+ "&hm=" + $("#hm").val() + "&vm=" + $("#vm").val() + "&sb=" + $("#cbblank").attr("checked") + "&fc=" + $("#cbfont").attr("checked")
+ "&fs=" + $("#fs").val() + "&sf=" + $("#sf").get(0).selectedIndex
);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
原始码:
<input type="text" id="rawData" maxlength="48" value="135703209730" />
<br />
编码选择:<select id="se"><option>Auto</option><option>A</option><option>B</option><option>C</option><option>GS1-128</option></select></div>
模块宽度(Pix):<select id="sw"><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option></select>
<br />
水平空白倍率:<input type="text" id="hm" value="10" />垂直空白倍率<input type="text" id="vm" value="8" />
<input type="checkbox" id="cbblank" checked="checked" />空白显示
<br />
字体大小:<input type="text" id="fs" value="16" />字体布局:<select id="sf"><option>Near</option><option selected="selected">Center</option><option>Far</option></select><input checked="checked" type="checkbox" id="cbfont" />字体显示
<div>
<input type="button" id="barButton" value="生成条形码" /></div>
<div style="text-align:center; ">
<img id="imgCode" alt="条形码" />
</div>
</form>
</body>
</html>
Code128Handler部分
public void ProcessRequest(HttpContext context)
{
HttpRequest Request = context.Request;
HttpResponse Response = context.Response;
Response.ClearContent();
Response.ContentType = "image/jpeg";
string barType = Request.QueryString["type"];
string rawData = string.IsNullOrEmpty(Request.QueryString["raw"]) ? ((char)20).ToString() + @"123a" + ((char)18).ToString() + "ab" : Request.QueryString["raw"];
if(string.IsNullOrEmpty(barType))
{
barType="C";
}
int cw = string.IsNullOrEmpty(Request.QueryString["cw"]) ? 1 : byte.Parse(Request.QueryString["cw"]);
int hm = string.IsNullOrEmpty(Request.QueryString["hm"]) ? 1 : byte.Parse(Request.QueryString["hm"]);
int vm = string.IsNullOrEmpty(Request.QueryString["vm"]) ? 1 : byte.Parse(Request.QueryString["vm"]);
bool showblank = string.IsNullOrEmpty(Request.QueryString["sb"]) ? true : bool.Parse(Request.QueryString["sb"]);
bool showfont = string.IsNullOrEmpty(Request.QueryString["fc"]) ? true : bool.Parse(Request.QueryString["fc"]);
int emSize = string.IsNullOrEmpty(Request.QueryString["fs"]) ? 16 : int.Parse(Request.QueryString["fs"]);
int textA = string.IsNullOrEmpty(Request.QueryString["sf"]) ? 1 : int.Parse(Request.QueryString["sf"]);
absCode128 code128;
switch(barType)
{
case "Auto":
code128 = new Code128Auto(rawData);
break;
case "A":
code128 = new Code128A(rawData);
break;
case "B":
code128=new Code128B(rawData);
break;
case "C":
code128 = new Code128C(rawData);
break;
default:
code128 = new GS1_128(rawData);
break;
}
code128.BarCellWidth = (byte)cw;
code128.HorizontalMulriple = (byte)hm;
code128.VerticalMulriple = (byte)vm;
code128.ShowBlank = showblank;
code128.DataDisplay = showfont;
code128.FontSize = emSize;
code128.TextAlignment = (System.Drawing.StringAlignment)textA;
System.Drawing.Image img = code128.GetBarCodeImage();
img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
img.Dispose();
}
本文介绍了一个使用JavaScript和ASP.NET实现的动态生成Code128条形码的示例。用户可以通过简单的界面输入原始数据和配置参数来定制条形码样式。文章详细展示了如何处理请求并生成不同类型的Code128条形码。
3249

被折叠的 条评论
为什么被折叠?



