翼度科技»论坛 编程开发 .net 查看内容

使用.Net Core 生成条形码,保存成图片,使用ZXing

4

主题

4

帖子

12

积分

新手上路

Rank: 1

积分
12
转载于作者Lucas汪星人:https://www.jianshu.com/p/9955b4f27501
在原先作者的基础上根据我自己修改了一些代码仅供参考:
首先需要引用NuGet包:ZXing.Net.Bindings.ZKWeb.System.Drawing
也可以使用终端开发者PowerShell使用指令安装:dotnet add package ZXing.Net.Bindings.ZKWeb.System.Drawing
然后可以自己去写一个Demo创建一个MVC控制器写入以下代码:
  1. /// <summary>
  2.         /// 生成条形码,保存成图片,使用了ZXing
  3.         /// </summary>
  4.         public IActionResult GenerateQRimage(string content)//public static byte[] GenerateQRimage(string content)
  5.         {
  6.             //初始化条形码格式,宽高,以及PureBarcode=true则不会留白框
  7.             var writer = new BarcodeWriterPixelData
  8.             {
  9.                 Format = BarcodeFormat.CODE_128,//编码格式CODE_128或者CODABAR
  10.                 Options = new EncodingOptions { Height = 31, Width = 167, PureBarcode = true, Margin = 1 }
  11.             };
  12.             var pixelData = writer.Write(content);
  13.             using (var bitmap = new System.DrawingCore.Bitmap(pixelData.Width, pixelData.Height, PixelFormat.Format32bppRgb))
  14.             using (var ms = new MemoryStream())
  15.             {
  16.                 var bitmapData = bitmap.LockBits(new System.DrawingCore.Rectangle(0, 0, pixelData.Width, pixelData.Height),
  17.                    System.DrawingCore.Imaging.ImageLockMode.WriteOnly, System.DrawingCore.Imaging.PixelFormat.Format32bppRgb);
  18.                 try
  19.                 {
  20.                     // we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image
  21.                     System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0,
  22.                        pixelData.Pixels.Length);
  23.                 }
  24.                 finally
  25.                 {
  26.                     bitmap.UnlockBits(bitmapData);
  27.                 }
  28.                 // save to stream as PNG
  29.                 bitmap.Save(ms, System.DrawingCore.Imaging.ImageFormat.Png);
  30.                 System.DrawingCore.Image image = System.DrawingCore.Bitmap.FromStream(ms, true);
  31.                 image.Save("D:\\2010-asmart-healthcare\\SmartHealthcare\\SmartHealthcare.Web\\wwwroot\\barcodeimg\" + content + ".png");
  32.                 byte[] bytes = ms.GetBuffer();
  33.                 if (bytes != null)
  34.                 {
  35.                     return Ok("生成成功");
  36.                 }
  37.                 else
  38.                 {
  39.                     return Ok("生成失败");
  40.                 }
  41.             }
  42.         }
复制代码
在这段代码中要注意引用的命名空间,如:System.DrawingCore 具体的情况请在使用到你的项目中去解决大概率就是命名空间的原因,还有代码中
image.Save("D:\\2010-asmart-healthcare\\SmartHealthcare\\SmartHealthcare.Web\\wwwroot\\barcodeimg\\" + content + ".png");具体的路径可以自己去自定义,比如可以保存到你自己的电脑文件夹中,改路径就行了。
然后创建一个MVC视图也是写一个小Demo去测试,代码如下:
  1. @{
  2.     Layout = null;
  3. }
  4.     <header>
  5.         <button id="imgOn">生成条形码</button>
  6.         @*url: '/BarCode/GetBarCode?message=' + message + "&gifFileName=" + "D:/test/test.gif" + "&width=" + 100 + "&height=" + 50,*@
  7.     </header>
复制代码
代码中的message可以自定义条形码中扫描出来的具体内容 比如我在代码中写的456789,那么我扫描条形码识别出来的内容就是456789
后端接受的参数名字叫content,具体的实现就需要结合具体的业务。
然后就可以生成条形码了。

来源:https://www.cnblogs.com/zkmblog/archive/2023/07/25/17581028.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具