barcodelib icon indicating copy to clipboard operation
barcodelib copied to clipboard

Label text fuzzy

Open JuanSaavedra opened this issue 2 years ago • 1 comments

hi there,

First of all thank you very much for creating such a great library.

Our issue revolves around when printing the labels generated.

When generating a barcode, we need the label to show underneath. All working fine for UPCA etc.

Unfortunately when printing, the label text goes all fuzzy and not sharp enough.

I was wondering if there was anyway to remove the antialising of the font.

We tried setting SKFontEdging to SubpixelAntialises to a all the options.

We are just not sure what are our best options to maximise the sharpness of the font for the label below the barcodes.

We are generating the image and returning as Jpg file (code shown below)

Or is the issue just printer related?

Thank you!

   var font = new SKFont
    {
      Size = fontSize.Value,
      Edging = SKFontEdging.SubpixelAntialias,
      Subpixel = false,
      Hinting = SKFontHinting.Full,
    };

    var typeface = SKTypeface.FromFamilyName(fontName,
      SKFontStyleWeight.Bold,
      SKFontStyleWidth.UltraExpanded,
      SKFontStyleSlant.Upright);

image image

 public byte[] GenerateBarcode(
    string barcodeText, 
    int? width = 290, 
    int? height = 120,
    string barcodeTypeAsString = "Code128", int? fontSize = 20, 
    string fontName = "Verdana")
  {
    if (string.IsNullOrWhiteSpace(barcodeText))
    {
      throw new ApplicationException("Barcode text cannot be empty");
    }
    
    // convert the barcode type to an enum using Enum.TryParse
    if (!Enum.TryParse(barcodeTypeAsString, true, out BarcodeStandard.Type barcodeType))
    {
      throw new ApplicationException("Invalid barcode type specified: " + barcodeTypeAsString);
    }
    
    var barcode = new Barcode();
    
    var font = new SKFont
    {
      Size = fontSize.Value,
      Edging = SKFontEdging.SubpixelAntialias,
      Subpixel = false,
      Hinting = SKFontHinting.Full,
    };

    var typeface = SKTypeface.FromFamilyName(fontName,
      SKFontStyleWeight.Bold,
      SKFontStyleWidth.UltraExpanded,
      SKFontStyleSlant.Upright);
    
    font.Typeface = typeface;
    
    if(barcodeType==BarcodeStandard.Type.Interleaved2Of5Mod10)
    {
      var checkDigit = CalculateMod10CheckDigit(barcodeText);
      _logger.LogInformation($"Check digit: {checkDigit}");
      barcode.AlternateLabel = barcodeText+checkDigit;
    }
    
    barcode.ImageFormat = SKEncodedImageFormat.Jpeg;
    barcode.Alignment = AlignmentPositions.Center;
    barcode.LabelFont = font;
    barcode.IncludeLabel = true;
    
    barcode.Encode(barcodeType, barcodeText, SKColors.Black, SKColors.White, width.Value, height.Value);
    
    using var ms = new MemoryStream();
    barcode.SaveImage(ms, SaveTypes.Jpg);
    var barcodeBytes = ms.ToArray();
    return barcodeBytes;
  }

JuanSaavedra avatar Dec 08 '23 05:12 JuanSaavedra

There is a default font specified without subpixel anti aliasing ... maybe its the usage of this custom font? Also the default font used is Arial but Im not sure what it would be without specifying it in this new Font creation above.

barnhill avatar Apr 29 '24 00:04 barnhill

Thanks for your reply, apologies for the delay in my response. All good, the customer is happy with how it came out for now. Thanks again.

JuanSaavedra avatar Jun 07 '24 00:06 JuanSaavedra

Awesome 😎👍 I'll close this issue!

barnhill avatar Jun 09 '24 14:06 barnhill