# Blazor Barcode and QR Code Generator

URL: https://flexcoreui.com/demo/barcodes  
Section: Components  
Controls: BarcodeControl, QrCodeControl  
Keywords: barcode generator, QR code generator, barcode component, Code 128, EAN-13

Barcodes and QR codes for labels and tickets in Blazor: Code 128, Code 39, EAN, UPC, ITF and Codabar drawn as SVG, encoded with ZXing.Net.

## Documentation

### Barcode

| Name | Type | Default | Description |
|---|---|---|---|
| Value | string | "FLEX-12345" | Text to encode. Empty draws no code; the empty frame stays. |
| Type | BarcodeType | Code128 | Symbology; see the table below. |
| ShowText | bool | true | Draws the value under the bars. |
| ForegroundColor / BackgroundColor | string | #000000 / #ffffff | Bar and text color; background rectangle. Any CSS color. |
| BarHeight | int | 50 | Bar height in SVG units (pixels at default size); at least 1. |
| Width / Height | string? | null | CSS size of the SVG. Unset means the natural size: one unit per module plus a 20-unit quiet zone on each side (40 in total), and the bar height plus 27 (text) or 10. |
| AriaLabel | string? | null | Overrides the default "Type barcode: value". |
| CssClass / Style (base) | string? | null | On the fx-barcode frame. Visible from the base is not applied. |
| Enabled / Id / Title / Tag / Data / Parent (base) |  | null | Accepted but have no effect. A written id binds to Id, so it does not reach the frame. |
| (unmatched attributes) | Dictionary<string,object> |  | Written on the frame <div>. |

### Types

| Name | Description |
|---|---|
| Code128 | Any ASCII text; compact for digits and mixed-case text. |
| Code39 | Upper-case letters, digits and - . $ / + % and space, one symbol each. Any other ASCII character is accepted and written in Full ASCII, two symbols each, so the code is wider and the scanner must read Full ASCII. Non-ASCII is rejected. |
| Code93 | Same characters as Code 39, more compact. Other ASCII characters are accepted and take two symbols each. |
| Ean8 | 7 or 8 digits. The check digit is computed when omitted and verified when given. |
| Ean13 | 12 or 13 digits. The check digit is computed when omitted and verified when given. |
| UpcA | 11 or 12 digits. |
| UpcE | 7 or 8 digits. |
| Itf | Digits, even count (Interleaved 2 of 5). |
| Codabar | Digits and - $ : / . +; A–D start and stop characters are optional and added when missing. |

### Qr

| Name | Type | Default | Description |
|---|---|---|---|
| Value | string | "FlexCore" | Text to encode, UTF-8: a link, mailto:, tel:, geo: or plain text. Empty draws no code; the empty frame stays. |
| Size | int | 150 | Rendered width and height in pixels; the module count comes from the text length and level. |
| ForegroundColor / BackgroundColor | string | #000000 / #ffffff | Module and background color. |
| ErrorCorrection | QrErrorCorrectionLevel | Medium | Redundancy level; see below. |
| AriaLabel | string? | null | Overrides the default "QR code: value". |
| CssClass / Style (base) | string? | null | On the fx-qrcode frame. Visible from the base is not applied. |
| Enabled / Id / Title / Tag / Data / Parent (base) |  | null | Accepted but have no effect. A written id binds to Id, so it does not reach the frame. |
| (unmatched attributes) | Dictionary<string,object> |  | Written on the frame <div>. |

### Ecc

| Name | Description |
|---|---|
| Low | About 7% of the symbol can be unreadable. |
| Medium | About 15%. |
| Quartile | About 25%. |
| High | About 30%; the largest symbol for a given text. |

### Props

| Name | Type | Description |
|---|---|---|
| BarcodeControl.ErrorMessage | string? | "Cannot encode Type barcode: …" from the encoder, or null. |
| QrCodeControl.ErrorMessage | string? | "Cannot encode QR code: …", or null. |

### Css

| Name | Description |
|---|---|
| fx-barcode · fx-barcode-error | White rounded frame with a hairline border and light shadow; the error text inside it. |
| fx-qrcode · fx-qrcode-error | Same frame with an 8px radius; the error text inside it. |

## Examples

### Barcode

