总结“条形码”的方法
一、心得:从事软件开发半年多,善于总结,能在用到的时候拿出来使用,既省时还节约开发成本,这是个好的习惯,希望看过文章的朋友也要意识到这点,在这里介绍自己总结的条形码的方法,后续会引进更多试用的方法,因为工作时间不是太长,如果有错误希望大家指正。
二、开始介绍“条形码方法”。。。
1、现在维护一个执业师注册系统的项目,其中有打印表的功能,还得在表的最上边自动生成“条形码”,这样的表格更具有防伪性,打印出来,相关部门审核。
2、现在用的是MVC2开发的系统,包括:Model、View、Controller
三、详细的代码:
1、前端(也就是View视图中)(“TestCode”为方法(Action),“ExamRegister”为Controller)
<img style="border: 0px; height: 40px;" src="<%:Url.Action("TestCode", "ExamRegister")%>" />
2、Controller中的Action,返回类型:File,MVC中ViewResult中的一种机制,其中调用了“CreateCodeLogo”方法。
1 public ActionResult TestCode() 2 { 3 CreateCodeLogos ccl = new CreateCodeLogos(); 4 ccl.CreateCodeLogo(Server.MapPath("~/Content/images/"));//创建LOGO 6 var imagePath = Server.MapPath("~/Content/images/new.stream"); 7 using (var fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read)) 8 { 9 // 读取文件的 byte[] 10 byte[] bytes = new byte[fileStream.Length]; 11 fileStream.Read(bytes, 0, bytes.Length); 12 fileStream.Flush(); 13 fileStream.Close(); 14 // 把 byte[] 转换成 Stream 15 Stream stream = new MemoryStream(bytes); 16 return File(stream, "image/x-Jpeg"); 17 }; 18 }
3、CreateCodeLogo方法,其中用到了GDI+,文件流的读取。
1 /// 生成条形码 2 /// </summary> 3 public void CreateCodeLogo(string path) 4 { 5 Random rd = new Random(); 6 Int64 rdSum = rd.Next(10000000, 100000000); 7 string code = (rdSum * 555).ToString() + DateTime.Now.ToString("yyyymmddhhmmss") + (rdSum * 777).ToString();// 8 //string path = Server.MapPath("~/Content/images/"); 9 long len = code.Length; 10 string lastString = ""; 11 char[] list = new char[len + 1]; 12 list = code.ToCharArray(); 13 for (int i = 0; i < list.Length; i++) 14 { 15 lastString += ConvertToBinaryString(list[i].ToString()); 16 } 17 char[] numList = new char[lastString.Length + 1]; 18 numList = lastString.ToCharArray(); 19 20 Bitmap image = new Bitmap(330, 40); 21 Graphics g = Graphics.FromImage(image); 22 g.Clear(Color.White); 23 Pen penBlack = new Pen(Color.FromArgb(255, 0, 0, 0), 2.5F); 24 Pen penWhite = new Pen(Color.White, 2.5F); 25 int j = 0; 26 for (float k = 10; j < numList.Length; k += 2F, j++) 27 { 28 if (numList[j].ToString() == "1") 29 { 30 g.DrawLine(penBlack, k, 10, k, 50); 31 } 32 else 33 { 34 g.DrawLine(penWhite, k, 10, k, 50); 35 } 36 if (j % 4 == 0) 37 { 38 g.DrawString(list[j / 4].ToString(), new System.Drawing.Font("Courier New", 12), new SolidBrush(Color.Blue), k, 52); 39 } 40 } 41 using (var ms = new MemoryStream()) 42 { 43 image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 44 ms.Seek(0, SeekOrigin.Begin); 45 path = path + "new.Stream";//获取服务器路径 46 using (var fileStream = new FileStream(path, FileMode.Create)) 47 { 48 byte[] bytes = new byte[ms.Length]; 49 ms.Read(bytes, 0, bytes.Length); 50 fileStream.Write(bytes, 0, Convert.ToInt32(ms.Length)); 51 }; 52 }; 53 }
4、上面方法使用到的ConvertToBinaryString(将字符串数值转换为二进制字符串数值方法)
1 public string ConvertToBinaryString(string buf) 2 { 3 int[] temp = new int[20]; 4 string binary; 5 int val = 0, i = 0, j; 6 //先将字符转化为十进制数 7 try 8 { 9 val = Convert.ToInt32(buf); 10 } 11 catch 12 { 13 val = 0; 14 } 15 if (val == 0) 16 { 17 return ("1111"); 18 } 19 i = 0; 20 while (val != 0) 21 { 22 temp[i++] = val % 2; 23 val /= 2; 24 } 25 binary = ""; 26 for (j = 0; j <= i - 1; j++) 27 { 28 binary += (char)(temp[i - j - 1] + 48); 29 } 30 if (binary.Length < 4) //如果小于4位左边补零 31 { 32 int len = 4 - binary.Length; 33 string str = ""; 34 while (len > 0) 35 { 36 str += "0"; 37 len--; 38 } 39 binary = str + binary; 40 } 41 return (binary); 42 43 }