简体   繁体   中英

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

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

Thanks!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM