Skip to content

Commit

Permalink
Support for top bar rendering.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjamro committed Mar 18, 2024
1 parent 244b421 commit 466f9cd
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 11 deletions.
38 changes: 36 additions & 2 deletions BarcodeStandard/BarcodeLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public Barcode(string data, Type iType)

/// <summary>
/// Mode when rendering guard bars for barcodes that support them (EAN-8/EAN-13/UPC-A/UPC-E)
///
///
/// Functionality not implemented for codes other than EAN-13
/// </summary>
public GuardBarsMode GuardBarsMode { get; set; }
Expand Down Expand Up @@ -228,6 +228,15 @@ public byte[] EncodedImageBytes
/// </summary>
public bool DisableEan13CountryException { get; set; } = false;

/// <summary>
/// Enable rendering of top bar when rendering code.
///
/// Top bar is typically used when printing codes on thermal printers
/// to detect damaged print head dots. Damage dots would create missing bars that
/// could cause the barcode to be invalid/unreadable.
/// </summary>
public bool IncludeTopBar { get; set; }

#endregion Properties

#region General Encode
Expand Down Expand Up @@ -563,7 +572,9 @@ private SKBitmap Generate_Image()
}//using

if (IncludeLabel)
{
Labels.Label_ITF14(this, bitmap);
}

break;
}//case
Expand All @@ -590,6 +601,7 @@ private SKBitmap Generate_Image()
//draw image
var pos = 0;
var halfBarWidth = (int)(iBarWidth * 0.5);
var totalBarcodeWidth = EncodedValue.Length * iBarWidth;

using (var canvas = new SKCanvas(bitmap))
{
Expand Down Expand Up @@ -617,7 +629,19 @@ private SKBitmap Generate_Image()

if (IncludeLabel)
{
Labels.Label_UPCA(this, bitmap);
if (GuardBarsMode != GuardBarsMode.Disabled)
{
Labels.Label_UPCA(this, bitmap);
}
else
{
Labels.Label_Generic(this, bitmap, totalBarcodeWidth);
}
}

if (IncludeTopBar)
{
Labels.TopBar(this, bitmap, shiftAdjustment, totalBarcodeWidth, iBarWidth);
}

break;
Expand Down Expand Up @@ -696,6 +720,11 @@ private SKBitmap Generate_Image()
}
}

if (IncludeTopBar)
{
Labels.TopBar(this, bitmap, shiftAdjustment, totalBarcodeWidth, iBarWidth);
}

break;
}//case
default:
Expand Down Expand Up @@ -768,6 +797,11 @@ private SKBitmap Generate_Image()
Labels.Label_Generic(this, bitmap, totalBarcodeWidth);
}//if

if (IncludeTopBar)
{
Labels.TopBar(this, bitmap, shiftAdjustment, totalBarcodeWidth, iBarWidth);
}

break;
}//switch
}//switch
Expand Down
20 changes: 20 additions & 0 deletions BarcodeStandard/Labels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,5 +346,25 @@ private static int GetFontSize(Barcode barcode, int wid, int hgt, string lbl)

return fontSize;
}

internal static void TopBar(Barcode barcode, SKBitmap img, int shiftAdjustment, int barcodeWidth, int iBarWidth)
{
using (var g = new SKCanvas(img))
using (var foreBrush = new SKPaint())
using (var backBrush = new SKPaint())
{
foreBrush.ColorF = barcode.ForeColor;
foreBrush.IsAntialias = true;
foreBrush.StrokeWidth = iBarWidth;
backBrush.ColorF = barcode.BackColor;
backBrush.IsAntialias = true;

// Clear area under top bar
g.DrawRect(new SKRect(0, 0, img.Width, iBarWidth * 3), backBrush);

// Draw the top bar
g.DrawLine(new SKPoint(shiftAdjustment, iBarWidth * 1.5f), new SKPoint(shiftAdjustment + barcodeWidth, iBarWidth * 1.5f), foreBrush);
}
}
}
}
31 changes: 22 additions & 9 deletions BarcodeStandardExample/TestApp.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions BarcodeStandardExample/TestApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private void btnEncode_Click(object sender, EventArgs e)
var w = Convert.ToInt32(txtWidth.Text.Trim());
var h = Convert.ToInt32(txtHeight.Text.Trim());
_b.Alignment = AlignmentPositions.Center;
_b.IncludeTopBar = chkTopBar.Checked;

//barcode alignment
switch (cbBarcodeAlign.SelectedItem.ToString().Trim().ToLower())
Expand Down

0 comments on commit 466f9cd

Please sign in to comment.