赞
踩
首先当然是要先注入 ZXing的包了, ZXing.Net 这个包在linux 是不受支持的, 所以这边注入的包:
ZXing.Net.Bingdings.imagesSharp.V2
注入了这个包以后, 接下去的就都简单了:
- /// <summary>
- /// 生成二维码
- /// </summary>
- /// <param name="value">value</param>
- /// <param name="barcodeFormat">生成的类型 CODE_39/ CODE_93/ CODE_128/ QR_CODE ....</param>
- /// <param name="pathUrl">保存路径</param>
- /// <param name="fileName">文件名字</param>
- /// <param name="width">二维码宽,默认500</param>
- /// <param name="height">二维码高,默认500</param>
- public static void GenerateCode(string value, string barcodeFormat, string pathUrl, string fileName, int width = 500, int height = 500)
- {
- var barcodeFormatType = (BarcodeFormat)Enum.Parse(typeof(BarcodeFormat), barcodeFormat);
- var writer = new ZXing.ImageSharp.BarcodeWriter<Rgba32>
- {
-
- Format = barcodeFormatType,
- Options = new ZXing.QrCode.QrCodeEncodingOptions
- {
- DisableECI = true,
- CharacterSet = "UTF-8",
- Width = width,
- Height = height,
- Margin = 1
- }
- };
- var image = writer.WriteAsImageSharp<Rgba32>(value);
- var ms = new MemoryStream();
- image.Save(ms, new PngEncoder());
- pathUrl = pathUrl + "/";
- using (var fileStream = File.Create(pathUrl + fileName))
- {
- ms.Seek(0, SeekOrigin.Begin);
- ms.CopyTo(fileStream);
- }
-
- }
其中可能会不清楚的就是: barcodeFormat 这个参数吧; 没事, 有截图
然后就解决了;
顺说一下, 如果你需要图片转成流, 看我这个(但是我只会 仅支持windows的方法):
注入: System.Drawing.Common这个包;
var watermarkedStream = new MemoryStream();
using (var img = Image.FromStream(stream)) //流转图片
{
img.Save(memoryStream, ImageFormat.Png);
//图片再转成流, 存到 memoryStream
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。