- 积分
- 2404
- 明经币
- 个
- 注册时间
- 2023-2-2
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|

楼主 |
发表于 2025-7-10 00:24:07
|
显示全部楼层
本帖最后由 箭头_Row 于 2025-7-10 00:29 编辑
優化了下語法,計時器改名了,博客上可以指定大小新建二維碼:
 - namespace JoinBox
- {
- public class CmdTestQrCodeGeneratorClass
- {
- [CommandMethod(nameof(CmdTest_QRCodeGenerator))]
- public void CmdTest_QRCodeGenerator()
- {
- // var msg = "二维码测试";
- var msg = "https://www.cnblogs.com/JJBox";
- // msg = "https://www.cnblogs.com/JJBox/p/14485956.html";
- QrCodeEncoder qr = new(msg);
- qr.Build();
- var bits = qr.ModuleMatrix;
- //这里是新建图片到桌面上
- qr.BuildBitmap();
- var desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
- var file = Path.Combine(desktop, "QRCode.png");
- qr.SaveBitmap(file);
- if (bits is null)
- return;
- using DBTrans tr = new();
- CreateQrCodeHatch(tr, bits, size: 50); //不需要背景
- }
- /// <summary>
- /// 创建二维码填充
- /// </summary>
- /// <param name="tr"></param>
- /// <param name="bits">二维码矩阵</param>
- /// <param name="needBackground">是否需要背景</param>
- /// <param name="size">尺寸</param>
- private static void CreateQrCodeHatch(
- DBTrans tr,
- IEnumerable<BitArray> bits,
- bool needBackground = true,
- int size = 100
- )
- {
- const string hatchName = "Solid";
- HatchInfo? hatchBackground = null;
- if (needBackground)
- {
- var hatchColor = Color.FromRgb(255, 255, 255); //填充背景_白色
- //var hatchColor = Color.FromRgb(255, 0, 0); //填充背景_红色
- hatchBackground = new HatchInfo(false).Mode1PreDefined(hatchName).Action(ent => ent.Color = hatchColor);
- }
- //填充前景_其他颜色
- // Color hatch2Color = Color.FromRgb(160, 200, 58); //绿色
- Color hatch2Color = Color.FromRgb(1, 1, 1); //黑色
- var hatch = new HatchInfo(false).Mode1PreDefined(hatchName).Action(ent => ent.Color = hatch2Color);
- ChangeHatch(bits, hatch, hatchBackground, size);
- hatch.Build(tr.CurrentSpace);
- hatchBackground?.Build(tr.CurrentSpace);
- //删除生成的边界
- hatch.EraseBoundary();
- hatchBackground?.EraseBoundary();
- }
- /// <summary>
- /// 修改填充
- /// </summary>
- private static void ChangeHatch(
- IEnumerable<BitArray> bits,
- HatchInfo hatch,
- HatchInfo? hatchBackground,
- int size = 100
- )
- {
- DBTrans tr = DBTrans.Top;
- //设置多段线的凸度,数量和点集一样,而且自动赋值0
- var bulges = new DoubleCollection(new double[5]);
- int row = 0;
- foreach (var bitArray in bits)
- {
- for (int column = 0; column < bitArray.Length; column++)
- {
- //创建矩形框5个点,否则填充是不闭合的
- var p1 = new Point2d(column * size, row * size);
- var p2 = new Point2d(p1.X + size, p1.Y);
- var p3 = new Point2d(p2.X, p1.Y + size);
- var p4 = new Point2d(p1.X, p3.Y);
- Point2dCollection pts = [p1, p2, p3, p4];
- if (bitArray.Get(column))
- hatch.AppendLoop(pts, bulges, tr.CurrentSpace); //前景黑块
- else
- hatchBackground?.AppendLoop(pts, bulges, tr.CurrentSpace); //背景白色
- }
- row--; //换行
- }
- }
- }
- }
|
|