是否可以使用ZXing.net创建QR码,而无需安静区域?

发布于 2025-01-24 21:52:48 字数 542 浏览 3 评论 0原文

(如何)可以使用ZXing.net创建QR码而没有安静区域?

这是我当前的代码:

BarcodeWriter barcodeWriter = new BarcodeWriter();

barcodeWriter.Format = BarcodeFormat.QR_CODE;
barcodeWriter.Renderer = new BitmapRenderer();

EncodingOptions encodingOptions = new EncodingOptions();
encodingOptions.Width = 500;
encodingOptions.Height = 500;
encodingOptions.Margin = 0;
encodingOptions.Hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);

barcodeWriter.Options = encodingOptions;

bitmap = barcodeWriter.Write(compressedText);

(How) is it possible to create a qr code using ZXing.Net without a quiet zone?

This is my current code:

BarcodeWriter barcodeWriter = new BarcodeWriter();

barcodeWriter.Format = BarcodeFormat.QR_CODE;
barcodeWriter.Renderer = new BitmapRenderer();

EncodingOptions encodingOptions = new EncodingOptions();
encodingOptions.Width = 500;
encodingOptions.Height = 500;
encodingOptions.Margin = 0;
encodingOptions.Hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);

barcodeWriter.Options = encodingOptions;

bitmap = barcodeWriter.Write(compressedText);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心的憧憬 2025-01-31 21:52:48

Zxing.net不支持反叠液缩放图像缩放。这意味着它只能通过整数值调整大小。在您的情况下,您应该创建最小的图像,并使用图像操纵库或框架中的位图和图形类调整所得图像。

var barcodeWriter = new BarcodeWriter
{
   Format = BarcodeFormat.QR_CODE
};
// set width and height to 1 to get the smallest possible representation without a quiet zone around the qr code
var encodingOptions = new EncodingOptions
{
   Width = 1,
   Height = 1,
   Margin = 0
};
encodingOptions.Hints.Add(EncodeHintType.ERROR_CORRECTION,    ErrorCorrectionLevel.L);

barcodeWriter.Options = encodingOptions;

var bitmap = barcodeWriter.Write(compressedText);
// scale the image to the desired size
var scaledBitmap = ScaleImage(bitmap, 500, 500);

private static Bitmap ScaleImage(Bitmap bmp, int maxWidth, int maxHeight)
{
  var ratioX = (double)maxWidth / bmp.Width;
  var ratioY = (double)maxHeight / bmp.Height;
  var ratio = Math.Min(ratioX, ratioY);
  var newWidth = (int)(bmp.Width * ratio);
  var newHeight = (int)(bmp.Height * ratio);
  var newImage = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb);
  using (var graphics = Graphics.FromImage(newImage))
  {
      graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
      graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
      graphics.DrawImage(bmp, 0, 0, newWidth, newHeight);
  }
  return newImage;
}

ZXing.Net doesn't support image scaling with anti-aliasing. That means it can only resize by integer values. In your case you should create the smallest possible image and resize the resulting image with an image manipulation library or the Bitmap and Graphics classes from the framework.

var barcodeWriter = new BarcodeWriter
{
   Format = BarcodeFormat.QR_CODE
};
// set width and height to 1 to get the smallest possible representation without a quiet zone around the qr code
var encodingOptions = new EncodingOptions
{
   Width = 1,
   Height = 1,
   Margin = 0
};
encodingOptions.Hints.Add(EncodeHintType.ERROR_CORRECTION,    ErrorCorrectionLevel.L);

barcodeWriter.Options = encodingOptions;

var bitmap = barcodeWriter.Write(compressedText);
// scale the image to the desired size
var scaledBitmap = ScaleImage(bitmap, 500, 500);

private static Bitmap ScaleImage(Bitmap bmp, int maxWidth, int maxHeight)
{
  var ratioX = (double)maxWidth / bmp.Width;
  var ratioY = (double)maxHeight / bmp.Height;
  var ratio = Math.Min(ratioX, ratioY);
  var newWidth = (int)(bmp.Width * ratio);
  var newHeight = (int)(bmp.Height * ratio);
  var newImage = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb);
  using (var graphics = Graphics.FromImage(newImage))
  {
      graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
      graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
      graphics.DrawImage(bmp, 0, 0, newWidth, newHeight);
  }
  return newImage;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文