ScannerBase 类提供两种方法:DecodeDataLabel 和 DecodeScanDataType,用于解码传入日期。 当分别访问属性 ScanDataLabel 和 ScanDataType 时,将调用这些方法。 ScannerBase 类会延迟数据解码,直到应用程序访问数据属性,解码的数据将被缓存以供将来读取。
ScannerBase 类实现统一服务点 (UnifiedPOS) 规范要求的 ScannerBase.DecodeData 属性。 如果在应用程序读取 ScanDataLabel 属性时 DecodeData 未设置为 true,则将返回空字节数组。 同样,ScanDataType 返回 BarCodeSymbology.Unknown。 此功能在 ScannerBase 类中实现,对应用程序和服务对象均透明。
实现 DecodeScanDataLabel
重写受保护的虚拟 ScannerBasic 成员 DecodeScanDataLabel。
DecodeScanData 采用参数 scanData,其中包含完整的数据缓冲区。 无需在服务对象代码中缓存任何其他数据。
DecodeScanData 应处理扫描的数据,以删除数据缓冲区开头和末尾的标头和类型信息。 修改后的缓冲区将在字节数组中返回。
实现 DecodeScanDataType
重写受保护的虚拟 ScannerBasic 成员 DecodeScanDataType。
与 DecodeScanDataLabel 一样,DecodeScanDataType 接收包含完整扫描缓冲区的参数。
DecodeScanDataType 检查缓冲区以查找扫描的数据的数据类型,并返回相应的 BarCodeSymbology 值。
示例
以下代码演示了一种服务对象开发人员可以实现的典型方法,以便从扫描的缓冲区中提取标签和数据值。 请注意,此代码演示了特定设备。 不同的服务对象需要特定于设备的解码。
// Decode the incoming scanner data, removing header and
// type information.
override protected byte[] DecodeScanDataLabel(
byte[] scanData)
{
int i;
int len = 0;
// Get length of label data.
for (i = 5; i < (int)scanData[1]
&& (int)scanData[i] > 31; i++)
{
len++;
}
// Copy label data into buffer.
byte[] label = new byte[len];
len = 0;
for (i = 5; i < (int)scanData[1]
&& (int)scanData[i] > 31; i++)
{
label[len++] = scanData[i];
}
return label;
}
// Process the incoming scanner data to find the data type.
override protected BarCodeSymbology DecodeScanDataType(
byte[] scanData)
{
int i;
for (i = 5; i < (int)scanData[1]
&& (int)scanData[i] > 31; i++)
{
}
// last 3 (or 1) bytes indicate symbology.
if (i + 2 <= (int)ScanData[1])
{
return GetSymbology(
ScanData[i],
ScanData[i + 1],
ScanData[i + 2]);
}
else
{
return GetSymbology(ScanData[i], 0, 0);
}
}
// This method determines the data type by examining
// the end of the scanned data buffer. Either 1 byte
// or 3 byte is used to determine the type, depending on
// the incoming buffer.
static private BarCodeSymbology GetSymbology(
byte b1,
byte b2,
byte b3)
{
if (b1 == 0 && b3 == 11)
{
// Use all 3 bytes to determine the date type.
switch (b2)
{
case 10:
return BarCodeSymbology.Code39;
case 13:
return BarCodeSymbology.Itf;
case 14:
return BarCodeSymbology.Codabar;
case 24:
return BarCodeSymbology.Code128;
case 25:
return BarCodeSymbology.Code93;
case 37:
return BarCodeSymbology.Ean128;
case 255:
return BarCodeSymbology.Rss14;
default:
break;
}
}
else if (b2 == 0 && b3 == 0)
{
// Only use the first byte to determine the data type.
switch (b1)
{
case 13:
return BarCodeSymbology.Upca;
case 22:
return BarCodeSymbology.EanJan13;
case 12:
return BarCodeSymbology.EanJan8;
default:
break;
}
}
return BarCodeSymbology.Other;
}
有关如何从扫描的数据缓冲区中提取标签和类型数据的其他详细信息,请参阅 UPOS 规范。
编译代码
- 有关创建和编译服务对象项目的其他信息,请参阅服务对象示例:入门。