Class QrSegment
- Namespace
- Net.Codecrete.QrCodeGenerator
- Assembly
- QrCodeGenerator.dll
Represents a segment of character/binary/control data in a QR code symbol.
public class QrSegment
- Inheritance
-
QrSegment
- Inherited Members
Remarks
The easiest way to deal with QR code segments is to call EncodeText(string, Ecc) or EncodeBinary(byte[], 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(Mode, int, 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
QrSegment(Mode, int, 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.
public QrSegment(QrSegment.Mode mode, int numChars, BitArray data)
Parameters
modeQrSegment.ModeThe segment mode used to encode this segment.
numCharsintThe data length in characters or bytes (depending on the segment mode).
dataBitArrayThe data bits.
Exceptions
- ArgumentNullException
modeordataisnull.- ArgumentOutOfRangeException
numCharsis negative.
Properties
EncodingMode
The encoding mode of this segment.
public QrSegment.Mode EncodingMode { get; }
Property Value
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.
public int NumChars { get; }
Property Value
- int
Length of the segment's unencoded data.
Methods
GetData()
Returns a copy of this segment's data bits.
public BitArray GetData()
Returns
- BitArray
A copy of the data bits.
GetJoinedText(List<QrSegment>)
Returns the joined text represented by the provided segments.
public static string GetJoinedText(List<QrSegment> segments)
Parameters
Returns
GetJoinedText(List<List<QrSegment>>)
Returns the joined text represented by the provided segments.
public static string GetJoinedText(List<List<QrSegment>> segments)
Parameters
Returns
GetText()
Returns the text represented by this segment.
This method will fail if the segment contains binary data that cannot be converted to text.
public string GetText()
Returns
- string
The text.
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.
public static bool IsAlphanumeric(string text)
Parameters
textstringthe string to test for encodability (not
null)
Returns
- bool
trueiff each character is in the alphanumeric mode character set.
Exceptions
- NullReferenceException
if the string is
null
- See Also
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".
public static bool IsNumeric(string text)
Parameters
textstringthe string to test for encodability (not
null)
Returns
- bool
trueiff each character is in the range "0" to "9".
Exceptions
- NullReferenceException
if the string is
null
- See Also
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.
public static QrSegment MakeAlphanumeric(string text)
Parameters
textstringThe text to encode, consisting of allowed characters only.
Returns
- QrSegment
The created segment containing the text.
Exceptions
- ArgumentNullException
textisnull.- ArgumentOutOfRangeException
textcontains non-encodable characters.
MakeBytes(ArraySegment<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.
public static QrSegment MakeBytes(ArraySegment<byte> data)
Parameters
dataArraySegment<byte>The binary data to encode.
Returns
- QrSegment
The created segment containing the specified data.
Exceptions
- ArgumentNullException
dataisnull.
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.
public static QrSegment MakeBytes(byte[] data)
Parameters
databyte[]The binary data to encode.
Returns
- QrSegment
The created segment containing the specified data.
Exceptions
- ArgumentNullException
dataisnull.
MakeEci(int)
Creates a segment representing an Extended Channel Interpretation (ECI) designator with the specified assignment value.
public static QrSegment MakeEci(int assignVal)
Parameters
assignValintThe ECI assignment number (see the AIM ECI specification).
Returns
- QrSegment
The created segment containing the data.
Exceptions
- ArgumentOutOfRangeException
assignValis outside the range [0, 106).
MakeNumeric(string)
Creates a segment representing the specified string of decimal digits. The segment is encoded in numeric mode.
public static QrSegment MakeNumeric(string digits)
Parameters
digitsstringThe text to encode, consisting of digits from 0 to 9 only.
Returns
- QrSegment
The created segment containing the text.
Exceptions
- ArgumentNullException
digitsisnull.- ArgumentOutOfRangeException
digitscontains non-digit characters
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 consist of multiple segments with various encoding modes in order to minimize the length of the bit stream.
public static List<QrSegment> MakeSegments(string text)
Parameters
textstringThe text to be encoded.
Returns
Remarks
The current implementation does not create multiple segments.
Exceptions
- ArgumentNullException
textisnull.