用于 Code128 条形码字体的 Python Code128 编码器

2024-01-10

就像标题所说,我有一个 Code128 字体,我想用它来打印条形码。但是,该字符串需要以 Code128 进行编码才能使条形码字体正常工作。我的应用程序使用Python3语言。

网络上曾经有一个示例,介绍如何将字符串编码为 Code128 字体,但我找不到了。

I do NOT想要一个字符串到 .svg 转换器。我特别想将字符串转换为 Code128 编码的字符串。

任何参考文献、Python3 中的代码片段或文档将不胜感激。

编辑:我使用的字体here http://www.barcodelink.net/barcode-font.php.


这是一个被接受的答案,所以我将原始代码留在下面。但我更喜欢这种精致。

def list_join(seq):
    ''' Join a sequence of lists into a single list, much like str.join
        will join a sequence of strings into a single string.
    '''
    return [x for sub in seq for x in sub]

code128B_mapping = dict((chr(c), [98, c+64] if c < 32 else [c-32]) for c in range(128))
code128C_mapping = dict([(u'%02d' % i, [i]) for i in range(100)] + [(u'%d' % i, [100, 16+i]) for i in range(10)])
code128_chars = u''.join(chr(c) for c in [212] + list(range(33,126+1)) + list(range(200,211+1)))

def encode128(s):
    ''' Code 128 conversion for a font as described at
        https://en.wikipedia.org/wiki/Code_128 and downloaded
        from http://www.barcodelink.net/barcode-font.php
        Only encodes ASCII characters, does not take advantage of
        FNC4 for bytes with the upper bit set. Control characters
        are not optimized and expand to 2 characters each.
        Coded for https://stackoverflow.com/q/52710760/5987
    '''
    if s.isdigit() and len(s) >= 2:
        # use Code 128C, pairs of digits
        codes = [105] + list_join(code128C_mapping[s[i:i+2]] for i in range(0, len(s), 2))
    else:
        # use Code 128B and shift for Code 128A
        codes = [104] + list_join(code128B_mapping[c] for c in s)
    check_digit = (codes[0] + sum(i * x for i,x in enumerate(codes))) % 103
    codes.append(check_digit)
    codes.append(106) # stop code
    return u''.join(code128_chars[x] for x in codes)

def encode128(s):
    ''' Code 128 conversion for a font as described at
        https://en.wikipedia.org/wiki/Code_128 and downloaded
        from http://www.barcodelink.net/barcode-font.php
        Only encodes ASCII characters, does not take advantage of
        FNC4 for bytes with the upper bit set.
        It does not attempt to optimize the length of the string,
        Code B is the default to prefer lower case over control characters.
        Coded for https://stackoverflow.com/q/52710760/5987
    '''
    s = s.encode('ascii').decode('ascii')
    if s.isdigit() and len(s) % 2 == 0:
        # use Code 128C, pairs of digits
        codes = [105]
        for i in range(0, len(s), 2):
            codes.append(int(s[i:i+2], 10))
    else:
        # use Code 128B and shift for Code 128A
        mapping = dict((chr(c), [98, c + 64] if c < 32 else [c - 32]) for c in range(128))
        codes = [104]
        for c in s:
            codes.extend(mapping[c])
    check_digit = (codes[0] + sum(i * x for i,x in enumerate(codes))) % 103
    codes.append(check_digit)
    codes.append(106) # stop code
    chars = (b'\xd4' + bytes(range(33,126+1)) + bytes(range(200,211+1))).decode('latin-1')
    return ''.join(chars[x] for x in codes)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

用于 Code128 条形码字体的 Python Code128 编码器 的相关文章

