2024-12-11 17:24:15 +08:00
2024-12-18 15:06:02 +08:00
using Microsoft.AspNetCore.Hosting.Server ;
using System ;
using System.IO ;
2024-12-11 17:24:15 +08:00
using ZXing ;
using ZXing.Common ;
2024-12-18 15:06:02 +08:00
using ZXing.QrCode ;
//using ZXing.ZKWeb.System.Drawing;
2024-12-11 17:24:15 +08:00
namespace DOAN.Infrastructure.Helper ;
public class PrintHelper
{
/// <summary>
/// 打印条形码
/// </summary>
/// <param name="message"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
2024-12-18 15:06:02 +08:00
//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
// };
2024-12-11 17:24:15 +08:00
2024-12-18 15:06:02 +08:00
// Bitmap b2 = zzb.Write(b);
2024-12-11 17:24:15 +08:00
2024-12-18 15:06:02 +08:00
// using (var ms = new System.IO.MemoryStream())
// {
// b2.Save(ms, ImageFormat.Png);
// var bytes = ms.ToArray();
2024-12-11 17:24:15 +08:00
2024-12-18 15:06:02 +08:00
// return bytes;
// }
2024-12-11 17:24:15 +08:00
2024-12-18 15:06:02 +08:00
//}
2024-12-11 17:24:15 +08:00
2024-12-17 18:31:31 +08:00
/// <summary>
/// 打印二维码
2024-12-18 15:06:02 +08:00
/// https://ironsoftware.com/csharp/barcode/blog/compare-to-other-components/zxing-net-generate-qr-code-barcode-alternatives/
2024-12-17 18:31:31 +08:00
/// </summary>
/// <param name="message"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte [ ] CreateQcCode ( string message , int width , int height )
{
if ( string . IsNullOrWhiteSpace ( message ) )
{
return null ;
}
2024-12-18 15:06:02 +08:00
Byte [ ] byteArray ;
var margin = 0 ;
var qrCodeWriter = new ZXing . BarcodeWriterPixelData
2024-12-17 18:31:31 +08:00
{
2024-12-18 15:06:02 +08:00
Format = ZXing . BarcodeFormat . QR_CODE ,
Options = new QrCodeEncodingOptions
{
Height = height ,
Width = width ,
Margin = margin
}
2024-12-17 18:31:31 +08:00
} ;
2024-12-18 15:06:02 +08:00
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 ) )
2024-12-17 18:31:31 +08:00
{
2024-12-18 15:06:02 +08:00
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 ( ) ;
}
2024-12-17 18:31:31 +08:00
}
2024-12-18 15:06:02 +08:00
return byteArray ;
2024-12-17 18:31:31 +08:00
}
2024-12-11 17:24:15 +08:00
}