项目作者: TyKonKet

项目描述 :
Barcode generator on .NET Standard 1.3
高级语言: C#
项目地址: git://github.com/TyKonKet/BarcodeGenerator.git
创建时间: 2018-03-18T23:43:22Z
项目社区:https://github.com/TyKonKet/BarcodeGenerator

开源协议:MIT License

下载


Barcode Generator

NuGet
NuGet
.NET Test Runner
.NET Benchmark Runner

Barcode generator based on SkiaSharp library, supports the following encoders:

  • EAN-13
  • UPC-A
  • ISBN
  • EAN-8
  • CODE93

Table of Contents

Installation

BarcodeGenerator is available as a convenient NuGet package.
To install the package, use one of the following commands:

  1. PM> Install-Package TyKonKet.BarcodeGenerator
  1. nuget install TyKonKet.BarcodeGenerator

Usage

You can generate all the barcodes using the Barcode class. The Barcode class has a constructor that accepts an Action<BarcodeOptions> delegate to set the barcode options. The BarcodeOptions class has the following properties:

  • Type: Specifies the encoding type (e.g., BarcodeTypes.Ean8).
  • Height: Sets the height of the barcode bars.
  • Scaling: Sets the scale of the barcode image.
  • Margins: Sets the margins around the barcode.
  • BackgroundColor: Sets the background color of the barcode.
  • ForegroundColor: Sets the color of the barcode.
  • RenderText: Specifies whether to draw text below the barcode.

The BarcodeOptions class also provides several methods to configure the font used for the barcode text:

  • UseTypeface(SKTypeface typeface): Sets the SKTypeface for the barcode text.
  • UseTypeface(FontFamily fontFamily): Sets the font family for the barcode text, this can be either a string representing the font family name or a FontFamilies enum value.
  • UseTypeface(FontFamily fontFamily, SKFontStyle fontStyle): Sets the font family and style for the barcode text.
  • UseTypeface(FontFamily fontFamily, int weight, int width, SKFontStyleSlant fontStyleSlant): Sets the font family, weight, width, and style slant for the barcode text.
  • UseTypeface(FontFamily fontFamily, SKFontStyleWeight weight, SKFontStyleWidth width, SKFontStyleSlant fontStyleSlant): Sets the font family, weight, width, and style slant for the barcode text using SKFontStyleWeight and SKFontStyleWidth.
  • UseTypefaceFromFile(string path, int index = 0): Sets the typeface from a font file.
  • UseTypefaceFromData(SKData data, int index = 0): Sets the typeface from font data.
  • UseTypefaceFromStream(SKStreamAsset stream, int index = 0): Sets the typeface from a font stream.
  • UseTypefaceFromStream(Stream stream, int index = 0): Sets the typeface from a font stream.

These methods allow you to customize the font used for rendering text in the barcode, providing flexibility in font selection and styling.

Getting Started

Here is an example of how to generate a barcode using the Barcode class:

  1. using SkiaSharp;
  2. using TyKonKet.BarcodeGenerator;
  3. internal static class Example
  4. {
  5. private static void CreateBarcodeImage()
  6. {
  7. // Set up the barcode generator with specific options
  8. using var barcodeGenerator = new Barcode(options =>
  9. {
  10. // Set the type of barcode to Code93
  11. options.Type = BarcodeTypes.Code93;
  12. // Set the height of the barcode
  13. options.Height = 30;
  14. // Set the scaling factor for the barcode
  15. options.Scaling = 10;
  16. // Set the margins around the barcode
  17. options.Margins = 2;
  18. // Set the background color of the barcode to transparent
  19. options.BackgroundColor = SKColors.Transparent;
  20. // Set the foreground color of the barcode to black
  21. options.ForegroundColor = SKColors.Black;
  22. // Enable rendering of text below the barcode
  23. options.RenderText = true;
  24. // Use the specified font family and style for the text
  25. options.UseTypeface("Arial", SKFontStyle.Normal);
  26. });
  27. // Encode the barcode with the specified string
  28. barcodeGenerator.Encode("THE-BARCODE");
  29. // Export the barcode image to a file
  30. // The file path can include the following keywords:
  31. // - {barcode}: The barcode string.
  32. // - {format}: The image format (e.g., png, jpg).
  33. // - {quality}: The image quality (1-100).
  34. barcodeGenerator.Export("path/{barcode}_{quality}.{format}", SKEncodedImageFormat.Png, 100);
  35. }
  36. }

Breaking Changes

  • Starting with version 2.0.0, the library is no longer compatible with .NET Standard 1.3 since it uses SkiaSharp 3.116.1, which requires .NET Standard 2.0.
  • Starting with version 2.0.0, the API has been changed to be more user-friendly and to allow more customization. Follow the examples in the README.md file to see how to use the new API.

Roadmap

  • Update the README.md documentation
  • Add support for CODE39 and CODE128 encoders
  • Add support for more barcode types
  • Add an API to validate the barcode