.net core下条形码二维码生成

最近做项目需要用到生成二维码,找了一圈找到了这个,收藏起来,
需要安装Nuget包:ZXing.Net.Bindings.ZKWeb.System.Drawing

/// <summary>
/// 二维码和条形码
/// </summary>
public class CodeHelper
{
// 生成二维码
public static void CreateCodeEwm(string message, string gifFileName, int width = 600, int height = 600)
{
int heig = width;
if (width > height)
{
heig = height;
width = height;
}
if (string.IsNullOrWhiteSpace(message))
{
return;
}
string dir = Path.GetDirectoryName(gifFileName);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
var w = new ZXing.QrCode.QRCodeWriter();

    BitMatrix b = w.encode(message, BarcodeFormat.QR_CODE, width, heig);
    var zzb = new ZXing.ZKWeb.BarcodeWriter();
    zzb.Options = new EncodingOptions()
    {
        Margin = 0,

    };

    Bitmap b2 = zzb.Write(b);            
    b2.Save(gifFileName, ImageFormat.Gif);
    b2.Dispose();
}

/// <summary>
/// 生成二维码返回byte数组
/// </summary>
/// <param name="message"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static byte[] CreateCodeBytes(string message, int width = 600, int height = 600)
{
    int heig = width;
    if (width > height)
    {
        heig = height;
        width = height;
    }
    if (string.IsNullOrWhiteSpace(message))
    {
        return null;
    }            
    var w = new ZXing.QrCode.QRCodeWriter();

    BitMatrix b = w.encode(message, BarcodeFormat.QR_CODE, width, heig);
    var zzb = new ZXing.ZKWeb.BarcodeWriter();
    zzb.Options = new EncodingOptions()
    {
        Margin = 0,

    };
    Bitmap b2 = zzb.Write(b);
    byte[] bytes = BitmapToArray(b2);
    return bytes;
}



/// <summary>
/// 读取二维码或者条形码从图片
/// </summary>
/// <param name="imgFile"></param>
/// <returns></returns>
public static string ReadFromImage(string imgFile)
{

    if (string.IsNullOrWhiteSpace(imgFile))
    {
        return "";
    }
    Image img = Image.FromFile(imgFile);
    Bitmap b = new Bitmap(img);

    //该类名称为BarcodeReader,可以读二维码和条形码
    var zzb = new ZXing.ZKWeb.BarcodeReader();
    zzb.Options = new DecodingOptions
    {
        CharacterSet = "UTF-8"
    };
    Result r = zzb.Decode(b);
    string resultText = r.Text;
    b.Dispose();
    img.Dispose();

    return resultText;

}

//将Bitmap  写为byte[]的方法
public static byte[] BitmapToArray(Bitmap bmp)
{
    byte[] byteArray = null;

    using (MemoryStream stream = new MemoryStream())
    {

        bmp.Save(stream, ImageFormat.Png);
        byteArray = stream.GetBuffer();
    }

    return byteArray;
}
// 生成条形码
public static void CreateCodeTxm(string message, string gifFileName, int width, int height)
{

    if (string.IsNullOrWhiteSpace(message))
    {
        return;
    }

    var w = new ZXing.OneD.CodaBarWriter();
    BitMatrix b = w.encode(message, BarcodeFormat.ITF, width, height);
    var zzb = new ZXing.ZKWeb.BarcodeWriter();
    zzb.Options = new EncodingOptions()
    {
        Margin = 3,
        PureBarcode = true
    };
    string dir = Path.GetDirectoryName(gifFileName);
    if (!Directory.Exists(dir))
    {
        Directory.CreateDirectory(dir);
    }
    Bitmap b2 = zzb.Write(b);
    b2.Save(gifFileName, ImageFormat.Gif);
    b2.Dispose();
}

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,630评论 0 17
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,067评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,785评论 18 399
  • Tecsun阅读 392评论 0 0
  • 小说创作精进营 文丨蔷薇下的阳光 精进营的成立,让我看到了更多喜欢文字的人,也看到了大家的积极与热情,看了每个人的...
    蔷薇下的阳光阅读 1,935评论 26 33