C# .Net 使用zxing.dll生成二维码,条形码
2016-04-22 22:52
726 查看
public static string GetBarcode(string format, string value, int? width, int? height)
{
BarcodeWriter writer = null;
Bitmap bitmap = null;
string strbase64 = string.Empty;
writer = new BarcodeWriter();
if (format == "QR_CODE")
{
EncodingOptions options = new QrCodeEncodingOptions
{
DisableECI = true,
CharacterSet = "UTF-8",
Width = width == null ? 500 : Convert.ToInt32(width),
Height = height == null ? 500 : Convert.ToInt32(height)
};
writer.Options = options;
writer.Format = BarcodeFormat.QR_CODE;
bitmap = writer.Write(value);
}
else if (format == "CODE_39")
{
/*
* 1.支持数字,字母,-,$,空格
* 2.产生的条码大
* 3.常用于许多用途
*/
writer.Format = BarcodeFormat.CODE_39;
MultiFormatWriter mutiWriter = new MultiFormatWriter();
BitMatrix bm = mutiWriter.encode(value, BarcodeFormat.CODE_39, (width == null ? 500 : Convert.ToInt32(width)), (height == null ? 150 : Convert.ToInt32(height)), null);
bitmap = new BarcodeWriter().Write(bm);
}
else if (format == "CODE_128")
{
/*
* 1.支持字母或者数字所有字符
* 2.包含3个表格更好的数据进行编码
*/
writer.Format = BarcodeFormat.CODE_128;
MultiFormatWriter mutiWriter = new MultiFormatWriter();
BitMatrix bm = mutiWriter.encode(value, BarcodeFormat.CODE_128, (width == null ? 500 : Convert.ToInt32(width)), (height == null ? 150 : Convert.ToInt32(height)), null);
bitmap = new BarcodeWriter().Write(bm);
}
else if (format == "ITF")
{
EncodingOptions options = new QrCodeEncodingOptions
{
DisableECI = true,
CharacterSet = "UTF-8",
Width = width == null ? 500 : Convert.ToInt32(width),
Height = height == null ? 150 : Convert.ToInt32(height)
};
writer.Options = options;
writer.Format = BarcodeFormat.ITF;
bitmap = writer.Write(value);
}
using (MemoryStream ms = new MemoryStream())
{
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
strbase64 = Convert.ToBase64String(arr);
}
return strbase64;
}
//引用文件下载地址
//http://files.cnblogs.com/files/guichi/Newtonsoft.rar
{
BarcodeWriter writer = null;
Bitmap bitmap = null;
string strbase64 = string.Empty;
writer = new BarcodeWriter();
if (format == "QR_CODE")
{
EncodingOptions options = new QrCodeEncodingOptions
{
DisableECI = true,
CharacterSet = "UTF-8",
Width = width == null ? 500 : Convert.ToInt32(width),
Height = height == null ? 500 : Convert.ToInt32(height)
};
writer.Options = options;
writer.Format = BarcodeFormat.QR_CODE;
bitmap = writer.Write(value);
}
else if (format == "CODE_39")
{
/*
* 1.支持数字,字母,-,$,空格
* 2.产生的条码大
* 3.常用于许多用途
*/
writer.Format = BarcodeFormat.CODE_39;
MultiFormatWriter mutiWriter = new MultiFormatWriter();
BitMatrix bm = mutiWriter.encode(value, BarcodeFormat.CODE_39, (width == null ? 500 : Convert.ToInt32(width)), (height == null ? 150 : Convert.ToInt32(height)), null);
bitmap = new BarcodeWriter().Write(bm);
}
else if (format == "CODE_128")
{
/*
* 1.支持字母或者数字所有字符
* 2.包含3个表格更好的数据进行编码
*/
writer.Format = BarcodeFormat.CODE_128;
MultiFormatWriter mutiWriter = new MultiFormatWriter();
BitMatrix bm = mutiWriter.encode(value, BarcodeFormat.CODE_128, (width == null ? 500 : Convert.ToInt32(width)), (height == null ? 150 : Convert.ToInt32(height)), null);
bitmap = new BarcodeWriter().Write(bm);
}
else if (format == "ITF")
{
EncodingOptions options = new QrCodeEncodingOptions
{
DisableECI = true,
CharacterSet = "UTF-8",
Width = width == null ? 500 : Convert.ToInt32(width),
Height = height == null ? 150 : Convert.ToInt32(height)
};
writer.Options = options;
writer.Format = BarcodeFormat.ITF;
bitmap = writer.Write(value);
}
using (MemoryStream ms = new MemoryStream())
{
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
strbase64 = Convert.ToBase64String(arr);
}
return strbase64;
}
//引用文件下载地址
//http://files.cnblogs.com/files/guichi/Newtonsoft.rar
相关文章推荐
- 从无到有写一个C#弹球小游戏(二)
- 转载:C#中的泛型
- C#外包小软件
- C#开发WINCE系统的PDA程序,GetSystemMetrics获取屏幕长宽以达到自适应分辨率
- C#字符串单词个数某个字符的个数反序大写
- C#—考题字符串2
- C# WinForm 键盘快捷键的使用
- 【C#设计模式-单例模式】
- C# 之泛型详解
- 身份证号码15位转18位 C#实现
- C#自动添加using引用命名空间
- C# 用DataSet读取数据
- C# 异常抛出-五分制
- c#删除字符串中指定字符
- C# 数组元素查找
- c#创建静态类,定义泛型方法,实现元素查找
- C#编程基础 实验(7) (3)
- C#添加系统计划任务,用于自启动应用
- C# Datatable group by 查询
- C#第六次上机