Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Label text fuzzy #183

Open
JuanSaavedra opened this issue Dec 8, 2023 · 1 comment
Open

Label text fuzzy #183

JuanSaavedra opened this issue Dec 8, 2023 · 1 comment

Comments

@JuanSaavedra
Copy link

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;
  }
@barnhill
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants