[HttpGet("{type}/{code}/{width}/{height}/{scale}")] //Get /api/design/barcode/BarCode128/123456/200/100/1 public async Task Barcode(string type, string code, int width, int height, float scale) { //设置当前用户会话 RuntimeContext.Current.CurrentSession = HttpContext.Session.LoadWebSession(); //判断权限 if (!(RuntimeContext.Current.CurrentSession is IDeveloperSession)) { throw new Exception("Must login as a Developer"); } var bw = new ZXing.SkiaSharp.BarcodeWriter(); //TODO:优化单例ZXing.BarCodeRender bw.Format = (ZXing.BarcodeFormat)Enum.Parse(typeof(ZXing.BarcodeFormat), type); bw.Options.Height = (int)(height * scale); bw.Options.Width = (int)(width * scale); var skbmp = bw.Write(code); using var bmp = new Drawing.Bitmap(skbmp); using var ms = new System.IO.MemoryStream(1024); bmp.Save(ms, Drawing.ImageFormat.Jpeg, 90); ms.Position = 0; HttpContext.Response.ContentType = "image/jpg"; await ms.CopyToAsync(HttpContext.Response.Body); //bmp.Save(HttpContext.Response.Body, Drawing.ImageFormat.Png); //不支持同步直接写 }
public byte[] Generate(string content, BarcodeType type, int?width, int?height, int?margin) { ZXing.BarcodeFormat barcodeFormat; switch (type) { case BarcodeType.AZTEC: barcodeFormat = ZXing.BarcodeFormat.AZTEC; break; case BarcodeType.CODABAR: barcodeFormat = ZXing.BarcodeFormat.CODABAR; break; case BarcodeType.CODE_39: barcodeFormat = ZXing.BarcodeFormat.CODE_39; break; case BarcodeType.CODE_93: barcodeFormat = ZXing.BarcodeFormat.CODE_93; break; case BarcodeType.CODE_128: barcodeFormat = ZXing.BarcodeFormat.CODE_128; break; case BarcodeType.DATA_MATRIX: barcodeFormat = ZXing.BarcodeFormat.DATA_MATRIX; break; case BarcodeType.EAN_8: barcodeFormat = ZXing.BarcodeFormat.EAN_8; break; case BarcodeType.EAN_13: barcodeFormat = ZXing.BarcodeFormat.EAN_13; break; case BarcodeType.ITF: barcodeFormat = ZXing.BarcodeFormat.ITF; break; case BarcodeType.MAXICODE: barcodeFormat = ZXing.BarcodeFormat.MAXICODE; break; case BarcodeType.PDF_417: barcodeFormat = ZXing.BarcodeFormat.PDF_417; break; case BarcodeType.QR_CODE: barcodeFormat = ZXing.BarcodeFormat.QR_CODE; break; case BarcodeType.RSS_14: barcodeFormat = ZXing.BarcodeFormat.RSS_14; break; case BarcodeType.RSS_EXPANDED: barcodeFormat = ZXing.BarcodeFormat.RSS_EXPANDED; break; case BarcodeType.UPC_A: barcodeFormat = ZXing.BarcodeFormat.UPC_A; break; case BarcodeType.UPC_E: barcodeFormat = ZXing.BarcodeFormat.UPC_E; break; case BarcodeType.UPC_EAN_EXTENSION: barcodeFormat = ZXing.BarcodeFormat.UPC_EAN_EXTENSION; break; case BarcodeType.MSI: barcodeFormat = ZXing.BarcodeFormat.MSI; break; case BarcodeType.PLESSEY: barcodeFormat = ZXing.BarcodeFormat.PLESSEY; break; case BarcodeType.IMB: barcodeFormat = ZXing.BarcodeFormat.IMB; break; default: throw new ArgumentException(); } var barcodeWriter = new ZXing.SkiaSharp.BarcodeWriter { Format = barcodeFormat, }; if (width.HasValue || height.HasValue || margin.HasValue) { barcodeWriter.Options = new EncodingOptions(); if (width.HasValue) { barcodeWriter.Options.Width = width.Value; } if (height.HasValue) { barcodeWriter.Options.Height = height.Value; } if (margin.HasValue) { barcodeWriter.Options.Margin = margin.Value; } } using (var image = barcodeWriter.Write(content)) { using (var memStream = new MemoryStream()) { using (var wstream = new SKManagedWStream(memStream)) { if (image.Encode(wstream, SKEncodedImageFormat.Png, 100)) { return(memStream.ToArray()); } return(null); } } } }