数据解码(POS for .NET v1.14 SDK 文档)

ScannerBase 类提供两种方法 :DecodeDataLabelDecodeScanDataType ,用于解码传入日期。 分别在访问 ScanDataLabelScanDataType 时调用这些方法。 ScannerBase 类将延迟数据解码,直到应用程序访问数据属性,并将缓存解码的数据以供将来读取。

ScannerBase 类根据统一服务点(UnifiedPOS)规范的要求实现ScannerBase.DecodeData该属性。 如果应用程序读取 ScanDataLabel 属性时未将 DecodeData 设置为 true,则返回空字节数组。 同样, ScanDataType 返回 BarCodeSymbology.Unknown。 此功能在 ScannerBase 类中实现,对应用程序和服务对象都是透明的。

实现 DecodeScanDataLabel

  1. 重写受保护的虚拟 ScannerBasic 成员 DecodeScanDataLabel

  2. DecodeScanData 采用参数 scanData,其中包含完整的数据缓冲区。 无需在服务对象代码中缓存任何其他数据。

  3. DecodeScanData 应处理扫描的数据,以在数据缓冲区的开头和结尾删除标头和类型信息。 修改后的缓冲区将在字节数组中返回。

实现 DecodeScanDataType

  1. 重写受保护的虚拟 ScannerBasic 成员 DecodeScanDataType

  2. DecodeScanDataLabel 一样, DecodeScanDataType 接收包含完整扫描缓冲区的参数。

  3. DecodeScanDataType 检查缓冲区以查找扫描数据的数据类型,并返回相应的 BarCodeSymbology 值。

Example

以下代码演示了 Service 对象开发人员可以实现的典型方法,以便从扫描的缓冲区中提取标签和数据值。 请注意,此代码演示了特定设备。 不同的服务对象将需要针对特定设备的解码。

// 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 规范中找到有关如何从扫描的数据缓冲区中提取标签和类型数据的其他详细信息。

编译代码

另请参阅

Reference

其他资源