QRCoder生成二维码

/* GetGraphic方法参数说明
                 public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, Bitmap icon = null, int iconSizePercent = 15, int iconBorderWidth = 6, bool drawQuietZones = true)
             * 
                 int pixelsPerModule:生成二维码图片的像素大小 ,我这里设置的是5 
             * 
                 Color darkColor:暗色   一般设置为Color.Black 黑色
             * 
                 Color lightColor:亮色   一般设置为Color.White  白色
             * 
                 Bitmap icon :二维码 水印图标 例如:Bitmap icon = new Bitmap(context.Server.MapPath("~/images/zs.png")); 默认为NULL ,加上这个二维码中间会显示一个图标
             * 
                 int iconSizePercent: 水印图标的大小比例 ,可根据自己的喜好设置 
             * 
                 int iconBorderWidth: 水印图标的边框
             * 
                 bool drawQuietZones:静止区,位于二维码某一边的空白边界,用来阻止读者获取与正在浏览的二维码无关的信息 即是否绘画二维码的空白边框区域 默认为true
   */
         /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult SourceQRImg(string url)
        {
            QRCodeGenerator qrGenerator = new QRCodeGenerator();

            QRCodeData qrCodeData = qrGenerator.CreateQrCode(url, QRCodeGenerator.ECCLevel.H);
            QRCode qrCode = new QRCode(qrCodeData);

            Bitmap qrCodeImage = qrCode.GetGraphic(10, Color.Black, Color.White, null, 15, 6, true);

            var bitmapBytes = BitmapToBytes(qrCodeImage);        
            return File(bitmapBytes, "image/jpeg");
        }

        private static byte[] BitmapToBytes(Bitmap img)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                img.Save(stream, ImageFormat.Jpeg);
                return stream.GetBuffer();
            }
        }
   [HttpGet]
        public FileResult GetNewImage(string code)
        {
            QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q);
            QRCode qrcode = new QRCode(qrCodeData);

            Bitmap qrCodeImage = qrcode.GetGraphic(5, Color.Black, Color.White, null, 15, 6, false);
            MemoryStream ms = new MemoryStream();
            qrCodeImage.Save(ms, ImageFormat.Jpeg);

            byte[] byData = new Byte[ms.Length];
            ms.Position = 0;
            ms.Read(byData, 0, byData.Length);
            ms.Close();

            return File(byData, @"image/jpeg");
        }
存本地
         public IActionResult Index()
        {
            var data = "qr code";
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(data, QRCodeGenerator.ECCLevel.Q);
            QRCode qrCode = new QRCode(qrCodeData);
            Bitmap qrCodeImage = qrCode.GetGraphic(30);

            string outputFileName = @"wwwroot\Images\Code.png";
            using (MemoryStream memory = new MemoryStream())
            {
                using (FileStream fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite))
                {
                    qrCodeImage.Save(memory, ImageFormat.Jpeg);
                    byte[] bytes = memory.ToArray();
                    fs.Write(bytes, 0, bytes.Length);
                }
            }

            ViewBag.ImageUrl = outputFileName.Replace("wwwroot","");
            return View();                  
        }

        <div class="text-center">
    <img src="@ViewBag.ImageUrl"/>
</div>

 

上一篇:LeetCode每日一题_21_04_22(368. 最大整除子集)


下一篇:368. 最大整除子集