/*条形码生成函数
*Author: Jone
*/
//主函数
// str 待获取的字符串,w 单个条形码的长度,h 条形码的高度
public string CreateCode(string str, int w, int h)
{
//alert(str);
string returnStr = "字符串不能为空";
if (w < 1)
w = 1;
if (h < 1)
h = 52;
if (!"".Equals(str))
{
string regstr = "^[a-zA-Z0-9+%*/$.-]{1,}$";
Regex reg = new Regex(regstr, RegexOptions.IgnoreCase);
str = str.ToLower();
if (reg.IsMatch(str))
{
str = "*" + str + "*";
string code = getCode(str);
int len = code.Length;
code = code.Replace("_", "<span x=1 style='float:left;height:" + h + "px;width:" + w + "px;background:#FFFFFF'></span>");
code = code.Replace("|", "<span x=2 style='float:left;height:" + h + "px;width:" + w + "px;background:#000000'></span>");
returnStr = "<div style='display:block;width:" + len + "px'>" + code + "</div>";
}
else
{
returnStr = "字符串格式不对";
}
}
return returnStr;
}
//将code转换成下面样式
protected string getCode(string str)
{
str = str.Replace("0", "_|_|__||_||_|");
str = str.Replace("1", "_||_|__|_|_||");
str = str.Replace("2", "_|_||__|_|_||");
str = str.Replace("3", "_||_||__|_|_|");
str = str.Replace("4", "_|_|__||_|_||");
str = str.Replace("5", "_||_|__||_|_|");
str = str.Replace("7", "_|_|__|_||_||");
str = str.Replace("6", "_|_||__||_|_|");
str = str.Replace("8", "_||_|__|_||_|");
str = str.Replace("9", "_|_||__|_||_|");
str = str.Replace("a", "_||_|_|__|_||");
str = str.Replace("b", "_|_||_|__|_||");
str = str.Replace("c", "_||_||_|__|_|");
str = str.Replace("d", "_|_|_||__|_||");
str = str.Replace("e", "_||_|_||__|_|");
str = str.Replace("f", "_|_||_||__|_|");
str = str.Replace("g", "_|_|_|__||_||");
str = str.Replace("h", "_||_|_|__||_|");
str = str.Replace("i", "_|_||_|__||_|");
str = str.Replace("j", "_|_|_||__||_|");
str = str.Replace("k", "_||_|_|_|__||");
str = str.Replace("l", "_|_||_|_|__||");
str = str.Replace("m", "_||_||_|_|__|");
str = str.Replace("n", "_|_|_||_|__||");
str = str.Replace("o", "_||_|_||_|__|");
str = str.Replace("p", "_|_||_||_|__|");
str = str.Replace("r", "_||_|_|_||__|");
str = str.Replace("q", "_|_|_|_||__||");
str = str.Replace("s", "_|_||_|_||__|");
str = str.Replace("t", "_|_|_||_||__|");
str = str.Replace("u", "_||__|_|_|_||");
str = str.Replace("v", "_|__||_|_|_||");
str = str.Replace("w", "_||__||_|_|_|");
str = str.Replace("x", "_|__|_||_|_||");
str = str.Replace("y", "_||__|_||_|_|");
str = str.Replace("z", "_|__||_||_|_|");
str = str.Replace("-", "_|__|_|_||_||");
str = str.Replace("*", "_|__|_||_||_|");
str = str.Replace("/", "_|__|__|_|__|");
str = str.Replace("%", "_|_|__|__|__|");
str = str.Replace("+", "_|__|_|__|__|");
str = str.Replace("$", "_|__|_|__|__|");
str = str.Replace(".", "_||__|_|_||_|");
return str;
}