```razor
@using Fx.ControlKit

<div class="controls">
    <ButtonGroupControl Items="_productItems" Values="@(new[] { _product })" ValuesChanged="PickProduct" AriaLabel="Product" />
    <ButtonGroupControl Items="_typeItems" Values="@(new[] { _type.ToString() })" ValuesChanged="PickType" AriaLabel="Symbology" />
    <TextBoxControl @bind-Value="_barcodeValue" UpdateOnInput="true" Width="170px" Placeholder="Value to encode" aria-label="Value to encode" />
    <ToggleButtonControl @bind-Selected="_showText">Show text</ToggleButtonControl>
</div>
<div class="tag">
    <span class="tag-hole"></span>
    <div class="tag-title">@Tag.Bin</div>
    <div class="tag-name">@_product · @Tag.Zone zone</div>
    <BarcodeControl Value="@(_barcodeValue ?? "")" Type="_type" ShowText="_showText" BarHeight="56" ForegroundColor="#1e293b"
                    AriaLabel="@($"Bin tag barcode for {_product}")" />
</div>
```

```csharp
// Barcode
private sealed record BinTag(string Bin, string Zone, string Sku, string Digits);
private static readonly Dictionary<string, BinTag> Tags = new()
{
    ["Camping stove"] = new("C-02-01", "Bulk", "SKU-48213", "200000048213"),
    ["Headlamp"] = new("A-12-03", "Ambient", "SKU-51907", "200000051907"),
    ["Trail mix"] = new("B-07-02", "Ambient", "SKU-30466", "200000030466"),
};
private readonly SelectionItem[] _productItems = Tags.Keys.Select(n => new SelectionItem(n, n)).ToArray();
private readonly SelectionItem[] _typeItems =
{
    new("Code128", "Code 128"), new("Code39", "Code 39"), new("Ean13", "EAN-13"), new("Itf", "ITF"),
};
private string _product = "Camping stove";
private BarcodeType _type = BarcodeType.Code128;
private string? _barcodeValue = "SKU-48213";
private bool _showText = true;
private BinTag Tag => Tags[_product];
// Code 128 and Code 39 take the lettered SKU; EAN-13 and ITF take digits only.
private string Sample => _type is BarcodeType.Ean13 or BarcodeType.Itf ? Tag.Digits : Tag.Sku;
private void PickProduct(IReadOnlyList<string> values)
{
    if (values.Count == 0) return;
    _product = values[0];
    _barcodeValue = Sample;
}
private void PickType(IReadOnlyList<string> values)
{
    if (values.Count == 0) return;
    _type = Enum.Parse<BarcodeType>(values[0]);
    _barcodeValue = Sample;
}
```

### Qr

```razor
@using Fx.ControlKit

<div class="controls">
    <ButtonGroupControl Items="_scanItems" Values="@(new[] { _scan })" ValuesChanged="PickScan" AriaLabel="Scan to" />
    <ButtonGroupControl Items="_eccItems" Values="@(new[] { _ecc.ToString() })" ValuesChanged="PickEcc" AriaLabel="Error correction" />
</div>
<div class="qr-row">
    <div class="slip">
        <div>
            <div class="tag-title">Shipment SHP-0456</div>
            <div class="tag-name">Road freight · 12 cartons · Dock 4</div>
            <div class="slip-scan">Scan to @_scanItems.First(i => i.Value == _scan).Text.ToLowerInvariant()</div>
        </div>
        <QrCodeControl Value="@Payload" Size="@Size" ErrorCorrection="_ecc" ForegroundColor="#1e293b"
                       AriaLabel="@($"QR code for shipment SHP-0456: {_scan}")" />
    </div>
    <div class="field">
        <span class="field-label">Size @Size px</span>
        <SliderControl @bind-Value="_size" Min="80" Max="240" Step="20" ShowValue="false" aria-label="QR code size" />
        <span class="field-label">Encodes</span>
        <code class="payload">@Payload</code>
    </div>
</div>
```

```csharp
// QR
private readonly SelectionItem[] _scanItems =
{
    new("track", "Track the shipment"), new("email", "Email us"), new("call", "Call us"), new("map", "Find us"),
};
private string _scan = "track";
private string Payload => _scan switch
{
    "email" => "mailto:dispatch@forkliftandfound.example?subject=Shipment%20SHP-0456",
    "call" => "tel:+12065550142",
    "map" => "geo:47.25,-122.30",
    _ => "https://forkliftandfound.example/track/SHP-0456",
};
private QrErrorCorrectionLevel _ecc = QrErrorCorrectionLevel.Medium;
private double _size = 140;
private int Size => (int)_size;
private readonly SelectionItem[] _eccItems =
{
    new("Low", "L 7%"), new("Medium", "M 15%"), new("Quartile", "Q 25%"), new("High", "H 30%"),
};
private void PickScan(IReadOnlyList<string> values)
{
    if (values.Count > 0) _scan = values[0];
}
private void PickEcc(IReadOnlyList<string> values)
{
    if (values.Count > 0) _ecc = Enum.Parse<QrErrorCorrectionLevel>(values[0]);
}
```

