15.13 HALCON/Python 中的 UTF-8

默认情况下,所有从 Python 传递到 HALCON 的字符串都编码为 UTF-8 字节,所有从 HALCON 传递到 HALCON/Python 的字符串字节都解码为 UTF-8 字符串。这是唯一支持的编码。

不支持通过 set_system('filename_encoding', 'locale') 配置 HALCON 以使用本地编码。

在传统模式下使用时,即使 "filename_encoding" 设置为 "utf8",某些算子也可能返回无效的 UTF-8 字符串。针对这种情况,有一种变通方法,即:

try:
    # Replace invalid UTF-8 when encoding HALCON strings for use in Python.
    ha.ffi.enable_utf8_error_replace()

    # Call problematic operator ...
finally:
    # Restore default behavior of raising a UnicodeError.
    ha.ffi.disable_utf8_error_replace()

建议将禁用安全默认模式作为一种变通方法,而且只应在需要时禁用,禁用时间也应视需要而定。