CodeCreater.cs
4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;
namespace CodeTest
{
public class CodeCreater
{
public static void CreateCode(int count)
{
DateTime now = DateTime.Now;
string data = now.Year.ToString() + now.Month.ToString().PadLeft(2, '0') + now.Day.ToString().PadLeft(2, '0') + now.Hour.ToString().PadLeft(2, '0');
string targetFilePath = @"D:\Code\" + data + @"\";
if (!File.Exists(targetFilePath))
{
Directory.CreateDirectory(targetFilePath);
}
for (int i = 100000; i <= 100000+count; i++)
{
string name = "pn;" + i.ToString().PadLeft(6, '0') + ";1000";
Bitmap map = ZXingCode(name, 200, BarcodeFormat.QR_CODE);
if (map != null)
{
map.Save(targetFilePath + name + ".bmp");
}
}
}
public static Bitmap ZXingCode(string msg, int codeSizeInPixels, BarcodeFormat type)
{
if (codeSizeInPixels <= 0)
{
codeSizeInPixels = 10;
}
BarcodeWriter writer = new BarcodeWriter();
writer.Format = type;
QrCodeEncodingOptions options = new QrCodeEncodingOptions();
options.DisableECI = true;
//设置内容编码
options.CharacterSet = "UTF-8";
//设置二维码的宽度和高度
options.Width = codeSizeInPixels;
options.Height = codeSizeInPixels;
//设置二维码的边距,单位不是固定像素
options.Margin = 1;
writer.Options = options;
Bitmap map = writer.Write(msg);
return map;
}
///// <summary>
///// 生成二维码图片
///// </summary>
///// <param name="strMessage">要生成二维码的字符串</param>
///// <param name="width">二维码图片宽度</param>
///// <param name="height">二维码图片高度</param>
///// <returns></returns>
//private static Bitmap GetQRCodeByZXingNet(String strMessage, Int32 width, Int32 height)
//{
// Bitmap result = null;
// try
// {
// BarcodeWriter barCodeWriter = new BarcodeWriter();
// barCodeWriter.Format = BarcodeFormat.QR_CODE;
// barCodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
// barCodeWriter.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
// barCodeWriter.Options.Height = height;
// barCodeWriter.Options.Width = width;
// barCodeWriter.Options.Margin = 0;
// ZXing.Common.BitMatrix bm = barCodeWriter.Encode(strMessage);
// result = barCodeWriter.Write(bm);
// }
// catch (Exception ex)
// {
// //异常输出
// }
// return result;
//}
//public bool DecodeQRCode(Bitmap bmp, out string text)
//{
// DecodingOptions option = new DecodingOptions();
// //option.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.QR_CODE, BarcodeFormat.All_1D };
// option.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.QR_CODE };
// BarcodeReader br = new BarcodeReader();
// br.Options = option;
// Result rs = br.Decode(bmp);
// if (rs == null)
// {
// text = "";
// return false;
// }
// else
// {
// text = rs.Text;
// return true;
// }
//}
///// <summary>
///// DataMatrix矩阵二维码
///// </summary>
///// <param name="text"></param>
///// <param name="size"></param>
///// <returns></returns>
//public static Bitmap DataMatrix(string text, int size)
//{
// if (size < 10) size = 10;
// DataMatrix.net.DmtxImageEncoderOptions opt = new DataMatrix.net.DmtxImageEncoderOptions();
// opt.ModuleSize = size;
// opt.MarginSize = 5;
// DataMatrix.net.DmtxImageEncoder encoder = new DataMatrix.net.DmtxImageEncoder();
// Bitmap bm = encoder.EncodeImage(text, opt);
// return bm;
//}
}
}