当前位置:   article > 正文

使用Zxing生成二维码_使用zxing生成二维码提示非gs1标准

使用zxing生成二维码提示非gs1标准

1.指定大小文本生成二维码

  1. private Bitmap GetQRCodeByZXingNet(String strMessage, Int32 width, Int32 height)
  2. {
  3. Bitmap result = null;
  4. try
  5. {
  6. BarcodeWriter barCodeWriter = new BarcodeWriter();
  7. barCodeWriter.Format = BarcodeFormat.QR_CODE;
  8. barCodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
  9. barCodeWriter.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
  10. barCodeWriter.Options.Height = height;
  11. barCodeWriter.Options.Width = width;
  12. barCodeWriter.Options.Margin = 0;
  13. BitMatrix bm = barCodeWriter.Encode(strMessage);
  14. result = barCodeWriter.Write(bm);
  15. }
  16. catch (Exception ex)
  17. {
  18. //异常输出
  19. }
  20. return result;
  21. }

2.生成中间带特定图标的二维码

  1. public static Bitmap GeneratorQrImage(string contents, Image middleImg, int width = 300, int height = 300)
  2. {
  3. try
  4. {
  5. //构造二维码写码器
  6. MultiFormatWriter mutiWriter = new MultiFormatWriter();
  7. Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
  8. hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
  9. hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
  10. //生成二维码
  11. BitMatrix bm = mutiWriter.encode(contents, BarcodeFormat.QR_CODE, width, height, hint);
  12. BarcodeWriter barcodeWriter = new BarcodeWriter();
  13. Bitmap bitmap = barcodeWriter.Write(bm);
  14. //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸)
  15. int[] rectangle = bm.getEnclosingRectangle();
  16. //计算插入图片的大小和位置
  17. int middleImgW = Math.Min((int)(rectangle[2] / 3.5), middleImg.Width);
  18. int middleImgH = Math.Min((int)(rectangle[3] / 3.5), middleImg.Height);
  19. int middleImgL = (bitmap.Width - middleImgW) / 2;
  20. int middleImgT = (bitmap.Height - middleImgH) / 2;
  21. //将img转换成bmp格式,否则后面无法创建 Graphics对象
  22. Bitmap bmpimg = new Bitmap(bitmap.Width, bitmap.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  23. using (Graphics g = Graphics.FromImage(bmpimg))
  24. {
  25. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  26. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  27. g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
  28. g.DrawImage(bitmap, 0, 0);
  29. }
  30. //在二维码中插入图片
  31. Graphics myGraphic = Graphics.FromImage(bmpimg);
  32. //白底
  33. myGraphic.FillRectangle(Brushes.White, middleImgL, middleImgT, middleImgW, middleImgH);
  34. myGraphic.DrawImage(middleImg, middleImgL, middleImgT, middleImgW, middleImgH);
  35. return bmpimg;
  36. }
  37. catch (Exception ex)
  38. {
  39. //异常输出
  40. }
  41. return null;
  42. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/715796
推荐阅读
相关标签
  

闽ICP备14008679号