Grant M
Grant M

Reputation: 658

C# Zxing Encode 1d EAN8

I want to generate a 1D EAN8 barcode using c# Zxing. I have only been able to find code examples and documentation for generating 2D QR-code

      var writer = new BarcodeWriter
  {
     Format = BarcodeFormat.QR_CODE,
     Options = new QrCodeEncodingOptions
     {
        Height = height,
        Width = width
     }
  };
  return writer.Write(textForEncoding);

which I can run and works fine, but there is no "1DCodeEncodingOptions" or similarly named function. I tried

      var writer = new BarcodeWriter
  {
     Format = BarcodeFormat.EAN_8

  };
  return writer.Write("1234567");

but it throughs an index error.

edit: I have the syntax correct now but it is not producing a proper barcode because I do not know the size it expects, and there seems to be no default.

using ZXing; Using ZXing.OneD

  var writer = new BarcodeWriter
  {
     Format = BarcodeFormat.EAN_8,
        Options = new ZXing.Common.EncodingOptions
        {
            Height = 100,
            Width = 300
        }
  };
  return writer.Write("12345678");

Upvotes: 0

Views: 2850

Answers (1)

Mike Dimmick
Mike Dimmick

Reputation: 9802

12345678 is not a valid EAN8 barcode. The check digit for 1234567 is 0. See EAN 8 : How to calculate checksum digit? for how to calculate checksums.

ZXing won't stop you creating invalid barcodes (at least in the version 0.11 I'm using, although the current source on Codeplex looks like it does), but they won't scan. The scanner uses the checksum to ensure that it has read the data correctly.

If you intend to use an EAN8 in commerce, you will need to get a GTIN-8 from your national GS1 Member Organization. If you only intend to sell them in your store, you should use one of the restricted distribution prefixes.

If you don't need to put your products in the retail supply chain, I'd recommend a different barcode format.

Interleaved 2 of 5 (BarcodeFormat.ITF) is very compact but can only contain digits; it doesn't have any self-checking and there's a design flaw that allows the barcode to be misread from some angles. It's recommended that you put black bars (called Bearer Bars) across the top and bottom of the barcode. I can't see an option for this in ZXing.

The next most compact format is Code 128 (BarcodeFormat.CODE_128), using Code Set C. This encodes two digits in one module (one block of six bars and spaces). The format is self-checking (there is always a check character, which is stripped off by the scanner). Some scanners don't handle Code Set C properly. To force Code Set B, use Code128EncodingOptions in place of Common.EncodingOptions and set ForceCodesetB to true.

Upvotes: 1

Related Questions