940 lines
33 KiB
C#
940 lines
33 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Threading;
|
||
using Avalonia.Controls;
|
||
using Avalonia.Controls.Shapes;
|
||
using Avalonia.Interactivity;
|
||
using Avalonia.Media.Imaging;
|
||
using Avalonia.Platform;
|
||
using Avalonia.Platform.Storage;
|
||
using Avalonia.Threading;
|
||
using SkiaSharp;
|
||
using Ursa.Controls;
|
||
using 常用工具集.Base;
|
||
using 常用工具集.Utility.Qrcode;
|
||
|
||
namespace 常用工具集
|
||
{
|
||
public partial class 导航二维码生成 : UserControl
|
||
{
|
||
private static int every = 16;
|
||
private object _obj = new object();
|
||
private bool flag;
|
||
private int totalCount;
|
||
private Queue<string> qrcodeList;
|
||
SKBitmap background;
|
||
SKBitmap left;
|
||
SKBitmap down;
|
||
|
||
public 导航二维码生成()
|
||
{
|
||
InitializeComponent();
|
||
background = GetResource("background");
|
||
left = GetResource("left");
|
||
down = GetResource("down");
|
||
}
|
||
private SKBitmap GetResource(string name)
|
||
{
|
||
// 获取Pack URI
|
||
string packUri = $"avares://常用工具集/Assets/Navi/{name}.png";
|
||
Stream stream = AssetLoader.Open(new Uri(packUri));
|
||
return SKBitmap.Decode(stream);
|
||
}
|
||
|
||
private async void button1_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
string text = this.textBox1.Text.Trim();
|
||
int num;
|
||
bool flag = int.TryParse(text, out num);
|
||
SKBitmap bitmap = null;
|
||
if (!flag)
|
||
{
|
||
await MessageBox.ShowAsync("请输入数字");
|
||
return;
|
||
}
|
||
if (uiRadioButton1.IsChecked.Value)
|
||
{
|
||
if (num < 0 || num > 99999999)
|
||
{
|
||
await MessageBox.ShowAsync("数据只能在0~99999999之间");
|
||
return;
|
||
}
|
||
bitmap = this.GetBeiJiaFuDataMatrixImage2(num.ToString().PadLeft(8, '0'));
|
||
}
|
||
if (uiRadioButton2.IsChecked.Value)
|
||
{
|
||
if (num < 0 || num > 9999999)
|
||
{
|
||
MessageBox.ShowAsync("数据只能在0~9999999之间");
|
||
return;
|
||
}
|
||
bitmap = this.GetHaiKangDataMatrixImage2(num.ToString().PadLeft(7, '0'));
|
||
}
|
||
if (bitmap == null)
|
||
{
|
||
GlobalValues.Error("生成失败");
|
||
return;
|
||
}
|
||
this.pictureBox1.Source = BitmapToBitmapImage(bitmap);
|
||
|
||
var sp = GlobalValues.StorageProvider;
|
||
if (sp is null) return;
|
||
var result = await sp.OpenFolderPickerAsync(new FolderPickerOpenOptions()
|
||
{
|
||
Title = "请选择要生成的目录",
|
||
AllowMultiple = false,
|
||
});
|
||
if (result == null || result.Count == 0)
|
||
{
|
||
await MessageBox.ShowAsync("文件夹路径不能为空");
|
||
return;
|
||
}
|
||
string str2 = result.FirstOrDefault()?.Path.LocalPath;
|
||
if (!str2.EndsWith("/"))
|
||
{
|
||
str2 += "/";
|
||
}
|
||
string path = str2 + text + ".png";
|
||
if (File.Exists(path))
|
||
{
|
||
File.Delete(path);
|
||
}
|
||
Save(path, bitmap);
|
||
GlobalValues.Success("保存成功");
|
||
}
|
||
|
||
private Bitmap BitmapToBitmapImage(SKBitmap skBitmap)
|
||
{
|
||
byte[] bytes = null;
|
||
using (MemoryStream stream = new MemoryStream())
|
||
{
|
||
skBitmap.Encode(SKEncodedImageFormat.Png, 100).SaveTo(stream);
|
||
bytes = stream.ToArray();
|
||
}
|
||
using (MemoryStream stream = new MemoryStream(bytes))
|
||
{
|
||
return new Bitmap(stream);
|
||
}
|
||
}
|
||
|
||
private async void button2_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
if (uiRadioButton1.IsChecked.Value)
|
||
{
|
||
if (button2.Content.ToString() == "批量生成")
|
||
{
|
||
int start = 0;
|
||
int end = 0;
|
||
if (!CheckBeiJiaFuNum(ref start, ref end))
|
||
{
|
||
return;
|
||
}
|
||
//提示
|
||
var sp = GlobalValues.StorageProvider;
|
||
if (sp is null) return;
|
||
var result = await sp.OpenFolderPickerAsync(new FolderPickerOpenOptions()
|
||
{
|
||
Title = "请选择要生成的目录",
|
||
AllowMultiple = false,
|
||
});
|
||
if (result == null || result.Count == 0)
|
||
{
|
||
await MessageBox.ShowAsync("文件夹路径不能为空");
|
||
return;
|
||
}
|
||
string str = result.FirstOrDefault()?.Path.LocalPath;
|
||
if (!str.EndsWith("/"))
|
||
{
|
||
str = str + "/";
|
||
}
|
||
flag = true;
|
||
//开始生成线程
|
||
GlobalValues.Warning("正在执行保存操作,这将会比较耗时,请稍等");
|
||
GenerateObject obj = new GenerateObject { StartNum = start, EndNum = end, ThreadCount = (int)threadCountUpDown.Value, SavePath = str, IsA4 = uiCheckBox1.IsChecked.Value };
|
||
new Thread(GenerateBeiJiaFuFunc).Start(obj);
|
||
button2.Content = "停止生成";
|
||
}
|
||
else
|
||
{
|
||
flag = false;
|
||
button2.Content = "批量生成";
|
||
if (qrcodeList != null)
|
||
qrcodeList.Clear();
|
||
}
|
||
|
||
}
|
||
if (uiRadioButton2.IsChecked.Value)
|
||
{
|
||
if (button2.Content.ToString() == "批量生成")
|
||
{
|
||
int start = 0;
|
||
int end = 0;
|
||
if (!CheckHaiKangNum(ref start, ref end))
|
||
{
|
||
return;
|
||
}
|
||
//提示
|
||
var sp = GlobalValues.StorageProvider;
|
||
if (sp is null) return;
|
||
var result = await sp.OpenFolderPickerAsync(new FolderPickerOpenOptions()
|
||
{
|
||
Title = "请选择要生成的目录",
|
||
AllowMultiple = false,
|
||
});
|
||
if (result == null || result.Count == 0)
|
||
{
|
||
await MessageBox.ShowAsync("文件夹路径不能为空");
|
||
return;
|
||
}
|
||
string str = result.FirstOrDefault()?.Path.LocalPath;
|
||
if (!str.EndsWith("/"))
|
||
{
|
||
str = str + "/";
|
||
}
|
||
flag = true;
|
||
//开始生成线程
|
||
GlobalValues.Warning("正在执行保存操作,这将会比较耗时,请稍等");
|
||
GenerateObject obj = new GenerateObject { StartNum = start, EndNum = end, ThreadCount = (int)threadCountUpDown.Value, SavePath = str, IsA4 = uiCheckBox1.IsChecked.Value };
|
||
new Thread(GenerateHaiKangFunc).Start(obj);
|
||
button2.Content = "停止生成";
|
||
}
|
||
else
|
||
{
|
||
flag = false;
|
||
button2.Content = "批量生成";
|
||
if (qrcodeList != null)
|
||
qrcodeList.Clear();
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
private void GenerateHaiKangFunc(object obj)
|
||
{
|
||
GenerateObject parm = (GenerateObject)obj;
|
||
if (qrcodeList == null)
|
||
qrcodeList = new Queue<string>();
|
||
qrcodeList.Clear();
|
||
if (!parm.IsA4)
|
||
{
|
||
//普通生成
|
||
for (int i = parm.StartNum; i <= parm.EndNum; i++)
|
||
{
|
||
qrcodeList.Enqueue(i.ToString().PadLeft(7, '0'));
|
||
}
|
||
totalCount = qrcodeList.Count;
|
||
}
|
||
else
|
||
{
|
||
//A4生成
|
||
//计算组
|
||
int count = (parm.EndNum - parm.StartNum + 1);
|
||
int group = 0;
|
||
if (count % 12 == 0)
|
||
group = count / 12;
|
||
else
|
||
group = count / 12 + 1;
|
||
|
||
int num = parm.StartNum;
|
||
for (int i = 0; i < (group - 1); i++)
|
||
{
|
||
int st = num + 12 * i;
|
||
int ed = num + 12 * i + 11;
|
||
string str = "";
|
||
for (int j = st; j <= ed; j++)
|
||
{
|
||
str = str + j.ToString().PadLeft(7, '0') + ",";
|
||
}
|
||
str = str.Substring(0, str.Length - 1);
|
||
qrcodeList.Enqueue(str);
|
||
}
|
||
//最后一组开始
|
||
int st1 = num + 12 * (group - 1);
|
||
int ed1 = parm.EndNum;
|
||
string str1 = "";
|
||
for (int j = st1; j <= ed1; j++)
|
||
{
|
||
str1 = str1 + j.ToString().PadLeft(7, '0') + ",";
|
||
}
|
||
str1 = str1.Substring(0, str1.Length - 1);
|
||
qrcodeList.Enqueue(str1);
|
||
totalCount = qrcodeList.Count;
|
||
}
|
||
//根据线程数,创建线程
|
||
for (int i = 0; i < parm.ThreadCount; i++)
|
||
{
|
||
Thread thread = new Thread(GenerateOneHaiKangQrCode);
|
||
thread.Start(parm);
|
||
}
|
||
while (true)
|
||
{
|
||
if (!flag)
|
||
{
|
||
break;
|
||
}
|
||
//判断qrcodeList是否为空
|
||
bool isEmpty = false;
|
||
lock (_obj)
|
||
{
|
||
isEmpty = qrcodeList.Count == 0;
|
||
}
|
||
if (isEmpty && totalCount == 0)
|
||
{
|
||
Dispatcher.UIThread.Invoke(() =>
|
||
{
|
||
GlobalValues.Success("保存成功");
|
||
});
|
||
flag = false;
|
||
Dispatcher.UIThread.Invoke(() =>
|
||
{
|
||
button2.Content = "批量生成";
|
||
});
|
||
if (qrcodeList != null)
|
||
qrcodeList.Clear();
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
private void GenerateOneHaiKangQrCode(object obj)
|
||
{
|
||
GenerateObject parm = (GenerateObject)obj;
|
||
while (flag)
|
||
{
|
||
try
|
||
{
|
||
//从list中取出一个
|
||
bool isEmpty;
|
||
string txt = "";
|
||
lock (_obj)
|
||
{
|
||
isEmpty = qrcodeList.Count == 0;
|
||
if (!isEmpty)
|
||
{
|
||
txt = qrcodeList.Dequeue();
|
||
}
|
||
}
|
||
if (isEmpty)
|
||
return;
|
||
List<string> everyQrcode = txt.Split(',').ToList();
|
||
if (parm.IsA4)
|
||
{
|
||
CreateHaiKangA4AndSave(everyQrcode, parm.SavePath);
|
||
lock (_obj)
|
||
{
|
||
totalCount--;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
string path = parm.SavePath + everyQrcode[0] + ".png";
|
||
if (File.Exists(path))
|
||
{
|
||
File.Delete(path);
|
||
}
|
||
SKBitmap bitmap = this.GetHaiKangDataMatrixImage2(everyQrcode[0]);
|
||
Save(path, bitmap);
|
||
bitmap.Dispose();
|
||
bitmap = null;
|
||
lock (_obj)
|
||
{
|
||
totalCount--;
|
||
}
|
||
}
|
||
}
|
||
catch { }
|
||
}
|
||
}
|
||
|
||
private void Save(string path, SKBitmap bitmap)
|
||
{
|
||
using (var stream = File.OpenWrite(path))
|
||
{
|
||
bitmap.Encode(SKEncodedImageFormat.Png, 100).SaveTo(stream);
|
||
}
|
||
}
|
||
|
||
private bool CheckHaiKangNum(ref int start, ref int end)
|
||
{
|
||
int startNum = 0;
|
||
int endNum = 0;
|
||
try
|
||
{
|
||
startNum = Convert.ToInt32(this.textBox2.Text.Trim());
|
||
}
|
||
catch (Exception)
|
||
{
|
||
MessageBox.ShowAsync("开始数据只能为整数");
|
||
return false;
|
||
}
|
||
try
|
||
{
|
||
endNum = Convert.ToInt32(this.textBox3.Text.Trim());
|
||
}
|
||
catch (Exception)
|
||
{
|
||
MessageBox.ShowAsync("结束数据只能为整数");
|
||
return false;
|
||
}
|
||
if (startNum > 9999999)
|
||
{
|
||
MessageBox.ShowAsync("开始数据不能超过9999999");
|
||
return false;
|
||
}
|
||
if (endNum > 9999999)
|
||
{
|
||
MessageBox.ShowAsync("结束数据不能超过9999999");
|
||
return false;
|
||
}
|
||
if (startNum > endNum)
|
||
{
|
||
MessageBox.ShowAsync("开始数据不能大于结束数据");
|
||
return false;
|
||
}
|
||
start = startNum;
|
||
end = endNum;
|
||
return true;
|
||
}
|
||
|
||
private void CreateHaiKangA4AndSave(List<string> everyQrcode, string savaPath)
|
||
{
|
||
string path = "";
|
||
if (everyQrcode.Count == 1)
|
||
{
|
||
path = savaPath + everyQrcode[0] + ".png";
|
||
}
|
||
else
|
||
{
|
||
path = savaPath + Convert.ToInt32(everyQrcode[0]) + "_" + Convert.ToInt32(everyQrcode[everyQrcode.Count - 1]) + ".png";
|
||
}
|
||
if (File.Exists(path))
|
||
{
|
||
File.Delete(path);
|
||
}
|
||
//创建一张A4纸
|
||
SKBitmap a4 = new SKBitmap(2480, 3508);
|
||
SKCanvas graphics = new SKCanvas(a4);
|
||
//循环生成start 到end的单个文件
|
||
int index = 1;
|
||
foreach (string text in everyQrcode)
|
||
{
|
||
SKBitmap bitmap = this.GetHaiKangDataMatrixImage2(text);
|
||
if (index == 1)
|
||
{
|
||
graphics.DrawBitmap(bitmap, 170 + 730 * 0, 200 + 790 * 0);
|
||
}
|
||
else if (index == 2)
|
||
{
|
||
graphics.DrawBitmap(bitmap, 170 + 730 * 1, 200 + 790 * 0);
|
||
}
|
||
else if (index == 3)
|
||
{
|
||
graphics.DrawBitmap(bitmap, 170 + 730 * 2, 200 + 790 * 0);
|
||
}
|
||
else if (index == 4)
|
||
{
|
||
graphics.DrawBitmap(bitmap, 170 + 730 * 0, 200 + 790 * 1);
|
||
}
|
||
else if (index == 5)
|
||
{
|
||
graphics.DrawBitmap(bitmap, 170 + 730 * 1, 200 + 790 * 1);
|
||
}
|
||
else if (index == 6)
|
||
{
|
||
graphics.DrawBitmap(bitmap, 170 + 730 * 2, 200 + 790 * 1);
|
||
}
|
||
else if (index == 7)
|
||
{
|
||
graphics.DrawBitmap(bitmap, 170 + 730 * 0, 200 + 790 * 2);
|
||
}
|
||
else if (index == 8)
|
||
{
|
||
graphics.DrawBitmap(bitmap, 170 + 730 * 1, 200 + 790 * 2);
|
||
}
|
||
else if (index == 9)
|
||
{
|
||
graphics.DrawBitmap(bitmap, 170 + 730 * 2, 200 + 790 * 2);
|
||
}
|
||
else if (index == 10)
|
||
{
|
||
graphics.DrawBitmap(bitmap, 170 + 730 * 0, 200 + 790 * 3);
|
||
}
|
||
else if (index == 11)
|
||
{
|
||
graphics.DrawBitmap(bitmap, 170 + 730 * 1, 200 + 790 * 3);
|
||
}
|
||
else if (index == 12)
|
||
{
|
||
graphics.DrawBitmap(bitmap, 170 + 730 * 2, 200 + 790 * 3);
|
||
}
|
||
bitmap.Dispose();
|
||
bitmap = null;
|
||
index++;
|
||
}
|
||
//保存A4纸
|
||
//清内存
|
||
graphics.Flush();
|
||
graphics.Dispose();
|
||
Save(path, a4);
|
||
a4.Dispose();
|
||
a4 = null;
|
||
GC.Collect();
|
||
}
|
||
|
||
private SKBitmap GetHaiKangDataMatrixImage2(string text)
|
||
{
|
||
SKBitmap bitmap = new SKBitmap(700, 733);
|
||
SKCanvas graphics = new SKCanvas(bitmap);
|
||
|
||
graphics.DrawBitmap(background, new SKRect(0, 0, 700, 733));
|
||
//在周围画上一圈虚线
|
||
var pen = new SKPaint
|
||
{
|
||
IsAntialias = true,
|
||
Style = SKPaintStyle.Stroke,
|
||
Color = SKColors.Black,
|
||
StrokeWidth = 2,
|
||
PathEffect = SKPathEffect.CreateDash(new float[] { 11F, 5.5F }, 0)
|
||
};
|
||
#region 周边虚线
|
||
|
||
//最上面的虚线
|
||
graphics.DrawLine(0, 0, 700, 0, pen);
|
||
//左边的虚线
|
||
graphics.DrawLine(0, 0, 0, 733, pen);
|
||
//右边
|
||
graphics.DrawLine(700, 0, 700, 700, pen);
|
||
//下边
|
||
graphics.DrawLine(0, 733, 700, 733, pen);
|
||
#endregion
|
||
SKBitmap every = GetHaiKangDataMatrixImage(text);
|
||
|
||
graphics.DrawBitmap(every, new SKRect(225, 220, 225 + 246, 220 + 246));
|
||
|
||
var brush = new SKPaint
|
||
{
|
||
Color = SKColors.Black,
|
||
IsAntialias = true,
|
||
Style = SKPaintStyle.Fill
|
||
};
|
||
graphics.DrawText(text, new SKPoint(410f, 640f), new SKFont
|
||
{
|
||
Size = 16f,
|
||
Typeface = SKTypeface.FromFamilyName("Arial", SKFontStyle.Bold)
|
||
}, brush);
|
||
every.Dispose();
|
||
graphics.Flush();
|
||
graphics.Dispose();
|
||
return bitmap;
|
||
}
|
||
|
||
public SKBitmap GetHaiKangDataMatrixImage(string data)
|
||
{
|
||
return DataMatrixUtils.EncodeDataMatrix(DmtxSymbolSize.DmtxSymbol14x14, data, 280, out string svg);
|
||
}
|
||
|
||
|
||
private void GenerateBeiJiaFuFunc(object obj)
|
||
{
|
||
GenerateObject parm = (GenerateObject)obj;
|
||
if (qrcodeList == null)
|
||
qrcodeList = new Queue<string>();
|
||
qrcodeList.Clear();
|
||
if (!parm.IsA4)
|
||
{
|
||
//普通生成
|
||
for (int i = parm.StartNum; i <= parm.EndNum; i++)
|
||
{
|
||
qrcodeList.Enqueue(i.ToString().PadLeft(8, '0'));
|
||
}
|
||
totalCount = qrcodeList.Count;
|
||
}
|
||
else
|
||
{
|
||
//A4生成
|
||
//计算组
|
||
int count = (parm.EndNum - parm.StartNum + 1);
|
||
int group = 0;
|
||
if (count % 6 == 0)
|
||
group = count / 6;
|
||
else
|
||
group = count / 6 + 1;
|
||
|
||
int num = parm.StartNum;
|
||
for (int i = 0; i < (group - 1); i++)
|
||
{
|
||
int st = num + 6 * i;
|
||
int ed = num + 6 * i + 5;
|
||
string str = "";
|
||
for (int j = st; j <= ed; j++)
|
||
{
|
||
str = str + j.ToString().PadLeft(8, '0') + ",";
|
||
}
|
||
str = str.Substring(0, str.Length - 1);
|
||
qrcodeList.Enqueue(str);
|
||
}
|
||
//最后一组开始
|
||
int st1 = num + 6 * (group - 1);
|
||
int ed1 = parm.EndNum;
|
||
string str1 = "";
|
||
for (int j = st1; j <= ed1; j++)
|
||
{
|
||
str1 = str1 + j.ToString().PadLeft(8, '0') + ",";
|
||
}
|
||
str1 = str1.Substring(0, str1.Length - 1);
|
||
qrcodeList.Enqueue(str1);
|
||
totalCount = qrcodeList.Count;
|
||
}
|
||
//根据线程数,创建线程
|
||
for (int i = 0; i < parm.ThreadCount; i++)
|
||
{
|
||
Thread thread = new Thread(GenerateOneBeiJiaFuQrCode);
|
||
thread.Start(parm);
|
||
}
|
||
while (true)
|
||
{
|
||
if (!flag)
|
||
{
|
||
break;
|
||
}
|
||
//判断qrcodeList是否为空
|
||
bool isEmpty = false;
|
||
lock (_obj)
|
||
{
|
||
isEmpty = qrcodeList.Count == 0;
|
||
}
|
||
if (isEmpty && totalCount == 0)
|
||
{
|
||
Dispatcher.UIThread.Invoke(() => { GlobalValues.Success("保存成功"); });
|
||
flag = false;
|
||
Dispatcher.UIThread.Invoke(() => { button2.Content = "批量生成"; });
|
||
if (qrcodeList != null)
|
||
qrcodeList.Clear();
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
private void GenerateOneBeiJiaFuQrCode(object obj)
|
||
{
|
||
GenerateObject parm = (GenerateObject)obj;
|
||
while (flag)
|
||
{
|
||
try
|
||
{
|
||
//从list中取出一个
|
||
bool isEmpty;
|
||
string txt = "";
|
||
lock (_obj)
|
||
{
|
||
isEmpty = qrcodeList.Count == 0;
|
||
if (!isEmpty)
|
||
{
|
||
txt = qrcodeList.Dequeue();
|
||
}
|
||
}
|
||
if (isEmpty)
|
||
return;
|
||
List<string> everyQrcode = txt.Split(',').ToList();
|
||
if (parm.IsA4)
|
||
{
|
||
CreateBeiJiaFuA4AndSave(everyQrcode, parm.SavePath);
|
||
lock (_obj)
|
||
{
|
||
totalCount--;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
string path = parm.SavePath + everyQrcode[0] + ".png";
|
||
if (File.Exists(path))
|
||
{
|
||
File.Delete(path);
|
||
}
|
||
SKBitmap bitmap = this.GetBeiJiaFuDataMatrixImage2(everyQrcode[0]);
|
||
Save(path, bitmap);
|
||
bitmap.Dispose();
|
||
bitmap = null;
|
||
lock (_obj)
|
||
{
|
||
totalCount--;
|
||
}
|
||
}
|
||
}
|
||
catch { }
|
||
}
|
||
}
|
||
|
||
private bool CheckBeiJiaFuNum(ref int start, ref int end)
|
||
{
|
||
int startNum = 0;
|
||
int endNum = 0;
|
||
try
|
||
{
|
||
startNum = Convert.ToInt32(this.textBox2.Text.Trim());
|
||
}
|
||
catch (Exception)
|
||
{
|
||
MessageBox.ShowAsync("开始数据只能为整数");
|
||
return false;
|
||
}
|
||
try
|
||
{
|
||
endNum = Convert.ToInt32(this.textBox3.Text.Trim());
|
||
}
|
||
catch (Exception)
|
||
{
|
||
MessageBox.ShowAsync("结束数据只能为整数");
|
||
return false;
|
||
}
|
||
if (startNum > 99999999)
|
||
{
|
||
MessageBox.ShowAsync("开始数据不能超过99999999");
|
||
return false;
|
||
}
|
||
if (endNum > 99999999)
|
||
{
|
||
MessageBox.ShowAsync("结束数据不能超过99999999");
|
||
return false;
|
||
}
|
||
if (startNum > endNum)
|
||
{
|
||
MessageBox.ShowAsync("开始数据不能大于结束数据");
|
||
return false;
|
||
}
|
||
start = startNum;
|
||
end = endNum;
|
||
return true;
|
||
}
|
||
|
||
private void CreateBeiJiaFuA4AndSave(List<string> everyQrcode, string savaPath)
|
||
{
|
||
string path = "";
|
||
if (everyQrcode.Count == 1)
|
||
{
|
||
path = savaPath + everyQrcode[0] + ".png";
|
||
}
|
||
else
|
||
{
|
||
path = savaPath + Convert.ToInt32(everyQrcode[0]) + "_" + Convert.ToInt32(everyQrcode[everyQrcode.Count - 1]) + ".png";
|
||
}
|
||
if (File.Exists(path))
|
||
{
|
||
File.Delete(path);
|
||
}
|
||
//创建一张A4纸
|
||
SKBitmap a4 = new SKBitmap(2480, 3508);
|
||
SKCanvas graphics = new SKCanvas(a4);
|
||
//循环生成start 到end的单个文件
|
||
int index = 1;
|
||
foreach (string text in everyQrcode)
|
||
{
|
||
SKBitmap bitmap = this.GetBeiJiaFuDataMatrixImage2(text);
|
||
if (index == 1)
|
||
{
|
||
graphics.DrawBitmap(bitmap, 200, 170);
|
||
}
|
||
else if (index == 2)
|
||
{
|
||
graphics.DrawBitmap(bitmap, 1300, 170);
|
||
}
|
||
else if (index == 3)
|
||
{
|
||
graphics.DrawBitmap(bitmap, 200, 1270);
|
||
}
|
||
else if (index == 4)
|
||
{
|
||
graphics.DrawBitmap(bitmap, 1300, 1270);
|
||
}
|
||
else if (index == 5)
|
||
{
|
||
graphics.DrawBitmap(bitmap, 200, 2370);
|
||
}
|
||
else if (index == 6)
|
||
{
|
||
graphics.DrawBitmap(bitmap, 1300, 2370);
|
||
}
|
||
bitmap.Dispose();
|
||
bitmap = null;
|
||
index++;
|
||
}
|
||
//保存A4纸
|
||
graphics.Dispose();
|
||
Save(path, a4);
|
||
a4.Dispose();
|
||
a4 = null;
|
||
GC.Collect();
|
||
}
|
||
|
||
public SKBitmap GetBeiJiaFuDataMatrixImage(string data)
|
||
{
|
||
return DataMatrixUtils.EncodeDataMatrix(DmtxSymbolSize.DmtxSymbol12x12, data, 240, out string svg);
|
||
}
|
||
|
||
public SKBitmap GetBeiJiaFuDataMatrixImage2(string text)
|
||
{
|
||
List<string> list = new List<string>();
|
||
for (int i = 3; i <= 6; i++)
|
||
{
|
||
for (int j = 3; j <= 6; j++)
|
||
{
|
||
string item = string.Format("{0}{1}{2}", j, 9 - i, text);
|
||
list.Add(item);
|
||
}
|
||
}
|
||
List<SKBitmap> list2 = new List<SKBitmap>();
|
||
foreach (string str2 in list)
|
||
{
|
||
list2.Add(this.GetBeiJiaFuDataMatrixImage(str2));
|
||
}
|
||
//生成的图片大小
|
||
SKBitmap image = new SKBitmap(1360 + 4, 1360 + 4);
|
||
SKCanvas graphics = new SKCanvas(image);
|
||
//在周围画上一圈虚线
|
||
var paint = new SKPaint
|
||
{
|
||
IsAntialias = true,
|
||
Style = SKPaintStyle.Stroke,
|
||
Color = SKColors.Black,
|
||
StrokeWidth = 5,
|
||
PathEffect = SKPathEffect.CreateDash(new float[] { 11F, 5.5F }, 0)
|
||
};
|
||
#region 周边虚线
|
||
//最上面的虚线
|
||
graphics.DrawLine(0, 0, 1364, 0, paint);
|
||
//左边的虚线
|
||
graphics.DrawLine(0, 0, 0, 1364, paint);
|
||
//右边
|
||
graphics.DrawLine(1362, 0, 1362, 1362, paint);
|
||
//下边
|
||
graphics.DrawLine(0, 1362, 1362, 1362, paint);
|
||
#endregion
|
||
//graphics.FillRectangle(brush, 0, 0, 4, 0x5a8);
|
||
//graphics.FillRectangle(brush, 0, 0, 0x5a8, 4);
|
||
//graphics.FillRectangle(brush, 0x5a4, 0, 0x5a8, 0x5a8);
|
||
//graphics.FillRectangle(brush, 0, 0x5a4, 0x5a8, 0x5a8);
|
||
#region 16个二维码
|
||
//第一行
|
||
graphics.DrawBitmap(list2[0], 5 * every + 2, 5 * every + 2);
|
||
graphics.DrawBitmap(list2[1], 25 * every + 2, 5 * every + 2);
|
||
graphics.DrawBitmap(list2[2], 45 * every + 2, 5 * every + 2);
|
||
graphics.DrawBitmap(list2[3], 65 * every + 2, 5 * every + 2);
|
||
//第二行
|
||
graphics.DrawBitmap(list2[4], 5 * every + 2, 25 * every + 2);
|
||
graphics.DrawBitmap(list2[5], 25 * every + 2, 25 * every + 2);
|
||
graphics.DrawBitmap(list2[6], 45 * every + 2, 25 * every + 2);
|
||
graphics.DrawBitmap(list2[7], 65 * every + 2, 25 * every + 2);
|
||
//第三行
|
||
graphics.DrawBitmap(list2[8], 5 * every + 2, 45 * every + 2);
|
||
graphics.DrawBitmap(list2[9], 25 * every + 2, 45 * every + 2);
|
||
graphics.DrawBitmap(list2[10], 45 * every + 2, 45 * every + 2);
|
||
graphics.DrawBitmap(list2[11], 65 * every + 2, 45 * every + 2);
|
||
//第四行
|
||
graphics.DrawBitmap(list2[12], 5 * every + 2, 65 * every + 2);
|
||
graphics.DrawBitmap(list2[13], 25 * every + 2, 65 * every + 2);
|
||
graphics.DrawBitmap(list2[14], 45 * every + 2, 65 * every + 2);
|
||
graphics.DrawBitmap(list2[15], 65 * every + 2, 65 * every + 2);
|
||
#endregion
|
||
|
||
#region 上三角
|
||
var brush = new SKPaint
|
||
{
|
||
Color = SKColors.Black,
|
||
IsAntialias = true,
|
||
Style = SKPaintStyle.Fill
|
||
};
|
||
|
||
//上三角
|
||
SKPath path1 = new SKPath();
|
||
path1.MoveTo(40 * every + 2, 9 * every / 4 + 2);
|
||
path1.LineTo(40 * every + 9 * every / 4 + 2, 9 * every / 4 + 2);
|
||
path1.LineTo(40 * every + 9 * every / 4 + 2, 2);
|
||
graphics.DrawPath(path1, brush);
|
||
|
||
|
||
SKPath path2 = new SKPath();
|
||
path2.MoveTo(40 * every + 9 * every / 4 + 2 + every / 2, 9 * every / 4 + 2);
|
||
path2.LineTo(40 * every + 9 * every / 4 + 2 + every / 2, 2);
|
||
path2.LineTo(45 * every + 2, 9 * every / 4 + 2);
|
||
graphics.DrawPath(path2, brush);
|
||
#endregion
|
||
#region 右三角
|
||
//右三角
|
||
SKPath path3 = new SKPath();
|
||
path3.MoveTo(1364 - (9 * every / 4 + 2), 40 * every + 2);
|
||
path3.LineTo(1364 - (9 * every / 4 + 2), 40 * every + 9 * every / 4 + 2);
|
||
path3.LineTo(1364 - 2, 40 * every + 9 * every / 4 + 2);
|
||
graphics.DrawPath(path3, brush);
|
||
|
||
|
||
SKPath path4 = new SKPath();
|
||
path4.MoveTo(1364 - (9 * every / 4 + 2), 40 * every + 9 * every / 4 + 2 + every / 2);
|
||
path4.LineTo(1364 - 2, 40 * every + 9 * every / 4 + 2 + every / 2);
|
||
path4.LineTo(1364 - (9 * every / 4 + 2), 45 * every + 2);
|
||
graphics.DrawPath(path4, brush);
|
||
|
||
#endregion
|
||
#region 左三角 下三角
|
||
graphics.DrawBitmap(left, new SKRect(2, 40 * every + 2, 2 + 9 * every / 4, 40 * every + 2 + 5 * every));
|
||
graphics.DrawBitmap(down, new SKRect(40 * every + 2, 1364 - 2 - 9 * every / 4, 40 * every + 2 + 5 * every, 1364 - 2 - 9 * every / 4 + 9 * every / 4));
|
||
#endregion
|
||
#region 中间十字
|
||
graphics.DrawRect(40 * every + 2 - 4, 40 * every + 9 * every / 4 + 2, 5 * every + 8, 8, brush);
|
||
graphics.DrawRect(40 * every + 9 * every / 4 + 2, 40 * every + 2 - 4, 8, 5 * every + 8, brush);
|
||
#endregion
|
||
#region X Y
|
||
graphics.DrawText("Y", new SKPoint(580f, 50f), SKTextAlign.Center, new SKFont
|
||
{
|
||
Size = 50f,
|
||
Typeface = SKTypeface.FromFamilyName("Arial", SKFontStyle.Bold)
|
||
}, brush);
|
||
graphics.DrawText("X", new SKPoint(1340f, 560f), SKTextAlign.Center, new SKFont
|
||
{
|
||
Size = 50f,
|
||
Typeface = SKTypeface.FromFamilyName("Arial", SKFontStyle.Bold)
|
||
}, brush);
|
||
text = ("TAG " + text).Insert(6, " ").Insert(10, " ");
|
||
graphics.DrawText(text, new SKPoint(850f, 39f), SKTextAlign.Center, new SKFont
|
||
{
|
||
Size = 35f,
|
||
Typeface = SKTypeface.FromFamilyName("Arial", SKFontStyle.Bold)
|
||
}, brush);
|
||
#endregion
|
||
graphics.Flush();
|
||
graphics.Dispose();
|
||
foreach (SKBitmap bitmap2 in list2)
|
||
{
|
||
bitmap2.Dispose();
|
||
}
|
||
list2 = null;
|
||
GC.Collect();
|
||
////去除白色,将白色转成透明
|
||
//image.MakeTransparent(System.Drawing.Color.White);
|
||
////将image转换为8.5cm * 8.5cm(1004px) dpi改成300像素/英寸
|
||
SKBitmap bitmap = new SKBitmap(1004, 1004);
|
||
using (SKCanvas canvas = new SKCanvas(bitmap))
|
||
{
|
||
canvas.DrawBitmap(image, new SKRect(0, 0, 1004, 1004));
|
||
}
|
||
//bitmap.SetResolution(300, 300);
|
||
//return bitmap;
|
||
return bitmap;
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
public class GenerateObject
|
||
{
|
||
public int StartNum { get; set; }
|
||
public int EndNum { get; set; }
|
||
public int ThreadCount { get; set; }
|
||
public string SavePath { get; set; }
|
||
public bool IsA4 { get; set; }
|
||
}
|
||
} |