Show / Hide Table of Contents

Class QrSegment

Represents a segment of character/binary/control data in a QR code symbol.

Inheritance
System.Object
QrSegment
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: Net.Codecrete.QrCodeGenerator
Assembly: QrCodeGenerator.dll
Syntax
public class QrSegment
Remarks

The easiest way to deal with QR code segments is to call EncodeText(String, QrCode.Ecc) or EncodeBinary(Byte[], QrCode.Ecc), and not to use instances of this class directly. The mid-level way is to take the payload data and call a static factory function such as MakeNumeric(String). The low-level way is to custom-make the bit array and call the QrSegment(QrSegment.Mode, Int32, BitArray) constructor with appropriate values.

This segment class imposes no length restrictions, but QR codes have restrictions. Even in the most favorable conditions, a QR code can only hold 7089 characters of data. Any segment longer than this is meaningless for the purpose of generating QR codes.

This class can represent kanji mode segments, but provides no help in encoding them - see QrSegmentAdvanced for full kanji support.

Instances of this class are immutable.

Constructors

| Improve this Doc View Source

QrSegment(QrSegment.Mode, Int32, BitArray)

Initializes a QR code segment with the specified attributes and data.

The character count numChars must agree with the mode and the bit array length, but the constraint isn't checked. The specified bit array is cloned.

Declaration
public QrSegment(QrSegment.Mode mode, int numChars, BitArray data)
Parameters
Type Name Description
QrSegment.Mode mode

The segment mode used to encode this segment.

System.Int32 numChars

The data length in characters or bytes (depending on the segment mode).

System.Collections.BitArray data

The data bits.

Exceptions
Type Condition
System.ArgumentNullException

mode or data is null.

System.ArgumentOutOfRangeException

numChars is negative.

Properties

| Improve this Doc View Source

EncodingMode

The encoding mode of this segment.

Declaration
public QrSegment.Mode EncodingMode { get; }
Property Value
Type Description
QrSegment.Mode

Encoding mode.

| Improve this Doc View Source

NumChars

The length of this segment's unencoded data.

Measured in characters for numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode.

Different from the data's bit length.

Declaration
public int NumChars { get; }
Property Value
Type Description
System.Int32

Length of the segment's unencoded data.

Methods

| Improve this Doc View Source

GetData()

Returns a copy of this segment's data bits.

Declaration
public BitArray GetData()
Returns
Type Description
System.Collections.BitArray

A copy of the data bits.

| Improve this Doc View Source

IsAlphanumeric(String)

Tests whether the specified string can be encoded as a segment in alphanumeric mode.

A string is encodable iff each character is in the range "0" to "9", "A" to "Z" (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.

Declaration
public static bool IsAlphanumeric(string text)
Parameters
Type Name Description
System.String text

the string to test for encodability (not null)

Returns
Type Description
System.Boolean

true iff each character is in the alphanumeric mode character set.

Exceptions
Type Condition
System.NullReferenceException

if the string is null

See Also
MakeAlphanumeric(String)
| Improve this Doc View Source

IsNumeric(String)

Tests whether the specified string can be encoded as a segment in numeric mode.

A string is encodable iff each character is in the range "0" to "9".

Declaration
public static bool IsNumeric(string text)
Parameters
Type Name Description
System.String text

the string to test for encodability (not null)

Returns
Type Description
System.Boolean

true iff each character is in the range "0" to "9".

Exceptions
Type Condition
System.NullReferenceException

if the string is null

See Also
MakeNumeric(String)
| Improve this Doc View Source

MakeAlphanumeric(String)

Creates a segment representing the specified text string. The segment is encoded in alphanumeric mode.

Allowed characters are: 0 to 9, A to Z (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.

Declaration
public static QrSegment MakeAlphanumeric(string text)
Parameters
Type Name Description
System.String text

The text to encode, consisting of allowed characters only.

Returns
Type Description
QrSegment

The created segment containing the text.

Exceptions
Type Condition
System.ArgumentNullException

text is null.

System.ArgumentOutOfRangeException

text contains non-encodable characters.

| Improve this Doc View Source

MakeBytes(Byte[])

Creates a segment representing the specified binary data encoded in byte mode. All input byte arrays are acceptable.

Any text string can be converted to UTF-8 bytes (using Encoding.UTF8.GetBytes(str)) and encoded as a byte mode segment.

Declaration
public static QrSegment MakeBytes(byte[] data)
Parameters
Type Name Description
System.Byte[] data

The binary data to encode.

Returns
Type Description
QrSegment

The created segment containing the specified data.

Exceptions
Type Condition
System.ArgumentNullException

data is null.

| Improve this Doc View Source

MakeEci(Int32)

Creates a segment representing an Extended Channel Interpretation (ECI) designator with the specified assignment value.

Declaration
public static QrSegment MakeEci(int assignVal)
Parameters
Type Name Description
System.Int32 assignVal

The ECI assignment number (see the AIM ECI specification).

Returns
Type Description
QrSegment

The created segment containing the data.

Exceptions
Type Condition
System.ArgumentOutOfRangeException

assignValis outside the range [0, 106).

| Improve this Doc View Source

MakeNumeric(String)

Creates a segment representing the specified string of decimal digits. The segment is encoded in numeric mode.

Declaration
public static QrSegment MakeNumeric(string digits)
Parameters
Type Name Description
System.String digits

The text to encode, consisting of digits from 0 to 9 only.

Returns
Type Description
QrSegment

The created segment containing the text.

Exceptions
Type Condition
System.ArgumentNullException

digits is null.

System.ArgumentOutOfRangeException

digits contains non-digit characters

| Improve this Doc View Source

MakeSegments(String)

Creates a list of zero or more segments representing the specified text string.

The text may contain the full range of Unicode characters.

The result may multiple segments with various encoding modes in order to minimize the length of the bit stream.

Declaration
public static List<QrSegment> MakeSegments(string text)
Parameters
Type Name Description
System.String text

The text to be encoded.

Returns
Type Description
System.Collections.Generic.List<QrSegment>

The created mutable list of segments representing the specified text.

Remarks

The current implementation does not use multiple segments.

Exceptions
Type Condition
System.ArgumentNullException

text is null.

  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX