二维码对齐模式的位置计算

2024-05-20 03:49:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要知道如何计算the table of ISO/IEC 18004:2000 Annex E中定义的二维码对齐模式的位置。在

我不明白是怎么计算出来的。以版本16为例,使用{6,26,50,74}计算位置,点之间的距离为{20,24,24}。为什么{6,28,52,74},如果点之间的距离{22,24,22}分布得更均匀,那为什么{6,28,52,74}?在

我想知道这是如何在程序上产生的。在


Tags: ofthe程序版本距离定义table模式
3条回答

我不知道问这个问题是否有用。它就是这样的,如果它是{22,24,22},其实并不重要。你为什么要问? 我想间距应该是4个模块的倍数。在

虽然规范确实提供了一个对齐表,但这是一个合理的问题(我发现我自己也有一个问题:-)-以程序方式生成位置的可能性有它的优点(不易出错的代码,更小的代码占用空间,知道位置的模式/属性)。在

我很高兴地报告,是的,有一个程序存在(它甚至相当简单)。 规范本身说明了其中的大部分内容:

[The alignment patterns] are spaced as evenly as possible between the Timing Pattern and the opposite side of the symbol, any uneven spacing being accommodated between the timing pattern and the first alignment pattern in the symbol interior.

也就是说,只有第一个和第二个坐标之间的间隔可能与其余的间隔不同。其余的必须相等。 当然,另一个重要的方面是,为了使ap与定时模式一致,间隔必须是均匀的。 剩下的棘手的一点就是把四舍五入正确。在

总之,这里是打印校准位置表的代码:

def size_for_version(version):
    return 17 + 4 * version

def alignment_coord_list(version):
    if version == 1:
        return []
    divs = 2 + version // 7
    size = size_for_version(version)
    total_dist = size - 7 - 6
    divisor = 2 * (divs - 1)
    # Step must be even, for alignment patterns to agree with timing patterns
    step = (total_dist + divisor // 2 + 1) // divisor * 2 # Get the rounding right
    coords = [6]
    for i in range(divs - 2, -1, -1): # divs-2 down to 0, inclusive
        coords.append(size - 7 - i * step)
    return coords

for version in range(1, 40 + 1): # 1 to 40 inclusive
    print("V%d: %s" % (version, alignment_coord_list(version)))

有人对排名靠前的答案有一些评论,认为它不是100%准确,所以我也在贡献我的解决方案。在

我的解决方案是用C写的。翻译成你选择的语言应该很容易。在

private static int[] getAlignmentCoords(int version)
    {
        if (version <= 1)
        {
            return new int[0];
        }

        int num = (version / 7) + 2;//number of coordinates to return
        int[] result = new int[num];

        result[0] = 6;

        if (num == 1)
        {
            return result;
        }

        result[num - 1] = 4 * version + 10;

        if (num == 2)
        {
            return result;
        }

        result[num - 2] = 2 * ((result[0] + result[num - 1] * (num - 2)) / ((num - 1) * 2)); //leave these brackets alone, because of integer division they ensure you get a number that's divisible by 2

        if (num == 3)
        {
            return result;
        }

        int step = result[num - 1] - result[num - 2];

        for (int i = num - 3; i > 0; i--)
        {
            result[i] = result[i + 1] - step;
        }

        return result;
    }

我得到的值与这里显示的相同:http://www.thonky.com/qr-code-tutorial/alignment-pattern-locations/

总而言之,第一个坐标总是6。在

最后一个坐标总是比图像大小小7。图像大小计算为4*version+17,因此最后一个坐标是4*version+10。在

如果坐标精确地均匀分布,则最后一个坐标之前的一个坐标的位置将是(第一个坐标+(num-2)*最后一个坐标)/(num-1),其中num是所有坐标的数目。 但是坐标不是均匀分布的,所以这个位置必须减少到一个偶数。在

其余每个坐标与下一个坐标的间距与最后两个坐标之间的距离相同。在

免责声明:我没有阅读任何文档,我只是编写了一些代码,生成的数字序列与我链接的表中的数字序列相同。在

相关问题 更多 >