随机推荐

  • SQL Server Asp.Net - “登录失败”

    我无法让我的 Asp Net 应用程序在我要部署到的服务器上运行 服务器运行的是 Server 2008 R2 我在其上运行 SQL Server 2008 R2 Express 当我浏览该网站时出现错误 用户 WIN 6VLI5UDJ5R
  • 如何在 Firefox 中从 JavaScript 解析 HTML?

    在 Firefox 中解析 XmlHttpRequest 的 HTML 结果 获取 DOM 树 的最佳方法是什么 EDIT I do not有DOM树 我想获取它 XmlHttpRequest 的 responseXML 仅在结果是实际 X
  • 如何防止 Bootstrap 列从一个部分跳到另一部分或在彼此下面?

    我正在为我的项目制作推荐部分 有 4 个 div 但它们的内容不均匀 因此当我开始将屏幕的宽度拉在一起时 col sm 6他们应该像 2 2 一样排列 但是 3 号 div 跳到 4 号 div 的位置 留下一个空白空间 然后 4 号 di
  • 就性能而言,什么时候用 BufferedOutputStream 包装 FileOutputStream 才有意义?

    我有一个模块负责读取 处理字节并将其写入磁盘 字节通过 UDP 传入 在组装各个数据报后 处理并写入磁盘的最终字节数组通常在 200 字节到 500 000 字节之间 有时 字节数组在组装后会超过 500 000 字节 但这种情况相对较少
  • 摆脱科学计数法

    我需要做一些计算 但我遇到的问题是值非常低 例如 我需要得到 0 005 的 2 7 最终得到 1 3500000000000003e 4 这不是我想要的 我只需要知道如何获得这些值的准确百分比 我现在正在做的是
  • IsOrderedBy 扩展方法

    在我的一些测试中 我需要检查列表的顺序并执行类似的操作 DateTime lastDate new DateTime 2009 10 1 foreach DueAssigmentViewModel assignment in dueAssi
  • Common Lisp 中格式指令的安全解析

    我想从输入文件中读取字符串 用户可能已修改也可能未修改 我想将此字符串视为要使用固定数量的参数调用的格式指令 但是 我了解一些格式指令 特别是 我想到 可能会被用来注入函数调用 使得这种方法本质上是不安全的 使用时read为了在 Commo
  • 我在 Windows 中设置 java 环境时遇到问题

    我有这样的 Gradle 构建 plugins id org springframework boot version 2 4 2 id io spring dependency management version 1 0 11 RELE
  • 为什么 DefaultMessageListenerContainer 不应该使用 CachingConnectionFactory?

    我正在阅读 spring 文档默认消息监听容器 http docs spring io spring docs 3 2 7 RELEASE javadoc api org springframework jms listener Defau
  • Apache Velocity 仍然是一个活跃的项目吗? [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 看到 Velocity 的最新稳定版本至少在两年前发布 我希望 SF 的好心人能够建议这个项目是否仍在积极维护 除了 JIRA 上的 Ve
  • 响应式 CSS 网格布局,位置:固定

    我正在使用 CSS 网格布局构建一个响应式模板 仍在学习 感谢这里的一些人 我已经完成了大部分工作 移动设备 最大宽度 767px 一切都应该出现在自己的行上 平板电脑 最小宽度 768px 导航位于第一行 旁边和主要在第二个 桌面 最小宽
  • 如何横向显示 SwiftUI 预览

    previewLayout fixed width 480 height 320 在 Xcode 13 4 和 Xcode 14 beta 3 中没有效果 The preview is shown in portrait vs expect
  • 使用预提交排除运行黑屏时的一些文件

    我想在预提交中配置黑色 并从检查任何迁移文件夹中排除预提交 我的 pyproject toml 看起来像这样 tool black line length 79 target version py37 include pyi exclude
  • Grails Webflow - 将事物保持在流程范围之外

    我错过了一些东西 我有一个 Grails 网络流程 如下所示 def childFlow start action def targets Target list each target gt targets add new TargetC
  • eig(X, 'nobalance') 的八度等效值是多少

    我试图找到马尔可夫链的平衡分布 这意味着找到代表它的转移矩阵的特征值 但是 eig函数自动标准化它返回的特征向量 在MatLab中有一个标志你可以传递给函数停止这种行为 eig X 不平衡 其中 X 是矩阵 看http www mathwo
  • 如何将指针传递给 LuaJIT ffi 以用作 out 参数?

    假设有以下C代码 struct Foo int dummy int tryToAllocateFoo Foo dest 如何在 LuaJIT 中执行以下操作 Foo pFoo NULL tryToAllocateFoo pFoo local
  • 在没有母版页控件的情况下打印asp.net页面

    我想打印我的页面 接受母版页的元素 母版页中有一个用户控件 这对我来说很重要 我的打印按钮也在主页上 谢谢 您需要创建新的样式表 print css 并设置 CSS media print 例如 并将 yesPrint 类添加到要打印的部分
  • 尝试请求存储库时,TypeORM 未找到连接“默认”

    我正在使用 Express TypeORM 构建一个 API 这是我的 ormconfig json type postgres host localhost port 5432 username mdsp9070 password mds
  • 实体框架数据库首先更新多重性冲突

    我看到的关于这些多重性冲突的大多数其他问题都是代码优先 而我首先是数据库 我进行了一些架构更改 当我从数据库刷新模型时收到以下错误 多重性与关系 FK MarketSelectionWager Bet 中角色 Bet 中的引用约束冲突 由于
  • 用于 Code128 条形码字体的 Python Code128 编码器

    就像标题所说 我有一个 Code128 字体 我想用它来打印条形码 但是 该字符串需要以 Code128 进行编码才能使条形码字体正常工作 我的应用程序使用Python3语言 网络上曾经有一个示例 介绍如何将字符串编码为 Code128 字