首页 > 解决方案 > pdf417 条码创建,Reed Solomon 纠错码字在 python 和 JAVA 之间的差异

问题描述

我使用 Python 库 pdf417gen 创建了一个 pdf417 条形码。

条形码是字符串“M1LONG”的图形表示。条码有两个数据列,Reed Solomon 纠错安全级别设置为“1”。这表明在输入八个数据码字的情况下,纠错码字的数量应该是四个。

Python 输出将 D07 到 D00 的数据代码字显示为{8, 389, 902, 11, 900, 344, 396, 900}. python 将 C03 到 C00 的纠错码字列为{718, 801, 313, 877}. 这是用于生成所有代码字的 Python:

from builtins import range

from .data import ERROR_CORRECTION_FACTORS

def compute_error_correction_code_words(data_words, level):
    assert 0 <= level <= 8

    # Correction factors for the given level
    factors = ERROR_CORRECTION_FACTORS[level]

    # Number of EC words
    count = 2 ** (level + 1)

    # Correction code words list, prepopulated with zeros
    ec_words = [0] * count

    # Do the math
    for data_word in data_words:
        temp = (data_word + ec_words[-1]) % 929

        for x in range(count - 1, -1, -1):
            word = ec_words[x - 1] if x > 0 else 0
            ec_words[x] = (word + 929 - (temp * factors[x]) % 929) % 929

    return [929 - x if x > 0 else x for x in reversed(ec_words)]

纠错码字是使用多项式、伽罗瓦域算术和模数 929 的补码生成的,这是 pdf417 系统可能的码字数。计算使用许多因素来简化过程。对于安全级别 1,建议的因子数为 4。因素是522,568,723,809

http://grandzebu.net/informatique/codbar/pdf417coef.txt

问题是这样的。我尝试使用从http://grandzebu.net/informatique/codbar-en/pdf417.htm获得的 JAVA 伪代码重新创建错误代码字

我编写了一个 JAVA 程序来尝试生成与上述 Python 软件相同的代码字,但它不会生成相同的错误代码字。

JAVA程序编译并运行,数学在我未经训练的眼中看起来不错,但产生的错误代码不一样。这是我的 JAVA,JAVA 变量被称为与 Python 相同,以便于比较两个程序。

import java.util.Arrays;

public class reedsolomon{

    public static void main (String[] args){

        int ec_words[] = new int[4];//correction codewords array
        int temp=0;//holding variable
        int count=4; //number of error correction codewords
        int data_words[] = {8,389,902,11,900,344,396,900};// eight data codewords array D7 to D0.
        int factors[]= {522,568,723,809}; //factors or coefficients array.
        for(int i=0; i<data_words.length-1; i++) { 
            temp=(data_words[i] + ec_words[count-1])%929;
            for(int x=count-1; x>-1; x--){
                if(x==0){
                    ec_words[x] = (929-(temp*factors[x])%929)%929; //negative values in the Galois Field
                    //GF(929) are equal to the complement of itself if
                    //ec_words[x] > -929
                }
                else{
                    ec_words[x]=(ec_words[x-1]+929-(temp*factors[x])%929) %929; //negative values in the Galois Field
                    //GF(929) are equal to the complement of the
                    //remainder (ec_words[x] /929) if ec_words[x] <= -929.
                }
            }
        }
        for(int j=0; j<count; j++){
            if(ec_words[j] != 0){
                ec_words[j]=929-ec_words[j]; 
            }
        }System.out.println("Error codewords are " + Arrays.toString(ec_words));
    }
}

我将不胜感激知道 JAVA 代码的问题是什么,它阻止它生成与库 pdf417gen 中包含的 python 程序相同的错误代码字。

标签: javabarcodeerror-correctionreed-solomonpdf417

解决方案


一些澄清(至少对于其他阅读此线程的人)。“因子”实际上是 GF(929) = 1 x 中生成多项式 g(x) = (x-3)(x-3^2)(x-3^3)(x-3^4) 的系数^4 + 809 x^3 + 723 x^2 + 568 x + 522。编码过程将数据视为多项式 m(x),将其乘以 x^4 为 4 个奇偶校验字节创建空间,然后除以 m (x)x^4 / g(x) 产生余数 r(x)。编码后的码字为 m(x)x^4 - r(x) = 8 x^11 + 389 x^10 + 902 x^9 + 11 x^8 + 900 x^7 + 344 x^6 + 396 x^5 + 900 x^4 + 718 x^3 + 801 x^2 + 313 x + 877 。

Wiki 文章还在其 BCH 视图示例中使用了 GF(929) 和相同的生成多项式:

https://en.wikipedia.org/wiki/Reed%E2%80%93Solomon_error_correction#Example_3

RS(12,8) GF(929) 的可能代码字数为 929^8(一个巨大的数字)。


推荐阅读