是否可以使用 ZXing.Net 创建一个没有静区的二维码?

Is it possible to create a qr code using ZXing.Net without a quiet zone?

(如何)可以使用ZXing.Net创建一个没有静区的二维码?

这是我当前的代码:

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);

谢谢!

ZXing.Net 不支持 anti-aliasing 的图像缩放。这意味着它只能按整数值调整大小。在您的情况下,您应该创建尽可能小的图像并使用图像处理库或框架中的位图和图形 类 调整结果图像的大小。

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;
}