CodeCreater.cs
6.5 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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;A" + 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 static string DecodeQRCode(Bitmap bmp)
{
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 = "";
}
else
{
text = ConvertISO88591ToEncoding(rs.ToString(), Encoding.Default);
}
return text;
}
//privatre void TestDecodeISO88591(string RssUrl)
//{
// string sResult = "";
// System.IO.Stream ResponseStream = null;
// HttpWebResponse hwrp = null;
// System.IO.StreamReader oStreamReader = null;
// Encoding UrlEncoding;
// System.Net.HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(RssUrl);
// hwr.Method = "GET";
// hwrp = (HttpWebResponse)hwr.GetResponse();
// UrlEncoding = Encoding.GetEncoding(hwrp.CharacterSet);
// ResponseStream = hwrp.GetResponseStream();
// oStreamReader = new System.IO.StreamReader(ResponseStream, UrlEncoding);
// sResult = oStreamReader.ReadToEnd();
// if (hwrp.CharacterSet == "ISO-8859-1") //如果编码为ISO-8859-1才转换
// {
// sResult = ConvertISO88591ToEncoding(sResult, Encoding.Default);
// }
// hwrp.Close();
// //处理RSS返回的数据
// //.......
//}
//转换
private static string ConvertISO88591ToEncoding(string srcString, Encoding dstEncode)
{
String sResult;
Encoding ISO88591Encoding = Encoding.GetEncoding("ISO-8859-1");
Encoding GB2312Encoding = Encoding.GetEncoding("GB2312"); //这个地方很特殊,必须利用GB2312编码
byte[] srcBytes = ISO88591Encoding.GetBytes(srcString);
//将原本存储ISO-8859-1的字节数组当成GB2312转换成目标编码(关键步骤)
byte[] dstBytes = Encoding.Convert(GB2312Encoding, dstEncode, srcBytes);
char[] dstChars = new char[dstEncode.GetCharCount(dstBytes, 0, dstBytes.Length)];
dstEncode.GetChars(dstBytes, 0, dstBytes.Length, dstChars, 0);//利用char数组存储字符
sResult = new string(dstChars);
return sResult;
}
//}
///// <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;
//}
}
}