java 生成条形码

package com.imgread;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Code {

/*protected void Page_Load(object sender, EventArgs e) {
   Response.Write(bar_code("
www.sosuo8.com", 100, 1, 2));
}*/

public String bar_code(Object str, int ch, int cw, int type_code) {
   /*
   * str:输入的字符串.
   * ch:要显示条形码的高度.
   * cw:要显示条形码的宽度.
   * type_code:代码类型.
   */
  
   String strTmp = str.toString();
   String code = strTmp;
   // ToLower()将string转化成小写形式的副本,返回是使用指定区域的性的大小写规则。
   strTmp = strTmp.toLowerCase();
   int height = ch;
   int width = cw;

   //将传入的参数进行转化。
  /*strTmp = strTmp.replace("0", "_|_|__||_||_|");
   strTmp = strTmp.replace("1", "_||_|__|_|_||");
   strTmp = strTmp.replace("2", "_|_||__|_|_||");
   strTmp = strTmp.replace("3", "_||_||__|_|_|");
   strTmp = strTmp.replace("4", "_|_|__||_|_||");
   strTmp = strTmp.replace("5", "_||_|__||_|_|");
   strTmp = strTmp.replace("7", "_|_|__|_||_||");
   strTmp = strTmp.replace("6", "_|_||__||_|_|");
   strTmp = strTmp.replace("8", "_||_|__|_||_|");
   strTmp = strTmp.replace("9", "_|_||__|_||_|");
   strTmp = strTmp.replace("a", "_||_|_|__|_||");
   strTmp = strTmp.replace("b", "_|_||_|__|_||");
   strTmp = strTmp.replace("c", "_||_||_|__|_|");
   strTmp = strTmp.replace("d", "_|_|_||__|_||");
   strTmp = strTmp.replace("e", "_||_|_||__|_|");
   strTmp = strTmp.replace("f", "_|_||_||__|_|");
   strTmp = strTmp.replace("g", "_|_|_|__||_||");
   strTmp = strTmp.replace("h", "_||_|_|__||_|");
   strTmp = strTmp.replace("i", "_|_||_|__||_|");
   strTmp = strTmp.replace("j", "_|_|_||__||_|");
   strTmp = strTmp.replace("k", "_||_|_|_|__||");
   strTmp = strTmp.replace("l", "_|_||_|_|__||");
   strTmp = strTmp.replace("m", "_||_||_|_|__|");
   strTmp = strTmp.replace("n", "_|_|_||_|__||");
   strTmp = strTmp.replace("o", "_||_|_||_|__|");
   strTmp = strTmp.replace("p", "_|_||_||_|__|");
   strTmp = strTmp.replace("r", "_||_|_|_||__|");
   strTmp = strTmp.replace("q", "_|_|_|_||__||");
   strTmp = strTmp.replace("s", "_|_||_|_||__|");
   strTmp = strTmp.replace("t", "_|_|_||_||__|");
   strTmp = strTmp.replace("u", "_||__|_|_|_||");
   strTmp = strTmp.replace("v", "_|__||_|_|_||");
   strTmp = strTmp.replace("w", "_||__||_|_|_|");
   strTmp = strTmp.replace("x", "_|__|_||_|_||");
   strTmp = strTmp.replace("y", "_||__|_||_|_|");
   strTmp = strTmp.replace("z", "_|__||_||_|_|");
   strTmp = strTmp.replace("-", "_|__|_|_||_||");
   strTmp = strTmp.replace("*", "_|__|_||_||_|");
   strTmp = strTmp.replace("/", "_|__|__|_|__|");
   strTmp = strTmp.replace("%", "_|_|__|__|__|");
   strTmp = strTmp.replace("+", "_|__|_|__|__|");
   strTmp = strTmp.replace(".", "_||__|_|_||_|");*/  
  
  strTmp = strTmp.replace("0", "_|__||_||_");
   strTmp = strTmp.replace("1", "|_|__|_|_|");
   strTmp = strTmp.replace("2", "_||__|_|_|");
   strTmp = strTmp.replace("3", "|_||__|_|_");
   strTmp = strTmp.replace("4", "_|__||_|_|");
   strTmp = strTmp.replace("5", "|_|__||_|_");
   strTmp = strTmp.replace("7", "_|__|_||_|");
   strTmp = strTmp.replace("6", "_||__||_|_");
   strTmp = strTmp.replace("8", "|_|__|_||_");
   strTmp = strTmp.replace("9", "_||__|_||_");
   strTmp = strTmp.replace("a", "|_|_|__|_|");
   strTmp = strTmp.replace("b", "_||_|__|_|");
   strTmp = strTmp.replace("c", "|_||_|__|_");
   strTmp = strTmp.replace("d", "_|_||__|_|");
   strTmp = strTmp.replace("e", "|_|_||__|_");
   strTmp = strTmp.replace("f", "_||_||__|_");
   strTmp = strTmp.replace("g", "_|_|__||_|");
   strTmp = strTmp.replace("h", "|_|_|__||_");
   strTmp = strTmp.replace("i", "_||_|__||_");
   strTmp = strTmp.replace("j", "_|_||__||_");
   strTmp = strTmp.replace("k", "|_|_|_|__|");
   strTmp = strTmp.replace("l", "_||_|_|__|");
   strTmp = strTmp.replace("m", "|_||_|_|__");
   strTmp = strTmp.replace("n", "_|_||_|__|");
   strTmp = strTmp.replace("o", "|_|_||_|__");
   strTmp = strTmp.replace("p", "_||_||_|__");
   strTmp = strTmp.replace("r", "|_|_|_||__");
   strTmp = strTmp.replace("q", "_|_|_||__|");
   strTmp = strTmp.replace("s", "_||_|_||__");
   strTmp = strTmp.replace("t", "_|_||_||__");
   strTmp = strTmp.replace("u", "|__|_|_|_|");
   strTmp = strTmp.replace("v", "__||_|_|_|");
   strTmp = strTmp.replace("w", "|__||_|_|_");
   strTmp = strTmp.replace("x", "__|_||_|_|");
   strTmp = strTmp.replace("y", "|__|_||_|_");
   strTmp = strTmp.replace("z", "__||_||_|_");
   strTmp = strTmp.replace("-", "__|_|_||_|");
   strTmp = strTmp.replace("*", "__|_||_||_");
   strTmp = strTmp.replace("/", "__|__|_|__");
   strTmp = strTmp.replace("%", "_|__|__|__");
   strTmp = strTmp.replace("+", "__|_|__|__");
   strTmp = strTmp.replace(".", "|__|_|_||_");
  

  //strTmp = strTmp.replace("_", "<span style='height:" + height + ";width:" + width + ";background:#FFFFFF;'></span>");
   //strTmp = strTmp.replace("|", "<span style='height:" + height + ";width:" + width + ";background:#165829;'></span>");

   
  String strTmp1 = strTmp.substring(0, strTmp.length()/2);
   String strTmp2 = strTmp.substring(strTmp.length()/2, strTmp.length());

   strTmp1 = strTmp1.replace("_", "<span style='height:" + height + ";width:" + width + ";background:#FFFFFF;'></span>");
   strTmp1 = strTmp1.replace("|", "<span style='height:" + height + ";width:" + width + ";background:#000000;'></span>");

   strTmp2 = strTmp2.replace("_", "<span style='height:" + height + ";width:" + width + ";background:#FFFFFF;'></span>");
   strTmp2 = strTmp2.replace("|", "<span style='height:" + height + ";width:" + width + ";background:#0068b7;'></span>");

   strTmp = strTmp1 + strTmp2;

   if (type_code == 1) {
    return strTmp + "<BR>" + code;
   } else {
    return strTmp;
   }
}

public static void main(String[] arg){
  
   SimpleDateFormat setDateFormat = new SimpleDateFormat("yy/MM/dd kk:mm:ss");
  
   Date time = new Date();
  
   String temp = setDateFormat.format(time.getTime()).replace("/", "").replace(":", "").replace(" ", "");
  
   Code code = new Code();
  
   Object co = code.bar_code(temp, 1, 1, 1);
  
   System.out.println(co);
  
}
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值