micjahn/ZXing.Net

Unable to parse this QR code, I don't know why

Opened this issue · 1 comments

下载

I'm running the latest version 0.16.9 from NuGet.

  BarcodeReader reader = new BarcodeReader();
  reader.AutoRotate = true;
  reader.Options.TryInverted = true;
  reader.Options.TryHarder = true;
  reader.Options.PossibleFormats = new List<ZXing.BarcodeFormat>();
  reader.Options.PossibleFormats.Add(ZXing.BarcodeFormat.QR_CODE);
  var result = reader.Decode(bitmap);

After compressing the image, it can be parsed. Is the image quality too high to be parsed?

BarcodeReader reader = new BarcodeReader();
reader.AutoRotate = true;
reader.Options.TryInverted = true;
reader.Options.TryHarder = true;
reader.Options.PossibleFormats = new List<ZXing.BarcodeFormat>();
reader.Options.PossibleFormats.Add(ZXing.BarcodeFormat.QR_CODE);
//compress pictures
bitmap = CompressImage(bitmap);
var result = reader.Decode(bitmap);

/// <summary>
/// compress pictures
/// </summary>
/// <param name="image"></param>
/// <param name="width">Target width</param>
/// <returns></returns>
public static Bitmap CompressImage(Bitmap srcBmp, int width)
{
    int srcWidth = srcBmp.Width;
    //if (srcWidth <= width)
    //{
    //    return srcBmp;
    //}
    int height = (int)(srcBmp.Height * (width / (double)srcWidth));
    Bitmap bmp = new Bitmap(width, height);
    using (Graphics gr = Graphics.FromImage(bmp))
    {
        gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
        gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
        //Setting to HighQualityBicubic cannot be resolved
        gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
        System.Drawing.Rectangle rectDestination = new Rectangle(0, 0, width, height);
        gr.DrawImage(srcBmp, rectDestination, 0, 0, srcBmp.Width, srcBmp.Height, GraphicsUnit.Pixel);
    }
    return bmp;
}