This commit is contained in:
qianhao.xu
2024-12-18 15:06:02 +08:00
parent f4f612a39e
commit ac7829ab6c
7 changed files with 114 additions and 93 deletions

View File

@@ -14,7 +14,7 @@
<PackageReference Include="AspectCore.Abstractions" Version="2.4.0" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="8.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="QuestPDF" Version="2025.1.0-alpha0" />
<PackageReference Include="System.Drawing.Common" Version="9.0.0" />
<PackageReference Include="UAParser" Version="3.1.47" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.10" />
<PackageReference Include="JinianNet.JNTemplate" Version="2.4.2" />
@@ -23,7 +23,6 @@
<PackageReference Include="AspNetCoreRateLimit" Version="5.0.0" />
<PackageReference Include="IP2Region.Net" Version="2.0.2" />
<PackageReference Include="ZXing.Net" Version="0.16.9" />
<PackageReference Include="ZXing.Net.Bindings.ZKWeb.System.Drawing" Version="0.16.7" />
</ItemGroup>
</Project>

View File

@@ -1,11 +1,11 @@
using System.Drawing;
using System.Drawing.Imaging;
using System.DrawingCore;
using System.DrawingCore.Imaging;
using Microsoft.AspNetCore.Hosting.Server;
using System;
using System.IO;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;
//using ZXing.ZKWeb.System.Drawing;
namespace DOAN.Infrastructure.Helper;
@@ -19,37 +19,38 @@ public class PrintHelper
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] CreateBarCode(string message, int width, int height)
{
if (string.IsNullOrWhiteSpace(message))
{
return null;
}
var w = new ZXing.OneD.Code128Writer();
BitMatrix b = w.encode(message, BarcodeFormat.CODE_128, width, height);
var zzb = new ZXing.ZKWeb.BarcodeWriter();
zzb.Options = new EncodingOptions()
{
Margin = 0,
PureBarcode = false
};
//public static byte[] CreateBarCode(string message, int width, int height)
//{
// if (string.IsNullOrWhiteSpace(message))
// {
// return null;
// }
// var w = new ZXing.OneD.Code128Writer();
// BitMatrix b = w.encode(message, BarcodeFormat.CODE_128, width, height);
// var zzb = new ZXing.ZKWeb.BarcodeWriter();
// zzb.Options = new EncodingOptions()
// {
// Margin = 0,
// PureBarcode = false
// };
Bitmap b2 = zzb.Write(b);
// Bitmap b2 = zzb.Write(b);
using (var ms = new System.IO.MemoryStream())
{
b2.Save(ms, ImageFormat.Png);
var bytes = ms.ToArray();
// using (var ms = new System.IO.MemoryStream())
// {
// b2.Save(ms, ImageFormat.Png);
// var bytes = ms.ToArray();
return bytes;
}
// return bytes;
// }
}
//}
/// <summary>
/// 打印二维码
/// https://ironsoftware.com/csharp/barcode/blog/compare-to-other-components/zxing-net-generate-qr-code-barcode-alternatives/
/// </summary>
/// <param name="message"></param>
/// <param name="width"></param>
@@ -61,25 +62,40 @@ public class PrintHelper
{
return null;
}
var w = new ZXing.OneD.Code128Writer();
BitMatrix b = w.encode(message, BarcodeFormat.QR_CODE, width, height);
var zzb = new ZXing.ZKWeb.BarcodeWriter();
zzb.Options = new EncodingOptions()
Byte[] byteArray;
var margin = 0;
var qrCodeWriter = new ZXing.BarcodeWriterPixelData
{
Margin = 0,
PureBarcode = false
Format = ZXing.BarcodeFormat.QR_CODE,
Options = new QrCodeEncodingOptions
{
Height = height,
Width = width,
Margin = margin
}
};
Bitmap b2 = zzb.Write(b);
using (var ms = new System.IO.MemoryStream())
var pixelData = qrCodeWriter.Write(message);
// creating a PNG bitmap from the raw pixel data; if only black and white colors are used it makes no difference if the raw pixel data is BGRA oriented and the bitmap is initialized with RGB
using (var bitmap = new System.Drawing.Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
{
b2.Save(ms, ImageFormat.Png);
var bytes = ms.ToArray();
return bytes;
using (var ms = new MemoryStream())
{
var bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, pixelData.Width, pixelData.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
try
{
// we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image
System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
}
finally
{
bitmap.UnlockBits(bitmapData);
}
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byteArray = ms.ToArray();
}
}
return byteArray;
}