码迷,mamicode.com
首页 > Windows程序 > 详细

c# ZXing 二维码 支持中文

时间:2019-10-18 15:34:26      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:文件   文本   cte   new   get   ring   ffffff   argument   测试   

public class QRCode
    {
        public static Bitmap QR(string content)
        {
            Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();
            hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");//解决中文异常
            QRCodeWriter writer = new QRCodeWriter();
            BitMatrix matrix= writer.encode(content, BarcodeFormat.QR_CODE, 300, 300,hints);
            Bitmap m = toBitmap(matrix);
            //m.Save("d:\\a.tif", ImageFormat.Tiff);
            return m;
        }
        public static Bitmap toBitmap(BitMatrix matrix)
        {
            int width = matrix.Width;
            int height = matrix.Height;
            Bitmap bmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    bmap.SetPixel(x, y,matrix[x, y]? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF"));
                }
            }
            return bmap;
        }
        public static byte[] BitmapToBytes(Bitmap Bitmap)
        {
            MemoryStream ms = null;
            try
            {
                ms = new MemoryStream();
                Bitmap.Save(ms,ImageFormat.Tiff);
                byte[] byteImage = new Byte[ms.Length];
                byteImage = ms.ToArray();
                return byteImage;
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            finally
            {
                ms.Close();
            }
        }
    }

 

public IActionResult Get()
        {
            try
            {
                var m = QRCode.QR("返回二维码测试文本!!!!");
                var n = QRCode.BitmapToBytes(m);
                new FileExtensionContentTypeProvider().Mappings.TryGetValue(".tif", out var contenttype);
                return File(n, contenttype, "qr.tif");
            }
            catch (Exception e)
            {
                return Ok(e.Message);
            }
        }

可通过此api下载二维码文件,支持中文

需要安装ZXing.Net,我的版本0.16.5

c# ZXing 二维码 支持中文

标签:文件   文本   cte   new   get   ring   ffffff   argument   测试   

原文地址:https://www.cnblogs.com/huanyun/p/11698424.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!