本项目参考了micjahn/ZXing.Net,只编写了生成器部分,并对处理逻辑进行了大量优化,构建后Z.QRCodeEncoder.Net.dll
文件仅40kb
注意:本项目不提供二维码绘制方法,如需绘制请看使用示例
Java
: ALI1416/qrcode-encoderJavaScript
: ALI1416/qrcode-encoder-js
dotnet add package Z.QRCodeEncoder.Net
参数名 | 中文名 | 类型 | 默认值 |
---|---|---|---|
content | 内容 | string | (无) |
level | 纠错等级 | int | 0 |
mode | 编码模式 | int | (自动探测) |
versionNumber | 版本号 | int | (最小版本) |
参数名 | 中文名 | 类型 | 默认值 |
---|---|---|---|
length | 内容字节数 | int | (无) |
level | 纠错等级 | int | (无) |
mode | 编码模式 | int | (无) |
versionNumber | 版本号 | int | (最小版本) |
参数名 | 中文名 | 类型 |
---|---|---|
data | 数据 | bool[] |
version | 版本 | Version |
level | 纠错等级 | int |
值 | 等级 | 纠错率 |
---|---|---|
0 | L | 7% |
1 | M | 15% |
2 | Q | 25% |
3 | H | 30% |
值 | 模式 | 备注 |
---|---|---|
0 | NUMERIC | 数字0-9 |
1 | ALPHANUMERIC | 数字0-9、大写字母A-Z、符号(空格)$%*+-./: |
2 | BYTE(ISO-8859-1) | 兼容ASCII |
3 | BYTE(UTF-8) |
取值范围:[1,40]
Program.cs
string content = "1234😀";
int level = 0;
QRCode qrCode = new QRCode(content, level);
Bitmap bitmap = ImageUtils.QrBytes2Bitmap(qrCode.Matrix, 10);
ImageUtils.SaveBitmap(bitmap, "qr.png");
ImageUtils.cs
private static readonly Brush BLACK_BRUSH = new SolidBrush(Color.Black);
public static Bitmap QrBytes2Bitmap(bool[,] bytes, int pixelSize)
{
int length = bytes.GetLength(0);
List<Rectangle> rects = new List<Rectangle>();
for (int x = 0; x < length; x++)
{
for (int y = 0; y < length; y++)
{
if (bytes[x, y])
{
rects.Add(new Rectangle((x + 1) * pixelSize, (y + 1) * pixelSize, pixelSize, pixelSize));
}
}
}
int size = (length + 2) * pixelSize;
Bitmap bitmap = new Bitmap(size, size);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.FillRectangles(BLACK_BRUSH, rects.ToArray());
}
return bitmap;
}
public static void SaveBitmap(Bitmap bitmap, string path)
{
bitmap.Save(path, ImageFormat.Png);
}
更多请见测试