java 生成条形码

来源:互联网 发布:汽车黑匣子数据记录 编辑:程序博客网 时间:2024/05/01 02:09

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);
  
 }
}