C#二維碼條碼生成存入文字加快取
這是Model層
using System; using System.Collections.Generic; using System.Linq; using System.Web;
namespace Book.Models { public class BookModel { public Guid BookId { get; set; } public string BookName { get; set; } public string BookISBN { get; set; } public string BookMa { get; set; } public string ErMa { get; set; } public DateTime BookTime { get; set; } } }
這是分頁類
using System; using System.Collections.Generic; using System.Linq; using System.Web;
namespace Book.Models { /// <summary> /// 分頁類 /// </summary> public class Pager { public int count { get; set; } public List<BookModel> list { get; set; } } }
這是控制器
需要拉快取helper
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Web.Mvc; using Newtonsoft.Json; using Spire.Barcode; using System.Drawing; using ThoughtWorks.QRCode.Codec;
namespace Book.Controllers { public class KZQController : Controller { // GET: KZQ public ActionResult Index() { return View(); } List<Models.BookModel> mjihe = new List<Models.BookModel>();//定義一個存放model資訊的空集合 /// <summary> /// 新增方法 /// </summary> /// <param name="m"></param> /// <returns></returns> public int BookAdd(Models.BookModel m) { try { m.BookId = Guid.NewGuid();//新增唯一識別符號 mjihe.Add(m);//將book資訊新增到集合 List<Models.BookModel> huanjihe = CacheHelper.GetCache("Key") as List<Models.BookModel>;//定義一個存cache集合 if (huanjihe != null) { mjihe.AddRange(huanjihe);//將緩集合中的資料填到model集合中去, } CacheHelper.SetCache("Key", mjihe, TimeSpan.FromMinutes(5));//每隔5分鐘快取集合裡的資料就會被實體集合中的資料覆蓋清除
var path = Server.MapPath("\\BookTxt\\"); //定義資料夾 if (!Directory.Exists(path))//判斷是否不存在此資料夾 { Directory.CreateDirectory(path);//如果不存在則建立 } //FileMode主要是查詢Book.text檔案尾或者建立Book.text using (FileStream filewrite = new FileStream(path + "Book.txt", FileMode.Append, FileAccess.Write)) { using (StreamWriter read = new StreamWriter(filewrite))//讀檔案流 { read.Write(JsonConvert.SerializeObject(m) + ",");//將讀到的檔案流轉化字串寫入 } } return 1; } catch (Exception) { return 0; } } /// <summary> /// 生成條形碼 /// </summary> /// <param name="content"></param> /// <returns></returns> public JsonResult GenerateBarCodeBySpire(string content) { //建立BarcodeSettings物件 BarcodeSettings settings = new BarcodeSettings(); // 設定條形碼型別為EAN - 13 settings.Type = BarCodeType.EAN13; //設定條形碼資料 settings.Data = content; //使用校檢和 settings.UseChecksum = CheckSumMode.ForceEnable; //在底部顯示條形碼資料 settings.ShowTextOnBottom = true; //設定寬度 settings.X = 1f; //初始化BarCodeGenerator物件, 傳入以上設定 BarCodeGenerator generator = new BarCodeGenerator(settings); //建立條形碼圖片並儲存為PNG格式 Image image = generator.GenerateImage();
var path = Server.MapPath("\\Ma\\");//相對路徑 if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string guid = Guid.NewGuid().ToString().Replace("-", "") + ".png"; //圖片名稱 image.Save(path + guid); return Json("/Ma/" + guid); //string filePath = @"D:\Image\" + guid; //圖片儲存地址,需要絕對路徑 //image.Save(filePath, System.Drawing.Imaging.ImageFormat.Png); } /// <summary> /// 生成二維碼圖片 /// </summary> /// <param name="content"></param> /// <returns></returns> public JsonResult CreateImage(string content) { string guid = Guid.NewGuid().ToString().Replace("-", "") + ".png"; //圖片名稱 int size = 10; //二維碼中每個小點的大小 Bitmap image = CreateImgCode(content, size); //生成二維碼圖片 //儲存圖片,需要圖片的絕對地址,這是web專案 var path = Server.MapPath("\\ERMa\\");//相對路徑 if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } image.Save(path+guid); return Json("/ERMa/"+guid); } /// <summary> /// 生成二維碼圖片 /// </summary> /// <param name="codeNumber">要生成二維碼的字串</param> /// <param name="size">二維碼每個顆粒大小尺寸</param> /// <returns>二維碼圖片</returns> public Bitmap CreateImgCode(string codeNumber, int size) { //建立二維碼生成類 QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); //設定編碼模式 qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; //設定編碼測量度 qrCodeEncoder.QRCodeScale = size; //設定編碼版本 qrCodeEncoder.QRCodeVersion = 0; //設定編碼錯誤糾正 qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; //生成二維碼圖片 System.Drawing.Bitmap image = qrCodeEncoder.Encode(codeNumber); return image; }
public ActionResult Show() { return View(); } int i = 0; /// <summary> /// 分頁展示 /// </summary> /// <returns></returns> [HttpGet] public string BookShow(int PageIndex,int PageSize) { List<Models.BookModel> huanlist = CacheHelper.GetCache("Key") as List<Models.BookModel>;//將Key cache中的資料賦給集合 List<Models.BookModel> list = null;//用來存放文字中的資訊 if (i == 0 ) {
var path = Server.MapPath("\\BookTxt\\");//獲取到文字目錄路徑 using (StreamReader read = new StreamReader(path + "Book.txt"))//讀取檔案流 { string str = "[" + read.ReadToEnd().ToString().TrimEnd(',') + "]";//讀取流中資料 list = JsonConvert.DeserializeObject<List<Models.BookModel>>(str); //string json = JsonConvert.SerializeObject(list); //return json; } i++; } else { list = huanlist;//將快取集合中獲取到的資料放入文字集合 } Models.Pager m = new Models.Pager(); m.list = list.Skip((PageIndex-1)*PageSize).Take(PageSize).ToList();//分頁 m.count = list.Count(); string json = JsonConvert.SerializeObject(m); return json; }
} }
這是新增頁面
@{ Layout = null; }
<!DOCTYPE html>
<html> <head> <meta name="viewport" content="width=device-width" /> <title>BookAdd</title> <script src="~/Scripts/jquery-1.10.2.js"></script> <script> function fh() { location.href = "/KZQ/Show"; } </script> </head> <body> <div> <input type="button" onclick="fh()" name="name" value="返回" /> </div> <div> <table> <tr> <td>書名:</td> <td><input type="text" id="BookName" value="" /></td> </tr> <tr> <td>ISBN碼:</td> <td><input type="text" id="BookISBN" value="" /></td> </tr> <tr> <td><input type="button" onclick="sc()" name="name" value="生成條形碼" /></td> <td id="ma"></td> </tr> <tr> <td><input type="text" id="MaPath" value="" /></td> <td><input type="text" id="ERMaPath" value="" /></td> </tr> <tr> <td>新增時間:</td> <td><input type="date" id="BookTime" value="" /></td> </tr> <tr> <td><input type="button" onclick="add()" name="name" value="新增" /></td> </tr> </table> </div> </body> </html>
這是展示頁面
@{ Layout = null; }
<!DOCTYPE html>
<html> <head> <meta name="viewport" content="width=device-width" /> <title>Show</title> <script src="~/Scripts/jquery-1.10.2.js"></script> <link href="~/Content/bootstrap.css" rel="stylesheet" /> <script> function fh() { location.href = "/KZQ/Index"; } </script> </head> <body> <div> <input type="button" name="name" onclick="fh()" value="返回新增" /> </div> <div id="table">
</div> <nav> <div id="ye">
</div> </nav> </body> </html>